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
|
|
|
|
PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
|
|
|
|
# Crea un artículo nuevo
|
|
|
|
#
|
|
|
|
# @return Post
|
|
|
|
def create
|
2020-05-23 15:38:03 +00:00
|
|
|
self.post = site.posts(lang: locale)
|
|
|
|
.build(layout: 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
|
|
|
|
2020-07-21 22:19:40 +00:00
|
|
|
commit(action: :created, file: update_related_posts) 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-06-16 22:10:54 +00:00
|
|
|
# Crear un post anónimo, con opciones más limitadas. No usamos post.
|
2020-02-18 16:45:08 +00:00
|
|
|
def create_anonymous
|
|
|
|
# XXX: Confiamos en el parámetro de idioma porque estamos
|
|
|
|
# verificándolos en Site#posts
|
2020-05-23 15:38:03 +00:00
|
|
|
self.post = site.posts(lang: locale)
|
|
|
|
.build(layout: layout)
|
2020-02-18 16:45:08 +00:00
|
|
|
# Los artículos anónimos siempre son borradores
|
2020-06-16 22:10:54 +00:00
|
|
|
params[:draft] = true
|
2020-02-18 16:45:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
# Es importante que el artículo se guarde primero y luego los
|
|
|
|
# relacionados.
|
2020-07-21 22:19:40 +00:00
|
|
|
commit(action: :updated, file: update_related_posts) 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
|
2020-10-04 00:32:32 +00:00
|
|
|
reorder = params.require(:post).permit(reorder: {})&.dig(:reorder)&.transform_values(&:to_i)
|
2020-08-28 16:48:04 +00:00
|
|
|
posts = site.posts(lang: locale).where(uuid: reorder.keys)
|
2019-10-18 20:35:09 +00:00
|
|
|
|
2020-08-28 16:48:04 +00:00
|
|
|
files = posts.map do |post|
|
|
|
|
next unless post.attribute? :order
|
|
|
|
|
|
|
|
order = reorder[post.uuid.value]
|
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
|
|
|
post.order.value = order
|
2019-10-18 20:35:09 +00:00
|
|
|
post.path.absolute
|
|
|
|
end.compact
|
|
|
|
|
2020-08-28 16:48:04 +00:00
|
|
|
return if files.empty?
|
|
|
|
|
2019-10-18 20:35:09 +00:00
|
|
|
# TODO: Implementar transacciones!
|
2020-08-28 16:48:04 +00:00
|
|
|
posts.save_all(validate: false) &&
|
2019-12-11 20:05:31 +00:00
|
|
|
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}",
|
2020-10-04 00:32:32 +00:00
|
|
|
title: post&.title&.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
|
2020-05-23 15:38:03 +00:00
|
|
|
params.permit(post.params).delete_if do |k, _|
|
2020-08-07 14:15:06 +00:00
|
|
|
%w[date slug order uuid].include? k.to_s
|
2020-02-18 16:45:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-23 15:38:03 +00:00
|
|
|
def locale
|
2021-03-03 12:43:15 +00:00
|
|
|
params.dig(:post, :lang)&.to_sym || I18n.locale
|
2020-03-19 18:31:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def layout
|
|
|
|
params.dig(:post, :layout) || params[:layout]
|
2019-10-18 20:35:09 +00:00
|
|
|
end
|
2020-07-21 22:19:40 +00:00
|
|
|
|
|
|
|
# Actualiza los artículos relacionados según los métodos que los
|
|
|
|
# metadatos declaren.
|
|
|
|
#
|
2021-02-11 18:27:44 +00:00
|
|
|
# Este método se asegura que todos los artículos se guardan una sola
|
|
|
|
# vez.
|
|
|
|
#
|
2020-07-21 22:19:40 +00:00
|
|
|
# @return [Array] Lista de archivos modificados
|
|
|
|
def update_related_posts
|
2021-02-11 18:27:44 +00:00
|
|
|
posts = Set.new
|
2020-07-21 22:19:40 +00:00
|
|
|
|
|
|
|
post.attributes.each do |a|
|
2020-07-22 23:35:43 +00:00
|
|
|
post[a].related_methods.each do |m|
|
2020-07-21 22:19:40 +00:00
|
|
|
next unless post[a].respond_to? m
|
|
|
|
|
2020-07-22 19:36:39 +00:00
|
|
|
# La respuesta puede ser una PostRelation también
|
2021-02-11 18:27:44 +00:00
|
|
|
posts.merge [post[a].public_send(m)].flatten.compact
|
2020-07-21 22:19:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
posts.map do |p|
|
|
|
|
p.path.absolute if p.save(validate: false)
|
|
|
|
end.compact << post.path.absolute
|
2020-07-21 22:19:40 +00:00
|
|
|
end
|
2019-08-13 23:33:57 +00:00
|
|
|
end
|