5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 01:16:23 +00:00
panel/app/controllers/posts_controller.rb
f aa9806d8f6
optimizacion: no preguntar si es usuarie todo el tiempo
solo funciona cuando le usuarie es usuarie del sitio.  si es invitade,
la pregunta se hace por cada post.
2020-01-02 15:05:28 -03:00

99 lines
2.2 KiB
Ruby

# frozen_string_literal: true
# Controlador para artículos
class PostsController < ApplicationController
include Pundit
before_action :authenticate_usuarie!
def index
authorize Post
@site = find_site
@category = session[:category] = params.dig(:category)
@layout = params.dig(:layout).try :to_sym
# TODO: Aplicar policy_scope
@posts = @site.posts(lang: I18n.locale)
@posts.sort_by!(:order, :date).reverse!
@usuarie = @site.usuarie? current_usuarie
end
def show
@site = find_site
@post = @site.posts.find params[:id]
authorize @post
end
def new
authorize Post
@site = find_site
@post = @site.posts.build(lang: I18n.locale, layout: params[:layout])
end
def create
authorize Post
@site = find_site
service = PostService.new(site: @site,
usuarie: current_usuarie,
params: params)
if (@post = service.create.persisted?)
redirect_to site_posts_path(@site)
else
render 'posts/new'
end
end
def edit
@site = find_site
@post = @site.posts.find params[:id]
authorize @post
end
def update
@site = find_site
@post = @site.posts.find params[:id]
authorize @post
service = PostService.new(site: @site,
post: @post,
usuarie: current_usuarie,
params: params)
if service.update.persisted?
redirect_to site_posts_path(@site)
else
render 'posts/edit'
end
end
# Eliminar artículos
def destroy
@site = find_site
@post = @site.posts.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
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
redirect_to site_posts_path(@site)
end
end