5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-03 12:46:08 +00:00
panel/app/services/post_service.rb
2020-03-19 15:31:29 -03:00

121 lines
3.2 KiB
Ruby

# frozen_string_literal: true
# Este servicio se encarga de crear artículos y guardarlos en git,
# asignándoselos a une usuarie
# rubocop:disable Metrics/BlockLength
PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
# Crea un artículo nuevo
#
# @return Post
def create
self.post = site.posts(lang: lang)
.build(layout: params[:post][:layout])
post.usuaries << usuarie
params[:post][:draft] = true if site.invitade? usuarie
commit(action: :created) if post.update(post_params)
# Devolver el post aunque no se haya salvado para poder rescatar los
# errores
post
end
# Crear un post anónimo, con opciones más limitadas
def create_anonymous
# XXX: Confiamos en el parámetro de idioma porque estamos
# verificándolos en Site#posts
self.post = site.posts(lang: lang)
.build(layout: params[:post][:layout])
# Los artículos anónimos siempre son borradores
params[:post][:draft] = true
commit(action: :created) if post.update(anon_post_params)
post
end
def update
post.usuaries << usuarie
params[:post][:draft] = true if site.invitade? usuarie
commit(action: :updated) if post.update(post_params)
# Devolver el post aunque no se haya salvado para poder rescatar los
# errores
post
end
def destroy
post.destroy!
commit(action: :destroyed) if post.destroyed?
post
end
# Reordena todos los posts que soporten orden de acuerdo a un hash de
# uuids y nuevas posiciones. La posición actual la da la posición en
# el array.
#
# { uuid => 2, uuid => 1, uuid => 0 }
def reorder
posts = site.posts(lang: lang)
reorder = params.require(:post).permit(reorder: {}).try(:[], :reorder)
modified = PostRelation.new(site: site)
files = reorder.keys.map do |uuid|
post = posts.find(uuid, uuid: true)
order = reorder[uuid].to_i
next unless post
next unless post.attributes.include? :order
next if post.order.value == order
modified << post
post.order.value = order
post.path.absolute
end.compact
# TODO: Implementar transacciones!
modified.save_all(validate: false) &&
commit(action: :reorder, file: files)
end
private
def commit(action:, file: nil)
site.repository.commit(file: file || post.path.absolute,
usuarie: usuarie,
remove: action == :destroyed,
message: I18n.t("post_service.#{action}",
title: post.try(:title).try(:value)))
end
# Solo permitir cambiar estos atributos de cada articulo
def post_params
params.require(:post).permit(post.params)
end
# Eliminar metadatos internos
def anon_post_params
post_params.delete_if do |k, _|
%w[date slug order uuid].include? k
end
end
# Devuelve el idioma solicitado a través de un parámetro, validando
# que el sitio soporte ese idioma
#
# TODO: DRY
def lang
return unless params[:lang]
return unless site.try(:locales).try(:include?, params[:lang])
params[:lang]
end
def layout
params.dig(:post, :layout) || params[:layout]
end
end
# rubocop:enable Metrics/BlockLength