permitir cambiar de idioma

This commit is contained in:
f 2020-03-19 15:31:29 -03:00
parent 8fcbc2280f
commit 26247a9544
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D
2 changed files with 27 additions and 7 deletions

View file

@ -11,21 +11,21 @@ class PostsController < ApplicationController
@category = session[:category] = params.dig(:category)
@layout = params.dig(:layout).try :to_sym
# TODO: Aplicar policy_scope
@posts = @site.posts(lang: I18n.locale)
@posts = @site.posts(lang: lang)
@posts.sort_by!(:order, :date).reverse!
@usuarie = @site.usuarie? current_usuarie
end
def show
@site = find_site
@post = @site.posts.find params[:id]
@post = @site.posts(lang: lang).find params[:id]
authorize @post
end
def new
authorize Post
@site = find_site
@post = @site.posts.build(lang: I18n.locale, layout: params[:layout])
@post = @site.posts.build(lang: lang, layout: params[:layout])
end
def create
@ -44,14 +44,14 @@ class PostsController < ApplicationController
def edit
@site = find_site
@post = @site.posts.find params[:id]
@post = @site.posts(lang: lang).find params[:id]
authorize @post
end
def update
@site = find_site
@post = @site.posts.find params[:id]
@post = @site.posts(lang: lang).find params[:id]
authorize @post
@ -70,7 +70,7 @@ class PostsController < ApplicationController
# Eliminar artículos
def destroy
@site = find_site
@post = @site.posts.find params[:id]
@post = @site.posts(lang: lang).find params[:id]
authorize @post
@ -96,4 +96,13 @@ class PostsController < ApplicationController
service.reorder
redirect_to site_posts_path(@site)
end
# 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
end

View file

@ -102,8 +102,19 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
end
end
# Devuelve el idioma solicitado a través de un parámetro, validando
# que el sitio soporte ese idioma
#
# TODO: DRY
def lang
params[:post][:lang] || I18n.locale
return unless params[:lang]
return unless site.try(:locales).try(:include?, params[:lang])
params[:lang]
end
def layout
params.dig(:post, :layout) || params[:layout]
end
end
# rubocop:enable Metrics/BlockLength