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
|
|
|
|
# TODO: Implementar layout
|
2019-09-17 21:27:51 +00:00
|
|
|
self.post = site.posts(lang: params[:post][:lang] || I18n.locale).build
|
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
|
|
|
|
|
|
|
|
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-08-13 23:33:57 +00:00
|
|
|
private
|
|
|
|
|
2019-08-14 21:19:01 +00:00
|
|
|
def commit(action:)
|
|
|
|
site.repository.commit(file: post.path.absolute,
|
|
|
|
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}",
|
|
|
|
title: post.title.value))
|
|
|
|
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
|
|
|
|
end
|
2019-09-25 22:31:33 +00:00
|
|
|
# rubocop:enable Metrics/BlockLength
|