5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-17 08:36:38 +00:00
panel/app/controllers/posts_controller.rb

109 lines
2.5 KiB
Ruby
Raw Permalink 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
2019-07-03 23:25:23 +00:00
before_action :authenticate_usuarie!
2018-01-29 22:19:10 +00:00
def index
authorize Post
@site = find_site
@category = session[:category] = params.dig(:category)
2019-11-07 16:06:40 +00:00
@layout = params.dig(:layout).try :to_sym
# TODO: Aplicar policy_scope
2020-03-19 18:31:29 +00:00
@posts = @site.posts(lang: lang)
@posts.sort_by!(:order, :date).reverse!
@usuarie = @site.usuarie? current_usuarie
2018-01-29 22:19:10 +00:00
end
2018-01-30 15:20:19 +00:00
def show
@site = find_site
2020-03-19 18:31:29 +00:00
@post = @site.posts(lang: lang).find params[:id]
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
def new
2018-09-28 17:15:09 +00:00
authorize Post
@site = find_site
2020-03-19 18:31:29 +00:00
@post = @site.posts.build(lang: lang, layout: params[:layout])
end
def create
2018-09-28 17:15:09 +00:00
authorize Post
@site = find_site
2019-08-13 23:33:57 +00:00
service = PostService.new(site: @site,
usuarie: current_usuarie,
params: params)
2018-12-14 15:12:17 +00:00
2019-11-15 16:35:27 +00:00
if (@post = service.create.persisted?)
2019-08-13 23:33:57 +00:00
redirect_to site_posts_path(@site)
else
render 'posts/new'
end
end
2018-01-31 20:29:27 +00:00
def edit
@site = find_site
2020-03-19 18:31:29 +00:00
@post = @site.posts(lang: lang).find params[:id]
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
2020-03-19 18:31:29 +00:00
@post = @site.posts(lang: lang).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?
redirect_to site_posts_path(@site)
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-03-19 18:31:29 +00:00
@post = @site.posts(lang: lang).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
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
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
# que el sitio soporte ese idioma
def lang
return unless params[:lang]
return unless @site.try(:locales).try(:include?, params[:lang])
params[:lang]
end
2018-01-29 22:19:10 +00:00
end