From 26247a95440620ac897a26a88367f38791978511 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 19 Mar 2020 15:31:29 -0300 Subject: [PATCH] permitir cambiar de idioma --- app/controllers/posts_controller.rb | 21 +++++++++++++++------ app/services/post_service.rb | 13 ++++++++++++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 4d999d9..fbe9036 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 diff --git a/app/services/post_service.rb b/app/services/post_service.rb index 125d4ee..4dfdba0 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -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