5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-01 23:26:07 +00:00
panel/app/controllers/posts_controller.rb

168 lines
4.4 KiB
Ruby
Raw Normal View History

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
include Pundit
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
# 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
2021-05-10 17:48:48 +00:00
{ locale: current_locale }
2020-05-23 15:38:03 +00:00
end
2018-01-29 22:19:10 +00:00
def index
authorize Post
2020-05-23 15:38:03 +00:00
@site = find_site
2020-05-23 15:38:03 +00:00
@category = params.dig(:category)
@layout = params.dig(:layout)
2020-05-23 16:52:58 +00:00
@locale = locale
2020-05-11 20:28:38 +00:00
# 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?([current_usuarie, @site])
2020-05-23 15:38:03 +00:00
@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
2020-05-23 15:38:03 +00:00
# Orden descendiente por número y luego por fecha
@posts.sort_by!(:order, :date).reverse!
end
2018-01-29 22:19:10 +00:00
end
2018-01-30 15:20:19 +00:00
def show
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
# Genera una previsualización del artículo.
def preview
authorize post
render html: post.render
end
def new
2018-09-28 17:15:09 +00:00
authorize Post
@post = site.posts.build(lang: locale, layout: params[:layout])
breadcrumb I18n.t('loaf.breadcrumbs.posts.new', layout: @post.layout.humanized_name.downcase), ''
end
def create
2018-09-28 17:15:09 +00:00
authorize Post
service = PostService.new(site: site,
2019-08-13 23:33:57 +00:00
usuarie: current_usuarie,
params: params)
@post = service.create
2018-12-14 15:12:17 +00:00
if @post.persisted?
site.touch
forget_content
redirect_to site_post_path(@site, @post)
else
render 'posts/new'
end
end
2018-01-31 20:29:27 +00:00
def edit
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
authorize post
2018-09-28 17:15:09 +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?
site.touch
forget_content
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
authorize post
2019-05-30 17:33:51 +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
site.touch
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
authorize site
2019-10-18 20:35:09 +00:00
service = PostService.new(site: site,
2019-10-18 20:35:09 +00:00
usuarie: current_usuarie,
params: params)
service.reorder
site.touch
redirect_to site_posts_path(site)
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
@locale ||= site&.locales&.find(-> { site&.default_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
# 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
private
def site
@site ||= find_site
end
def post
@post ||= site.posts(lang: locale).find(params[:post_id] || params[:id])
end
2018-01-29 22:19:10 +00:00
end