# frozen_string_literal: true # Controlador para artículos class PostsController < ApplicationController include Pundit before_action :authenticate_usuarie! def index authorize Post @site = find_site @category = session[:category] = params.dig(:category) # TODO: Aplicar policy_scope @posts = @site.posts(lang: I18n.locale) if params[:sort_by].present? begin @posts.sort_by! do |p| p.send(params[:sort_by].to_s) end rescue ArgumentError end end end def show @site = find_site @post = @site.posts.find params[:id] authorize @post end def new authorize Post @site = find_site # TODO: Implementar layout @post = @site.posts.build(lang: I18n.locale) end def create authorize Post @site = find_site service = PostService.new(site: @site, usuarie: current_usuarie, params: params) if service.create.persisted? redirect_to site_posts_path(@site) else render 'posts/new' end end def edit @site = find_site @post = @site.posts.find params[:id] authorize @post end def update @site = find_site @lang = find_lang(@site) @post = find_post(@site) authorize @post @post.update_attributes(repair_nested_params(post_params)) # Solo las usuarias pueden modificar la autoría if @site.usuarie? current_usuarie if params[:post][:author].present? @post.update_attributes(author: params[:post][:author]) end @post.update_attributes(draft: false) else # Todo lo que crean les invitades es borrador @post.update_attributes(draft: true) end if @post.save flash[:success] = @site.config.dig('thanks') redirect_to site_posts_path(@site, category: session[:category], lang: @lang) else render 'posts/edit' end end # Eliminar artículos def destroy @site = find_site @lang = find_lang(@site) @post = find_post(@site) authorize @post @post.destroy redirect_to site_posts_path(@site, category: session[:category], lang: @lang) end end