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
|
|
|
|
# TODO: Implementar layout
|
|
|
|
self.post = site.posts.build
|
|
|
|
# TODO: No podemos pasar los post_params a build aun porque para
|
|
|
|
# saber los parámetros tenemos que haber instanciado el post
|
|
|
|
# primero.
|
2019-08-14 21:19:01 +00:00
|
|
|
post.update_attributes(post_params) && commit(action: :created)
|
|
|
|
|
|
|
|
# Devolver el post aunque no se haya salvado para poder rescatar los
|
|
|
|
# errores
|
|
|
|
post
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
# TODO: No podemos pasar los post_params a build aun porque para
|
|
|
|
# saber los parámetros tenemos que haber instanciado el post
|
|
|
|
# primero.
|
|
|
|
post.update_attributes(post_params) && commit(action: :updated)
|
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
|
|
|
|
post.destroy! && commit(action: :destroyed)
|
|
|
|
|
|
|
|
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
|