5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-02 12:56:07 +00:00
panel/app/services/post_service.rb

58 lines
1.5 KiB
Ruby
Raw Normal View History

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
# 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
self.post = site.posts(lang: params[:post][:lang] || I18n.locale).build
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
def update
post.usuaries << usuarie
params[:post][:draft] = true if site.invitade? usuarie
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
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
def commit(action:)
site.repository.commit(file: post.path.absolute,
usuarie: usuarie,
2019-08-16 23:12:22 +00:00
remove: action == :destroyed,
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
# rubocop:enable Metrics/BlockLength