2019-03-26 15:32:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-03 23:25:23 +00:00
|
|
|
# Controlador para artículos
|
2018-01-29 22:19:10 +00:00
|
|
|
class PostsController < ApplicationController
|
2018-09-28 15:47:28 +00:00
|
|
|
include Pundit
|
2020-06-08 23:25:05 +00:00
|
|
|
rescue_from Pundit::NilPolicyError, with: :page_not_found
|
|
|
|
|
2019-07-03 23:25:23 +00:00
|
|
|
before_action :authenticate_usuarie!
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2020-05-23 15:38:03 +00:00
|
|
|
# Las URLs siempre llevan el idioma actual o el de le usuarie
|
|
|
|
def default_url_options
|
2020-10-04 00:32:32 +00:00
|
|
|
{ locale: params[:locale] || current_usuarie&.lang || I18n.locale }
|
2020-05-23 15:38:03 +00:00
|
|
|
end
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
def index
|
2019-02-16 18:17:18 +00:00
|
|
|
authorize Post
|
2020-05-23 15:38:03 +00:00
|
|
|
|
2019-08-13 19:09:23 +00:00
|
|
|
@site = find_site
|
2021-05-07 20:45:07 +00:00
|
|
|
@locale = locale
|
2020-05-11 20:28:38 +00:00
|
|
|
|
2020-05-12 15:15:28 +00:00
|
|
|
# XXX: Cada vez que cambiamos un Post tocamos el sitio con lo que es
|
|
|
|
# más simple saber si hubo cambios.
|
2021-05-07 19:17:25 +00:00
|
|
|
if filter_params.present? || stale?(@site)
|
|
|
|
# Todos los artículos de este sitio para el idioma actual
|
2021-05-07 22:25:09 +00:00
|
|
|
@posts = @site.indexed_posts.where(locale: locale)
|
2021-05-07 19:17:25 +00:00
|
|
|
# De este tipo
|
|
|
|
@posts = @posts.where(layout: filter_params[:layout]) if filter_params[:layout]
|
|
|
|
# Que estén dentro de la categoría
|
|
|
|
@posts = @posts.in_category(filter_params[:category]) if filter_params[:category]
|
|
|
|
# Aplicar los parámetros de búsqueda
|
|
|
|
@posts = @posts.search(locale, filter_params[:q]) if filter_params[:q].present?
|
|
|
|
# A los que este usuarie tiene acceso
|
2020-09-25 17:07:54 +00:00
|
|
|
@posts = PostPolicy::Scope.new(current_usuarie, @posts).resolve
|
|
|
|
|
|
|
|
# Filtrar los posts que les invitades no pueden ver
|
|
|
|
@usuarie = @site.usuarie? current_usuarie
|
2020-05-12 15:15:28 +00:00
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
2018-01-30 15:20:19 +00:00
|
|
|
|
|
|
|
def show
|
|
|
|
@site = find_site
|
2020-05-23 15:38:03 +00:00
|
|
|
@post = @site.posts(lang: locale).find params[:id]
|
2020-06-08 23:25:05 +00:00
|
|
|
|
2018-09-28 17:15:09 +00:00
|
|
|
authorize @post
|
2020-05-23 18:42:12 +00:00
|
|
|
@locale = locale
|
2020-05-11 20:28:38 +00:00
|
|
|
|
|
|
|
fresh_when @post
|
2018-01-30 15:20:19 +00:00
|
|
|
end
|
2018-01-31 20:29:27 +00:00
|
|
|
|
2020-10-30 21:57:46 +00:00
|
|
|
# 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
|
|
|
|
|
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
|
2020-05-23 15:38:03 +00:00
|
|
|
@post = @site.posts.build(lang: locale, layout: params[:layout])
|
2020-05-23 18:42:12 +00:00
|
|
|
@locale = locale
|
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
|
2019-08-13 23:33:57 +00:00
|
|
|
service = PostService.new(site: @site,
|
|
|
|
usuarie: current_usuarie,
|
|
|
|
params: params)
|
2020-07-24 18:03:13 +00:00
|
|
|
@post = service.create
|
2018-12-14 15:12:17 +00:00
|
|
|
|
2020-07-24 18:03:13 +00:00
|
|
|
if @post.persisted?
|
2020-05-11 22:01:41 +00:00
|
|
|
@site.touch
|
2020-11-20 18:01:13 +00:00
|
|
|
forget_content
|
2020-05-11 22:01:41 +00:00
|
|
|
|
2020-07-22 23:36:34 +00:00
|
|
|
redirect_to site_post_path(@site, @post)
|
2018-10-01 22:36:58 +00:00
|
|
|
else
|
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
|
2020-05-23 15:38:03 +00:00
|
|
|
@post = @site.posts(lang: locale).find params[:id]
|
2018-09-28 17:15:09 +00:00
|
|
|
|
|
|
|
authorize @post
|
2020-05-23 18:42:12 +00:00
|
|
|
|
|
|
|
@locale = locale
|
2018-01-31 20:29:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2018-02-02 22:20:31 +00:00
|
|
|
@site = find_site
|
2020-05-23 15:38:03 +00:00
|
|
|
@post = @site.posts(lang: locale).find params[:id]
|
2018-09-28 17:15:09 +00:00
|
|
|
|
|
|
|
authorize @post
|
|
|
|
|
2019-08-16 23:12:22 +00:00
|
|
|
service = PostService.new(site: @site,
|
|
|
|
post: @post,
|
|
|
|
usuarie: current_usuarie,
|
|
|
|
params: params)
|
2018-09-28 17:35:40 +00:00
|
|
|
|
2019-08-16 23:12:22 +00:00
|
|
|
if service.update.persisted?
|
2020-05-11 22:01:41 +00:00
|
|
|
@site.touch
|
2020-11-20 18:01:13 +00:00
|
|
|
forget_content
|
2020-05-11 22:01:41 +00:00
|
|
|
|
2020-07-22 23:36:34 +00:00
|
|
|
redirect_to site_post_path(@site, @post)
|
2018-02-02 22:20:31 +00:00
|
|
|
else
|
|
|
|
render 'posts/edit'
|
|
|
|
end
|
2018-01-31 20:29:27 +00:00
|
|
|
end
|
|
|
|
|
2019-05-30 17:33:51 +00:00
|
|
|
# Eliminar artículos
|
|
|
|
def destroy
|
|
|
|
@site = find_site
|
2020-05-23 15:38:03 +00:00
|
|
|
@post = @site.posts(lang: locale).find params[:id]
|
2019-05-30 17:33:51 +00:00
|
|
|
|
|
|
|
authorize @post
|
|
|
|
|
2019-08-16 23:12:22 +00:00
|
|
|
service = PostService.new(site: @site,
|
|
|
|
post: @post,
|
|
|
|
usuarie: current_usuarie,
|
|
|
|
params: params)
|
2019-05-30 17:33:51 +00:00
|
|
|
|
2019-08-16 23:12:22 +00:00
|
|
|
# TODO: Notificar si se pudo o no
|
|
|
|
service.destroy
|
2020-05-11 22:01:41 +00:00
|
|
|
@site.touch
|
2019-08-16 23:12:22 +00:00
|
|
|
redirect_to site_posts_path(@site)
|
2019-05-30 17:33:51 +00:00
|
|
|
end
|
2019-10-18 20:35:09 +00:00
|
|
|
|
|
|
|
# Reordenar los artículos
|
|
|
|
def reorder
|
|
|
|
@site = find_site
|
|
|
|
authorize @site
|
|
|
|
|
|
|
|
service = PostService.new(site: @site,
|
|
|
|
usuarie: current_usuarie,
|
|
|
|
params: params)
|
|
|
|
|
|
|
|
service.reorder
|
2020-05-11 22:01:41 +00:00
|
|
|
@site.touch
|
2019-10-18 20:35:09 +00:00
|
|
|
redirect_to site_posts_path(@site)
|
|
|
|
end
|
2020-03-19 18:31:29 +00:00
|
|
|
|
|
|
|
# Devuelve el idioma solicitado a través de un parámetro, validando
|
2020-05-23 15:38:03 +00:00
|
|
|
# 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
|
2021-03-03 12:43:15 +00:00
|
|
|
@site&.locales&.find(-> { I18n.locale }) do |l|
|
|
|
|
l.to_s == params[:locale]
|
2020-05-23 15:38:03 +00:00
|
|
|
end
|
2020-03-19 18:31:29 +00:00
|
|
|
end
|
2020-11-20 18:01:13 +00:00
|
|
|
|
|
|
|
# Instruye al editor a olvidarse el contenido del artículo. Usar
|
|
|
|
# cuando hayamos guardado la información correctamente.
|
|
|
|
def forget_content
|
2021-03-27 20:18:09 +00:00
|
|
|
flash[:js] = { target: 'editor', action: 'forget-content', keys: (params[:storage_keys] || []).to_json }
|
2020-11-20 18:01:13 +00:00
|
|
|
end
|
2021-05-07 19:17:25 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Los parámetros de filtros que vamos a mantener en todas las URLs,
|
|
|
|
# solo los que no estén vacíos.
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
def filter_params
|
|
|
|
@filter_params ||= params.permit(:q, :category, :layout).to_h.select { |_,v| v.present? }
|
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|