# 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! # Las URLs siempre llevan el idioma actual o el de le usuarie def default_url_options { locale: params[:locale] || current_usuarie&.lang || I18n.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?(@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 @site = find_site @post = @site.posts(lang: locale).find params[:id] authorize @post @locale = locale fresh_when @post end # Genera una previsualización del artículo. # # TODO: No todos los artículos tienen previsualización! def preview @site = find_site @post = @site.posts(lang: locale).find params[:post_id] authorize @post render html: @post.render end def new authorize Post @site = find_site @post = @site.posts.build(lang: locale, layout: params[:layout]) @locale = locale end def create authorize Post @site = find_site 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 @site = find_site @post = @site.posts(lang: locale).find params[:id] authorize @post @locale = locale end def update @site = find_site @post = @site.posts(lang: locale).find params[:id] 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 @site = find_site @post = @site.posts(lang: locale).find params[:id] 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) end # Reordenar los artículos def reorder @site = find_site authorize @site service = PostService.new(site: @site, usuarie: current_usuarie, params: params) service.reorder @site.touch redirect_to site_posts_path(@site) 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 @site&.locales&.find(-> { I18n.locale.to_s }) do |l| l == 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 end