2019-03-26 15:32:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
class PostsController < ApplicationController
|
2018-09-28 15:47:28 +00:00
|
|
|
include Pundit
|
2018-01-29 22:19:10 +00:00
|
|
|
before_action :authenticate!
|
|
|
|
|
|
|
|
def index
|
2019-02-16 18:17:18 +00:00
|
|
|
authorize Post
|
2018-02-22 19:01:11 +00:00
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-02-19 21:16:48 +00:00
|
|
|
@category = session[:category] = params.dig(:category)
|
2018-09-28 15:47:28 +00:00
|
|
|
@posts = policy_scope(@site.posts_for(@lang), policy_scope_class: PostPolicy::Scope)
|
2018-04-30 20:50:29 +00:00
|
|
|
|
|
|
|
if params[:sort_by].present?
|
2018-09-28 15:47:28 +00:00
|
|
|
begin
|
|
|
|
@posts.sort_by! do |p|
|
|
|
|
p.send(params[:sort_by].to_s)
|
|
|
|
end
|
|
|
|
rescue ArgumentError
|
2018-04-30 20:50:29 +00:00
|
|
|
end
|
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
2018-01-30 15:20:19 +00:00
|
|
|
|
|
|
|
def show
|
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-01-30 15:20:19 +00:00
|
|
|
@post = find_post(@site)
|
2018-09-28 17:15:09 +00:00
|
|
|
authorize @post
|
2018-01-30 15:20:19 +00:00
|
|
|
end
|
2018-01-31 20:29:27 +00:00
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
def new
|
2018-09-28 17:15:09 +00:00
|
|
|
authorize Post
|
2018-02-03 22:37:09 +00:00
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-05-08 21:57:11 +00:00
|
|
|
@template = find_template(@site)
|
|
|
|
@post = Post.new(site: @site,
|
2019-03-26 15:32:20 +00:00
|
|
|
front_matter: { date: Time.now },
|
|
|
|
lang: @lang,
|
|
|
|
template: @template)
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-09-28 17:15:09 +00:00
|
|
|
authorize Post
|
2018-02-03 22:37:09 +00:00
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-05-17 18:04:33 +00:00
|
|
|
@template = find_template(@site)
|
|
|
|
@post = Post.new(site: @site, lang: @lang, template: @template)
|
2018-07-27 14:22:08 +00:00
|
|
|
@post.update_attributes(repair_nested_params(post_params))
|
2018-02-03 22:37:09 +00:00
|
|
|
|
2018-12-14 15:12:17 +00:00
|
|
|
# El post se guarda como incompleto si se envió con "guardar para
|
|
|
|
# después"
|
2019-03-26 15:32:20 +00:00
|
|
|
@post.update_attributes(incomplete: params[:commit_incomplete].present?)
|
2018-12-14 15:12:17 +00:00
|
|
|
|
2018-09-28 18:10:10 +00:00
|
|
|
# Las usuarias pueden especificar una autora, de la contrario por
|
|
|
|
# defecto es la usuaria actual
|
|
|
|
if current_user.is_a? Usuaria
|
2019-03-26 15:32:20 +00:00
|
|
|
@post.update_attributes(author: params[:post][:author])
|
2018-10-01 22:36:58 +00:00
|
|
|
else
|
|
|
|
# Todo lo que crean lxs invitadxs es borrador
|
2019-03-26 15:32:20 +00:00
|
|
|
@post.update_attributes(draft: true)
|
2018-09-28 18:10:10 +00:00
|
|
|
end
|
2019-03-26 15:32:20 +00:00
|
|
|
@post.update_attributes(author: current_user.username) unless @post.author
|
2018-09-28 17:35:40 +00:00
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
if @post.save
|
2018-10-01 22:36:58 +00:00
|
|
|
flash[:success] = @site.config.dig('thanks')
|
2018-02-24 21:24:11 +00:00
|
|
|
redirect_to site_posts_path(@site, lang: @lang)
|
2018-02-03 22:37:09 +00:00
|
|
|
else
|
2018-12-14 15:12:17 +00:00
|
|
|
@invalid = true
|
2018-02-03 22:37:09 +00:00
|
|
|
render 'posts/new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-31 20:29:27 +00:00
|
|
|
def edit
|
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-01-31 20:29:27 +00:00
|
|
|
@post = find_post(@site)
|
2018-09-28 17:15:09 +00:00
|
|
|
|
|
|
|
authorize @post
|
2018-01-31 20:29:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2018-02-02 22:20:31 +00:00
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-02-02 22:20:31 +00:00
|
|
|
@post = find_post(@site)
|
2018-09-28 17:15:09 +00:00
|
|
|
|
|
|
|
authorize @post
|
|
|
|
|
2018-07-24 20:49:32 +00:00
|
|
|
@post.update_attributes(repair_nested_params(post_params))
|
2018-02-03 22:37:09 +00:00
|
|
|
|
2018-09-28 17:35:40 +00:00
|
|
|
# Solo las usuarias pueden modificar la autoría
|
|
|
|
if current_user.is_a? Usuaria
|
2019-03-26 15:32:20 +00:00
|
|
|
@post.update_attributes(author: params[:post][:author]) if params[:post][:author].present?
|
|
|
|
@post.update_attributes(draft: false)
|
2018-10-01 22:36:58 +00:00
|
|
|
else
|
|
|
|
# Todo lo que crean lxs invitadxs es borrador
|
2019-03-26 15:32:20 +00:00
|
|
|
@post.update_attributes(draft: true)
|
2018-09-28 17:35:40 +00:00
|
|
|
end
|
|
|
|
|
2018-02-02 22:20:31 +00:00
|
|
|
if @post.save
|
2018-10-01 22:36:58 +00:00
|
|
|
flash[:success] = @site.config.dig('thanks')
|
2018-02-24 21:24:11 +00:00
|
|
|
redirect_to site_posts_path(@site, category: session[:category], lang: @lang)
|
2018-02-02 22:20:31 +00:00
|
|
|
else
|
|
|
|
render 'posts/edit'
|
|
|
|
end
|
2018-01-31 20:29:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-02-02 22:20:31 +00:00
|
|
|
# Solo permitir cambiar estos atributos de cada articulo
|
2018-01-31 20:29:27 +00:00
|
|
|
def post_params
|
2018-06-21 18:15:03 +00:00
|
|
|
params.require(:post).permit(@post.template_params)
|
2018-01-31 20:29:27 +00:00
|
|
|
end
|
2018-07-24 20:49:32 +00:00
|
|
|
|
|
|
|
# https://gist.github.com/bloudermilk/2884947#gistcomment-1915521
|
|
|
|
def repair_nested_params(obj)
|
|
|
|
obj.each do |key, value|
|
|
|
|
if value.is_a?(ActionController::Parameters) || value.is_a?(Hash)
|
|
|
|
# If any non-integer keys
|
2019-03-26 15:32:20 +00:00
|
|
|
if value.keys.find { |k, _| k =~ /\D/ }
|
2018-07-24 20:49:32 +00:00
|
|
|
repair_nested_params(value)
|
|
|
|
else
|
|
|
|
obj[key] = value.values
|
2019-03-26 15:32:20 +00:00
|
|
|
obj[key].each { |h| repair_nested_params(h) }
|
2018-07-24 20:49:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|