class PostsController < ApplicationController before_action :authenticate! def index @site = find_site @lang = find_lang(@site) @category = session[:category] = params.dig(:category) @posts = @site.posts_for(@lang) if params[:sort_by].present? @posts.sort_by! do |p| p.send(params[:sort_by].to_s) end end end def show @site = find_site @lang = find_lang(@site) @post = find_post(@site) end def new @site = find_site @lang = find_lang(@site) @template = find_template(@site) @post = Post.new(site: @site, front_matter: { date: Time.now }, lang: @lang, template: @template) end def create @site = find_site @lang = find_lang(@site) @post = Post.new(site: @site, front_matter: post_params.to_hash, lang: @lang) if @post.save redirect_to site_posts_path(@site, lang: @lang) else render 'posts/new' end end def edit @site = find_site @lang = find_lang(@site) @post = find_post(@site) end def update @site = find_site @lang = find_lang(@site) @post = find_post(@site) @post.update_attributes(post_params) if @post.save redirect_to site_posts_path(@site, category: session[:category], lang: @lang) else render 'posts/edit' end end private # Solo permitir cambiar estos atributos de cada articulo def post_params default_post_params = [:title, :date, :content, :slug, :cover, :layout, :permalink, :dir, { lang: {} }, { tags: [] }, { categories: [] }] params.require(:post).permit(default_post_params + @post.template_params) end end