mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-22 15:46:21 +00:00
93 lines
2 KiB
Ruby
93 lines
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)
|
|
# TODO: Aplicar policy_scope
|
|
@posts = @site.posts(lang: I18n.locale)
|
|
|
|
if params[:sort_by].present?
|
|
begin
|
|
@posts.sort_by! do |p|
|
|
p.send(params[:sort_by].to_s)
|
|
end
|
|
rescue ArgumentError
|
|
end
|
|
end
|
|
end
|
|
|
|
def show
|
|
@site = find_site
|
|
@post = @site.posts.find params[:id]
|
|
authorize @post
|
|
end
|
|
|
|
def new
|
|
authorize Post
|
|
@site = find_site
|
|
# TODO: Implementar layout
|
|
@post = @site.posts.build(lang: I18n.locale)
|
|
end
|
|
|
|
def create
|
|
authorize Post
|
|
@site = find_site
|
|
service = PostService.new(site: @site,
|
|
usuarie: current_usuarie,
|
|
params: params)
|
|
|
|
if 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
|
|
end
|