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
|
2024-07-10 20:04:59 +00:00
|
|
|
include StrongParamsHelper
|
|
|
|
|
2019-07-03 23:25:23 +00:00
|
|
|
before_action :authenticate_usuarie!
|
2022-03-06 18:25:15 +00:00
|
|
|
before_action :service_for_direct_upload, only: %i[new edit]
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2021-05-09 16:08:18 +00:00
|
|
|
# TODO: Traer los comunes desde ApplicationController
|
|
|
|
breadcrumb -> { current_usuarie.email }, :edit_usuarie_registration_path
|
|
|
|
breadcrumb 'sites.index', :sites_path, match: :exact
|
|
|
|
breadcrumb -> { site.title }, -> { site_posts_path(site, locale: locale) }, match: :exact
|
|
|
|
|
2020-05-23 15:38:03 +00:00
|
|
|
# Las URLs siempre llevan el idioma actual o el de le usuarie
|
|
|
|
def default_url_options
|
2023-03-18 22:14:12 +00:00
|
|
|
{ locale: locale }
|
2020-05-23 15:38:03 +00:00
|
|
|
end
|
|
|
|
|
2024-05-17 18:24:08 +00:00
|
|
|
# @todo Mover a tu propio scope
|
|
|
|
def new_array
|
2024-07-10 20:04:59 +00:00
|
|
|
@value = pluck_param(:value)
|
|
|
|
@name = pluck_param(:name)
|
|
|
|
id = pluck_param(:id)
|
2024-05-17 18:24:08 +00:00
|
|
|
|
|
|
|
headers['HX-Trigger-After-Swap'] = 'htmx:resetForm'
|
|
|
|
|
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_array_value
|
2024-07-10 20:04:59 +00:00
|
|
|
@value = pluck_param(:value)
|
2024-05-17 18:24:08 +00:00
|
|
|
|
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
2024-05-17 20:58:44 +00:00
|
|
|
def new_related_post
|
2024-07-10 20:04:59 +00:00
|
|
|
@uuid = pluck_param(:value)
|
2024-05-17 20:58:44 +00:00
|
|
|
|
|
|
|
@indexed_post = site.indexed_posts.find_by!(post_id: @uuid)
|
|
|
|
|
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
2024-06-03 20:52:53 +00:00
|
|
|
def new_has_one
|
2024-07-10 20:04:59 +00:00
|
|
|
@uuid = pluck_param(:value)
|
2024-06-03 20:52:53 +00:00
|
|
|
|
|
|
|
@indexed_post = site.indexed_posts.find_by!(post_id: @uuid)
|
|
|
|
|
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
|
|
|
# El formulario de un Post, si pasamos el UUID, estamos editando, sino
|
2024-05-18 15:10:37 +00:00
|
|
|
# estamos creando.
|
|
|
|
def form
|
2024-07-10 20:04:59 +00:00
|
|
|
uuid = pluck_param(:uuid, optional: true)
|
2024-05-20 17:12:53 +00:00
|
|
|
locale
|
2024-05-18 15:10:37 +00:00
|
|
|
|
|
|
|
@post =
|
|
|
|
if uuid.present?
|
|
|
|
site.indexed_posts.find_by!(post_id: uuid).post
|
|
|
|
else
|
|
|
|
# @todo Usar la base de datos
|
2024-07-10 20:04:59 +00:00
|
|
|
site.posts(lang: locale).build(layout: pluck_param(:layout))
|
2024-05-18 15:10:37 +00:00
|
|
|
end
|
|
|
|
|
2024-05-20 17:12:53 +00:00
|
|
|
swap_modals
|
2024-05-18 21:50:00 +00:00
|
|
|
|
2024-05-18 15:10:37 +00:00
|
|
|
render layout: false
|
|
|
|
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
|
|
|
|
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-10-21 16:25:05 +00:00
|
|
|
return unless stale?([current_usuarie, site, filter_params])
|
|
|
|
|
|
|
|
# Todos los artículos de este sitio para el idioma actual
|
2023-05-19 17:36:42 +00:00
|
|
|
@posts = site.indexed_posts.where(locale: locale)
|
|
|
|
@posts = @posts.page(filter_params.delete(:page)) if site.pagination
|
2021-10-21 16:25:05 +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
|
|
|
|
@posts = PostPolicy::Scope.new(current_usuarie, @posts).resolve
|
|
|
|
|
|
|
|
# Filtrar los posts que les invitades no pueden ver
|
|
|
|
@usuarie = site.usuarie? current_usuarie
|
2022-07-15 17:17:46 +00:00
|
|
|
|
|
|
|
@site_stat = SiteStat.new(site)
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
2018-01-30 15:20:19 +00:00
|
|
|
|
|
|
|
def show
|
2021-05-09 16:08:18 +00:00
|
|
|
authorize post
|
|
|
|
breadcrumb post.title.value, ''
|
|
|
|
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.
|
|
|
|
def preview
|
2021-05-09 16:08:18 +00:00
|
|
|
authorize post
|
2020-10-30 21:57:46 +00:00
|
|
|
|
2021-05-09 16:08:18 +00:00
|
|
|
render html: post.render
|
2020-10-30 21:57:46 +00:00
|
|
|
end
|
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
def new
|
2018-09-28 17:15:09 +00:00
|
|
|
authorize Post
|
2024-07-10 20:04:59 +00:00
|
|
|
@post = site.posts(lang: locale).build(layout: pluck_param(:layout))
|
2021-05-09 16:08:18 +00:00
|
|
|
|
|
|
|
breadcrumb I18n.t('loaf.breadcrumbs.posts.new', layout: @post.layout.humanized_name.downcase), ''
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-09-28 17:15:09 +00:00
|
|
|
authorize Post
|
2021-05-09 16:08:18 +00:00
|
|
|
service = PostService.new(site: site,
|
2019-08-13 23:33:57 +00:00
|
|
|
usuarie: current_usuarie,
|
|
|
|
params: params)
|
2024-06-04 20:08:31 +00:00
|
|
|
@post = service.create_or_update
|
2018-12-14 15:12:17 +00:00
|
|
|
|
2024-06-03 20:52:53 +00:00
|
|
|
if post.persisted?
|
2021-05-09 16:08:18 +00:00
|
|
|
site.touch
|
2020-11-20 18:01:13 +00:00
|
|
|
forget_content
|
2024-05-20 17:12:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# @todo Enviar la creación a otro endpoint para evitar tantas
|
|
|
|
# condiciones.
|
|
|
|
if htmx?
|
2024-06-03 20:52:53 +00:00
|
|
|
if post.persisted?
|
2024-07-10 20:04:59 +00:00
|
|
|
triggers = { 'notification:show' => { 'id' => pluck_param(:saved, optional: true) } }
|
2024-06-19 21:21:44 +00:00
|
|
|
|
|
|
|
swap_modals(triggers)
|
2020-05-11 22:01:41 +00:00
|
|
|
|
2024-06-03 20:52:53 +00:00
|
|
|
@value = post.title.value
|
|
|
|
@uuid = post.uuid.value
|
2024-07-10 20:04:59 +00:00
|
|
|
@name = pluck_param(:name)
|
2024-05-20 17:12:53 +00:00
|
|
|
|
2024-05-28 16:15:45 +00:00
|
|
|
render render_path_from_attribute, layout: false
|
2024-05-20 17:12:53 +00:00
|
|
|
else
|
2024-07-10 20:04:59 +00:00
|
|
|
headers['HX-Retarget'] = "##{pluck_param(:form)}"
|
2024-05-27 18:13:11 +00:00
|
|
|
headers['HX-Reswap'] = 'outerHTML'
|
|
|
|
|
|
|
|
render 'posts/form', layout: false, post: post, site: site, **params.permit(:form, :base, :dir, :locale)
|
2024-05-20 17:12:53 +00:00
|
|
|
end
|
2024-06-03 20:52:53 +00:00
|
|
|
elsif post.persisted?
|
|
|
|
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
|
2021-05-09 16:11:15 +00:00
|
|
|
authorize post
|
|
|
|
breadcrumb post.title.value, site_post_path(site, post, locale: locale), match: :exact
|
|
|
|
breadcrumb 'posts.edit', ''
|
2018-01-31 20:29:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2021-05-09 16:11:15 +00:00
|
|
|
authorize post
|
2018-09-28 17:15:09 +00:00
|
|
|
|
2021-05-09 16:11:15 +00:00
|
|
|
service = PostService.new(site: site,
|
|
|
|
post: post,
|
2019-08-16 23:12:22 +00:00
|
|
|
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?
|
2021-05-09 16:11:15 +00:00
|
|
|
site.touch
|
2020-11-20 18:01:13 +00:00
|
|
|
forget_content
|
2024-06-04 20:09:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if htmx?
|
|
|
|
if post.persisted?
|
2024-07-10 20:04:59 +00:00
|
|
|
triggers = { 'notification:show' => pluck_param(:saved, optional: true) }
|
2024-06-19 21:21:44 +00:00
|
|
|
|
|
|
|
swap_modals(triggers)
|
2024-06-04 20:09:04 +00:00
|
|
|
|
|
|
|
@value = post.title.value
|
|
|
|
@uuid = post.uuid.value
|
2024-07-10 20:04:59 +00:00
|
|
|
@name = pluck_param(:name)
|
2020-05-11 22:01:41 +00:00
|
|
|
|
2024-06-04 20:09:04 +00:00
|
|
|
render render_path_from_attribute, layout: false
|
|
|
|
else
|
|
|
|
headers['HX-Retarget'] = "##{params.require(:form)}"
|
|
|
|
headers['HX-Reswap'] = 'outerHTML'
|
|
|
|
|
|
|
|
render 'posts/form', layout: false, post: post, site: site, **params.permit(:form, :base, :dir, :locale)
|
|
|
|
end
|
|
|
|
elsif post.persisted?
|
2021-05-09 16:11:15 +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
|
2021-05-09 16:11:15 +00:00
|
|
|
authorize post
|
2019-05-30 17:33:51 +00:00
|
|
|
|
2021-05-09 16:11:15 +00:00
|
|
|
service = PostService.new(site: site,
|
|
|
|
post: post,
|
2019-08-16 23:12:22 +00:00
|
|
|
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
|
2021-05-09 16:11:15 +00:00
|
|
|
site.touch
|
2021-05-11 20:59:30 +00:00
|
|
|
redirect_to site_posts_path(site, locale: post.lang.value)
|
2019-05-30 17:33:51 +00:00
|
|
|
end
|
2019-10-18 20:35:09 +00:00
|
|
|
|
|
|
|
# Reordenar los artículos
|
|
|
|
def reorder
|
2021-05-09 16:11:15 +00:00
|
|
|
authorize site
|
2019-10-18 20:35:09 +00:00
|
|
|
|
2021-05-09 16:11:15 +00:00
|
|
|
service = PostService.new(site: site,
|
2019-10-18 20:35:09 +00:00
|
|
|
usuarie: current_usuarie,
|
|
|
|
params: params)
|
|
|
|
|
|
|
|
service.reorder
|
2021-05-09 16:11:15 +00:00
|
|
|
site.touch
|
2021-05-10 18:18:04 +00:00
|
|
|
redirect_to site_posts_path(site, locale: site.default_locale)
|
2019-10-18 20:35:09 +00:00
|
|
|
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-05-10 17:49:01 +00:00
|
|
|
@locale ||= site&.locales&.find(-> { site&.default_locale }) do |l|
|
2021-03-03 12:43:15 +00:00
|
|
|
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
|
2023-05-19 17:34:24 +00:00
|
|
|
@filter_params ||= params.permit(:q, :category, :layout, :page).to_hash.select do |_, v|
|
2021-10-21 17:25:05 +00:00
|
|
|
v.present?
|
|
|
|
end.transform_keys(&:to_sym)
|
2021-05-14 15:53:35 +00:00
|
|
|
end
|
|
|
|
|
2021-05-09 16:11:15 +00:00
|
|
|
def post
|
|
|
|
@post ||= site.posts(lang: locale).find(params[:post_id] || params[:id])
|
2021-05-07 19:17:25 +00:00
|
|
|
end
|
2022-03-06 18:25:15 +00:00
|
|
|
|
|
|
|
# Recuerda el nombre del servicio de subida de archivos
|
|
|
|
def service_for_direct_upload
|
|
|
|
session[:service_name] = site.name.to_sym
|
|
|
|
end
|
2024-05-20 17:12:53 +00:00
|
|
|
|
|
|
|
# @param triggers [Hash] Otros disparadores
|
|
|
|
def swap_modals(triggers = {})
|
|
|
|
params.permit(:show, :hide).each_pair do |key, value|
|
2024-06-03 20:52:53 +00:00
|
|
|
triggers["modal:#{key}"] = { id: value } if value.present?
|
2024-05-20 17:12:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
headers['HX-Trigger'] = triggers.to_json if triggers.present?
|
|
|
|
end
|
2024-05-28 16:15:45 +00:00
|
|
|
|
|
|
|
# @return [String]
|
|
|
|
def render_path_from_attribute
|
2024-07-10 20:04:59 +00:00
|
|
|
case pluck_param(:attribute)
|
2024-05-28 16:15:45 +00:00
|
|
|
when 'new_has_many' then 'posts/new_has_many_value'
|
|
|
|
when 'new_belongs_to' then 'posts/new_belongs_to_value'
|
2024-05-28 19:27:39 +00:00
|
|
|
when 'new_has_and_belongs_to_many' then 'posts/new_has_many_value'
|
2024-06-03 20:52:53 +00:00
|
|
|
when 'new_has_one' then 'posts/new_has_one_value'
|
2024-05-28 16:15:45 +00:00
|
|
|
else 'nothing'
|
|
|
|
end
|
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|