5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-15 04:51:43 +00:00
panel/app/controllers/posts_controller.rb

107 lines
2.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
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
# TODO: por qué no lo está leyendo @site.posts?
@site.read
@category = session[:category] = params.dig(:category)
# TODO: Aplicar policy_scope
@posts = @site.posts(lang: I18n.locale)
2018-04-30 20:50:29 +00:00
if params[:sort_by].present?
begin
@posts.sort_by! do |p|
p.send(params[:sort_by].to_s)
end
rescue ArgumentError
2018-04-30 20:50:29 +00:00
end
end
2018-01-29 22:19:10 +00:00
end
2018-01-30 15:20:19 +00:00
def show
@site = find_site
@lang = find_lang(@site)
2018-01-30 15:20:19 +00:00
@post = find_post(@site)
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
# TODO: Implementar layout
@post = @site.posts.build(lang: I18n.locale)
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-08-13 23:33:57 +00:00
if service.create.persisted?
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
@lang = find_lang(@site)
2018-01-31 20:29:27 +00:00
@post = find_post(@site)
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
@lang = find_lang(@site)
2018-02-02 22:20:31 +00:00
@post = find_post(@site)
2018-09-28 17:15:09 +00:00
authorize @post
@post.update_attributes(repair_nested_params(post_params))
2018-09-28 17:35:40 +00:00
# Solo las usuarias pueden modificar la autoría
2019-07-29 18:20:19 +00:00
if @site.usuarie? current_usuarie
2019-07-03 23:25:23 +00:00
if params[:post][:author].present?
@post.update_attributes(author: params[:post][:author])
end
2019-03-26 15:32:20 +00:00
@post.update_attributes(draft: false)
else
2019-08-06 17:50:58 +00:00
# Todo lo que crean les invitades es borrador
2019-03-26 15:32:20 +00:00
@post.update_attributes(draft: true)
2018-09-28 17:35:40 +00:00
end
2018-02-02 22:20:31 +00:00
if @post.save
flash[:success] = @site.config.dig('thanks')
2019-07-03 23:25:23 +00:00
redirect_to site_posts_path(@site,
category: session[:category],
lang: @lang)
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
@lang = find_lang(@site)
@post = find_post(@site)
authorize @post
@post.destroy
2019-07-03 23:25:23 +00:00
redirect_to site_posts_path(@site, category: session[:category],
lang: @lang)
2019-05-30 17:33:51 +00:00
end
2018-01-29 22:19:10 +00:00
end