# 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 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) # Devolver el post aunque no se haya salvado para poder rescatar los # errores post end def destroy post.destroy! commit(action: :destroyed) if post.destroyed? post end private def commit(action:) site.repository.commit(file: post.path.absolute, usuarie: usuarie, remove: action == :destroyed, message: I18n.t("post_service.#{action}", title: post.title.value)) end # Solo permitir cambiar estos atributos de cada articulo def post_params params.require(:post).permit(post.params) end end # rubocop:enable Metrics/BlockLength