2019-08-13 23:33:57 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Este servicio se encarga de crear artículos y guardarlos en git,
|
|
|
|
# asignándoselos a une usuarie
|
2019-09-25 22:31:33 +00:00
|
|
|
# rubocop:disable Metrics/BlockLength
|
2019-08-13 23:33:57 +00:00
|
|
|
PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
|
|
|
|
# Crea un artículo nuevo
|
|
|
|
#
|
|
|
|
# @return Post
|
|
|
|
def create
|
2020-02-18 16:45:08 +00:00
|
|
|
self.post = site.posts(lang: lang)
|
2019-11-15 16:35:27 +00:00
|
|
|
.build(layout: params[:post][:layout])
|
2019-09-25 22:31:33 +00:00
|
|
|
post.usuaries << usuarie
|
2019-09-30 18:41:50 +00:00
|
|
|
params[:post][:draft] = true if site.invitade? usuarie
|
2019-09-17 21:27:51 +00:00
|
|
|
|
|
|
|
commit(action: :created) if post.update(post_params)
|
2019-08-14 21:19:01 +00:00
|
|
|
|
|
|
|
# Devolver el post aunque no se haya salvado para poder rescatar los
|
|
|
|
# errores
|
|
|
|
post
|
|
|
|
end
|
|
|
|
|
2020-02-18 16:45:08 +00:00
|
|
|
# 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
|
|
|
|
|
2019-08-14 21:19:01 +00:00
|
|
|
def update
|
2019-09-25 22:31:33 +00:00
|
|
|
post.usuaries << usuarie
|
2019-09-30 18:41:50 +00:00
|
|
|
params[:post][:draft] = true if site.invitade? usuarie
|
|
|
|
|
2019-09-17 21:27:51 +00:00
|
|
|
commit(action: :updated) if post.update(post_params)
|
2019-08-13 23:33:57 +00:00
|
|
|
|
|
|
|
# Devolver el post aunque no se haya salvado para poder rescatar los
|
|
|
|
# errores
|
|
|
|
post
|
|
|
|
end
|
|
|
|
|
2019-08-16 23:12:22 +00:00
|
|
|
def destroy
|
2019-09-17 21:27:51 +00:00
|
|
|
post.destroy!
|
|
|
|
|
|
|
|
commit(action: :destroyed) if post.destroyed?
|
2019-08-16 23:12:22 +00:00
|
|
|
|
|
|
|
post
|
|
|
|
end
|
|
|
|
|
2019-11-06 22:35:48 +00:00
|
|
|
# Reordena todos los posts que soporten orden de acuerdo a un hash de
|
2020-01-02 23:29:04 +00:00
|
|
|
# uuids y nuevas posiciones. La posición actual la da la posición en
|
2019-10-18 20:35:09 +00:00
|
|
|
# el array.
|
|
|
|
#
|
2020-01-02 23:29:04 +00:00
|
|
|
# { uuid => 2, uuid => 1, uuid => 0 }
|
2019-10-18 20:35:09 +00:00
|
|
|
def reorder
|
|
|
|
posts = site.posts(lang: lang)
|
2019-11-06 22:35:48 +00:00
|
|
|
reorder = params.require(:post).permit(reorder: {}).try(:[], :reorder)
|
2019-12-11 20:05:31 +00:00
|
|
|
modified = PostRelation.new(site: site)
|
2019-10-18 20:35:09 +00:00
|
|
|
|
2020-01-02 23:29:04 +00:00
|
|
|
files = reorder.keys.map do |uuid|
|
|
|
|
post = posts.find(uuid, uuid: true)
|
|
|
|
order = reorder[uuid].to_i
|
2019-12-11 20:05:31 +00:00
|
|
|
|
2020-02-14 15:11:55 +00:00
|
|
|
next unless post
|
2019-10-18 20:35:09 +00:00
|
|
|
next unless post.attributes.include? :order
|
2019-12-11 20:05:31 +00:00
|
|
|
next if post.order.value == order
|
2019-10-18 20:35:09 +00:00
|
|
|
|
2019-12-11 20:05:31 +00:00
|
|
|
modified << post
|
|
|
|
post.order.value = order
|
2019-10-18 20:35:09 +00:00
|
|
|
post.path.absolute
|
|
|
|
end.compact
|
|
|
|
|
|
|
|
# TODO: Implementar transacciones!
|
2019-12-11 20:05:31 +00:00
|
|
|
modified.save_all(validate: false) &&
|
|
|
|
commit(action: :reorder, file: files)
|
2019-10-18 20:35:09 +00:00
|
|
|
end
|
|
|
|
|
2019-08-13 23:33:57 +00:00
|
|
|
private
|
|
|
|
|
2019-10-18 20:35:09 +00:00
|
|
|
def commit(action:, file: nil)
|
|
|
|
site.repository.commit(file: file || post.path.absolute,
|
2019-08-14 21:19:01 +00:00
|
|
|
usuarie: usuarie,
|
2019-08-16 23:12:22 +00:00
|
|
|
remove: action == :destroyed,
|
2019-08-14 21:19:01 +00:00
|
|
|
message: I18n.t("post_service.#{action}",
|
2019-10-18 20:35:09 +00:00
|
|
|
title: post.try(:title).try(:value)))
|
2019-08-14 21:19:01 +00:00
|
|
|
end
|
|
|
|
|
2019-08-13 23:33:57 +00:00
|
|
|
# Solo permitir cambiar estos atributos de cada articulo
|
|
|
|
def post_params
|
|
|
|
params.require(:post).permit(post.params)
|
|
|
|
end
|
2019-10-18 20:35:09 +00:00
|
|
|
|
2020-02-18 16:45:08 +00:00
|
|
|
# Eliminar metadatos internos
|
|
|
|
def anon_post_params
|
|
|
|
post_params.delete_if do |k, _|
|
|
|
|
%w[date slug order uuid].include? k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-19 18:31:29 +00:00
|
|
|
# Devuelve el idioma solicitado a través de un parámetro, validando
|
|
|
|
# que el sitio soporte ese idioma
|
|
|
|
#
|
|
|
|
# TODO: DRY
|
2019-10-18 20:35:09 +00:00
|
|
|
def lang
|
2020-03-19 18:31:29 +00:00
|
|
|
return unless params[:lang]
|
|
|
|
return unless site.try(:locales).try(:include?, params[:lang])
|
|
|
|
|
|
|
|
params[:lang]
|
|
|
|
end
|
|
|
|
|
|
|
|
def layout
|
|
|
|
params.dig(:post, :layout) || params[:layout]
|
2019-10-18 20:35:09 +00:00
|
|
|
end
|
2019-08-13 23:33:57 +00:00
|
|
|
end
|
2019-09-25 22:31:33 +00:00
|
|
|
# rubocop:enable Metrics/BlockLength
|