ya que estaba, un poco de refactorización para no escribir tantas @

memoizamos site y post cuando podemos
This commit is contained in:
f 2021-05-09 13:11:15 -03:00
parent 8fe343ae45
commit a9da4b3f2c
2 changed files with 33 additions and 31 deletions

View file

@ -85,31 +85,24 @@ class PostsController < ApplicationController
end
def edit
@site = find_site
@post = @site.posts(lang: locale).find params[:id]
authorize @post
@locale = locale
breadcrumb @post.title.value, site_post_path(@site, @post, locale: locale), match: :exact
authorize post
breadcrumb post.title.value, site_post_path(site, post, locale: locale), match: :exact
breadcrumb 'posts.edit', ''
end
def update
@site = find_site
@post = @site.posts(lang: locale).find params[:id]
authorize post
authorize @post
service = PostService.new(site: @site,
post: @post,
service = PostService.new(site: site,
post: post,
usuarie: current_usuarie,
params: params)
if service.update.persisted?
@site.touch
site.touch
forget_content
redirect_to site_post_path(@site, @post)
redirect_to site_post_path(site, post)
else
render 'posts/edit'
end
@ -117,34 +110,30 @@ class PostsController < ApplicationController
# Eliminar artículos
def destroy
@site = find_site
@post = @site.posts(lang: locale).find params[:id]
authorize post
authorize @post
service = PostService.new(site: @site,
post: @post,
service = PostService.new(site: site,
post: post,
usuarie: current_usuarie,
params: params)
# TODO: Notificar si se pudo o no
service.destroy
@site.touch
redirect_to site_posts_path(@site)
site.touch
redirect_to site_posts_path(site)
end
# Reordenar los artículos
def reorder
@site = find_site
authorize @site
authorize site
service = PostService.new(site: @site,
service = PostService.new(site: site,
usuarie: current_usuarie,
params: params)
service.reorder
@site.touch
redirect_to site_posts_path(@site)
site.touch
redirect_to site_posts_path(site)
end
# Devuelve el idioma solicitado a través de un parámetro, validando
@ -155,7 +144,7 @@ class PostsController < ApplicationController
# 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
@site&.locales&.find(-> { I18n.locale }) do |l|
@locale ||= site&.locales&.find(-> { I18n.locale }) do |l|
l.to_s == params[:locale]
end
end
@ -165,4 +154,14 @@ class PostsController < ApplicationController
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
end

View file

@ -14,8 +14,7 @@ class UsuariesController < ApplicationController
# Mostrar todes les usuaries e invitades de un sitio
def index
@site = find_site
site_usuarie = SiteUsuarie.new(@site, current_usuarie)
site_usuarie = SiteUsuarie.new(site, current_usuarie)
authorize site_usuarie
breadcrumb 'usuaries.index', ''
@ -163,4 +162,8 @@ class UsuariesController < ApplicationController
'invitade'
end
end
def site
@site ||= find_site
end
end