5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-03 13:16:08 +00:00
panel/app/controllers/posts_controller.rb
f 072662cab8 siempre mandar un array en data-keys
closes #887
closes #886
closes #885
closes #884
closes #877
closes #876
closes #871
closes #867
closes #865
2021-03-27 17:18:09 -03:00

173 lines
4.3 KiB
Ruby

# frozen_string_literal: true
# Controlador para artículos
class PostsController < ApplicationController
include Pundit
rescue_from Pundit::NilPolicyError, with: :page_not_found
before_action :authenticate_usuarie!
# Las URLs siempre llevan el idioma actual o el de le usuarie
def default_url_options
{ locale: params[:locale] || current_usuarie&.lang || I18n.locale }
end
def index
authorize Post
@site = find_site
@category = params.dig(:category)
@layout = params.dig(:layout)
@locale = locale
# XXX: Cada vez que cambiamos un Post tocamos el sitio con lo que es
# más simple saber si hubo cambios.
if @category || @layout || stale?(@site)
@posts = @site.posts(lang: locale)
@posts = @posts.where(categories: @category) if @category
@posts = @posts.where(layout: @layout) if @layout
@posts = PostPolicy::Scope.new(current_usuarie, @posts).resolve
@category_name = if uuid?(@category)
@site.posts(lang: locale).find(@category, uuid: true)&.title&.value
else
@category
end
# Filtrar los posts que les invitades no pueden ver
@usuarie = @site.usuarie? current_usuarie
# Orden descendiente por número y luego por fecha
@posts.sort_by!(:order, :date).reverse!
end
end
def show
@site = find_site
@post = @site.posts(lang: locale).find params[:id]
authorize @post
@locale = locale
fresh_when @post
end
# 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
def new
authorize Post
@site = find_site
@post = @site.posts.build(lang: locale, layout: params[:layout])
@locale = locale
end
def create
authorize Post
@site = find_site
service = PostService.new(site: @site,
usuarie: current_usuarie,
params: params)
@post = service.create
if @post.persisted?
@site.touch
forget_content
redirect_to site_post_path(@site, @post)
else
render 'posts/new'
end
end
def edit
@site = find_site
@post = @site.posts(lang: locale).find params[:id]
authorize @post
@locale = locale
end
def update
@site = find_site
@post = @site.posts(lang: locale).find params[:id]
authorize @post
service = PostService.new(site: @site,
post: @post,
usuarie: current_usuarie,
params: params)
if service.update.persisted?
@site.touch
forget_content
redirect_to site_post_path(@site, @post)
else
render 'posts/edit'
end
end
# Eliminar artículos
def destroy
@site = find_site
@post = @site.posts(lang: locale).find params[:id]
authorize @post
service = PostService.new(site: @site,
post: @post,
usuarie: current_usuarie,
params: params)
# TODO: Notificar si se pudo o no
service.destroy
@site.touch
redirect_to site_posts_path(@site)
end
# Reordenar los artículos
def reorder
@site = find_site
authorize @site
service = PostService.new(site: @site,
usuarie: current_usuarie,
params: params)
service.reorder
@site.touch
redirect_to site_posts_path(@site)
end
# Devuelve el idioma solicitado a través de un parámetro, validando
# 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
@site&.locales&.find(-> { I18n.locale }) do |l|
l.to_s == params[:locale]
end
end
# Instruye al editor a olvidarse el contenido del artículo. Usar
# cuando hayamos guardado la información correctamente.
def forget_content
flash[:js] = { target: 'editor', action: 'forget-content', keys: (params[:storage_keys] || []).to_json }
end
end