# frozen_string_literal: true # Controlador para artículos class PostsController < ApplicationController include Pundit rescue_from Pundit::NilPolicyError, with: :page_not_found before_action :authenticate_usuarie! # TODO: Traer los comunes desde ApplicationController breadcrumb -> { current_usuarie.email }, :edit_usuarie_registration_path breadcrumb 'sites.index', :sites_path, match: :exact breadcrumb -> { site.title }, -> { site_posts_path(site, locale: locale) }, match: :exact # Las URLs siempre llevan el idioma actual o el de le usuarie def default_url_options { locale: current_locale } end def index authorize Post @site = find_site @category = params.dig(:category) @layout = params.dig(:layout) @locale = locale # XXX: Cada vez que cambiamos un Post tocamos el sitio con lo que es # más simple saber si hubo cambios. if @category || @layout || stale?([current_usuarie, @site]) @posts = @site.posts(lang: locale) @posts = @posts.where(categories: @category) if @category @posts = @posts.where(layout: @layout) if @layout @posts = PostPolicy::Scope.new(current_usuarie, @posts).resolve @category_name = if uuid?(@category) @site.posts(lang: locale).find(@category, uuid: true)&.title&.value else @category end # Filtrar los posts que les invitades no pueden ver @usuarie = @site.usuarie? current_usuarie # Orden descendiente por número y luego por fecha @posts.sort_by!(:order, :date).reverse! end end def show authorize post breadcrumb post.title.value, '' fresh_when post end # Genera una previsualización del artículo. def preview authorize post render html: post.render end def new authorize Post @post = site.posts.build(lang: locale, layout: params[:layout]) breadcrumb I18n.t('loaf.breadcrumbs.posts.new', layout: @post.layout.humanized_name.downcase), '' end def create authorize Post service = PostService.new(site: site, usuarie: current_usuarie, params: params) @post = service.create if @post.persisted? site.touch forget_content redirect_to site_post_path(@site, @post) else render 'posts/new' end end def edit authorize post breadcrumb post.title.value, site_post_path(site, post, locale: locale), match: :exact breadcrumb 'posts.edit', '' end def update authorize post service = PostService.new(site: site, post: post, usuarie: current_usuarie, params: params) if service.update.persisted? site.touch forget_content redirect_to site_post_path(site, post) else render 'posts/edit' end end # Eliminar artículos def destroy authorize post service = PostService.new(site: site, post: post, usuarie: current_usuarie, params: params) # TODO: Notificar si se pudo o no service.destroy site.touch redirect_to site_posts_path(site, locale: post.locale.value) end # Reordenar los artículos def reorder authorize site service = PostService.new(site: site, usuarie: current_usuarie, params: params) service.reorder site.touch redirect_to site_posts_path(site, locale: site.default_locale) end # Devuelve el idioma solicitado a través de un parámetro, validando # que el sitio soporte ese idioma, de lo contrario devuelve el idioma # actual. # # TODO: Debería devolver un error o mostrar una página donde se # solicite a le usuarie crear el nuevo idioma y que esto lo agregue al # _config.yml del sitio en lugar de mezclar idiomas. def locale @locale ||= site&.locales&.find(-> { site&.default_locale }) do |l| l.to_s == params[:locale] end end # Instruye al editor a olvidarse el contenido del artículo. Usar # cuando hayamos guardado la información correctamente. def forget_content flash[:js] = { target: 'editor', action: 'forget-content', keys: (params[:storage_keys] || []).to_json } end private def site @site ||= find_site end def post @post ||= site.posts(lang: locale).find(params[:post_id] || params[:id]) end end