From c006ed7f29d1ea304e8844bd97c2bd2402d619c1 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 3 Mar 2021 09:43:15 -0300 Subject: [PATCH] garantizar que todos los locales son simbolos --- app/controllers/posts_controller.rb | 4 ++-- app/helpers/application_helper.rb | 7 +------ app/models/metadata_lang.rb | 2 +- app/models/metadata_locales.rb | 6 +++--- app/models/metadata_predefined_array.rb | 4 ++-- app/models/metadata_template.rb | 4 ++-- app/models/post.rb | 10 +++++----- app/models/site.rb | 6 +++--- app/services/post_service.rb | 2 +- app/services/site_service.rb | 10 ++++------ app/views/posts/attributes/_locales.haml | 6 +++--- 11 files changed, 27 insertions(+), 34 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index d60879d..6f84c91 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -159,8 +159,8 @@ 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.to_s }) do |l| - l == params[:locale] + @site&.locales&.find(-> { I18n.locale }) do |l| + l.to_s == params[:locale] end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8bb2d3b..f177211 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -117,16 +117,11 @@ module ApplicationHelper "#{attribute.join('-')}-datalist" end - def local_channel - "sutty-#{I18n.locale}" - end - private def post_t(*attribute, post:, type:) post.layout.metadata.dig(*attribute, type.to_s, I18n.locale.to_s) || - post.layout.metadata.dig(*attribute, - type.to_s, I18n.default_locale.to_s) || + post.layout.metadata.dig(*attribute, type.to_s, I18n.default_locale.to_s) || I18n.t("posts.attributes.#{attribute.join('.')}.#{type}") end end diff --git a/app/models/metadata_lang.rb b/app/models/metadata_lang.rb index 93ef87c..5f31ee9 100644 --- a/app/models/metadata_lang.rb +++ b/app/models/metadata_lang.rb @@ -7,7 +7,7 @@ class MetadataLang < MetadataTemplate end def value_from_document - document.collection.label + document.collection.label.to_sym end def value diff --git a/app/models/metadata_locales.rb b/app/models/metadata_locales.rb index 4f0d4f8..4d540ef 100644 --- a/app/models/metadata_locales.rb +++ b/app/models/metadata_locales.rb @@ -14,9 +14,9 @@ class MetadataLocales < MetadataTemplate # @return { lang: { title: uuid } } def values @values ||= site.locales.map do |locale| - [locale.to_sym, site.posts(lang: locale).map do |post| - [post.title.value, post.uuid.value] - end.to_h] + [locale, site.posts(lang: locale).map do |post| + [post.title.value, post.uuid.value] + end.to_h] end.to_h end end diff --git a/app/models/metadata_predefined_array.rb b/app/models/metadata_predefined_array.rb index 829a8a2..b8e5050 100644 --- a/app/models/metadata_predefined_array.rb +++ b/app/models/metadata_predefined_array.rb @@ -3,8 +3,8 @@ # Una lista de valores predefinidos class MetadataPredefinedArray < MetadataArray def values - @values ||= layout[:metadata][name]['values'].map do |k, v| + @values ||= layout.dig(:metadata, name, 'values')&.map do |k, v| [v[I18n.locale.to_s], k] - end.to_h + end&.to_h end end diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index 60b879a..44bb569 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -57,12 +57,12 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, # Trae el idioma actual del sitio o del panel # @return [String] def lang - @lang ||= post&.lang&.value || I18n.locale.to_s + @lang ||= post&.lang&.value || I18n.locale end # El valor por defecto def default_value - layout.metadata.dig(name, 'default', lang) + layout.metadata.dig(name, 'default', lang.to_s) end # Valores posibles, busca todos los valores actuales en otros diff --git a/app/models/post.rb b/app/models/post.rb index 68bee86..3b25c1f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -69,16 +69,16 @@ class Post # date_local # # TODO: Cambiar el locale en otro lado - site.jekyll.config['lang'] = lang.value - site.jekyll.config['locale'] = lang.value + l = lang.value.to_s + site.jekyll.config['locale'] = site.jekyll.config['lang'] = l # Payload básico con traducciones. document.renderer.payload = { 'site' => { 'data' => site.data, - 'i18n' => site.data[lang.value], - 'lang' => lang.value, - 'locale' => lang.value + 'i18n' => site.data[l], + 'lang' => l, + 'locale' => l }, 'page' => document.to_liquid } diff --git a/app/models/site.rb b/app/models/site.rb index 4ec0b9f..fe64f57 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -161,7 +161,7 @@ class Site < ApplicationRecord # incorporadas a los sitios de Sutty, aunque les usuaries no traduzcan # sus sitios. def locales - config.fetch('locales', I18n.available_locales.map(&:to_s)) + @locales ||= config.fetch('locales', I18n.available_locales).map(&:to_sym) end # Similar a site.i18n en jekyll-locales @@ -230,7 +230,7 @@ class Site < ApplicationRecord # Crea un Struct dinámico con los valores de los locales, si # llegamos a pasar un idioma que no existe vamos a tener una # excepción NoMethodError - @posts ||= Struct.new(*locales.map(&:to_sym)).new + @posts ||= Struct.new(*locales).new return @posts[lang] unless @posts[lang].blank? @@ -372,7 +372,7 @@ class Site < ApplicationRecord # Si estamos usando nuestro propio plugin de i18n, los posts están # en "colecciones" - locales.each do |i| + locales.map(&:to_s).each do |i| @configuration['collections'][i] = {} end diff --git a/app/services/post_service.rb b/app/services/post_service.rb index bc8def1..e448bb4 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -103,7 +103,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do end def locale - params.dig(:post, :lang) || I18n.locale + params.dig(:post, :lang)&.to_sym || I18n.locale end def layout diff --git a/app/services/site_service.rb b/app/services/site_service.rb index 24e5bc0..4f3905a 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -2,7 +2,6 @@ # Se encargar de guardar cambios en sitios # TODO: Implementar rollback en la configuración -# rubocop:disable Metrics/BlockLength SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do # Crea un sitio, agrega un rol nuevo y guarda los cambios a la # configuración en el repositorio git @@ -11,7 +10,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do add_role temporal: false, rol: 'usuarie' - I18n.with_locale(usuarie&.lang || I18n.default_locale) do + I18n.with_locale(usuarie&.lang&.to_sym || I18n.default_locale) do site.save && site.config.write && commit_config(action: :create) @@ -24,7 +23,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do # Actualiza el sitio y guarda los cambios en la configuración def update - I18n.with_locale(usuarie&.lang || I18n.default_locale) do + I18n.with_locale(usuarie&.lang&.to_sym || I18n.default_locale) do site.update(params) && site.config.write && commit_config(action: :update) @@ -80,7 +79,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do # Crea la licencia del sitio para cada locale disponible en el sitio def add_licencias - site.locales.map(&:to_sym).each do |locale| + site.locales.each do |locale| next unless I18n.available_locales.include? locale Mobility.with_locale(locale) do @@ -110,7 +109,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do # TODO: Crear un layout específico para licencias así es más certera # la búsqueda. def change_licencias - site.locales.map(&:to_sym).each do |locale| + site.locales.each do |locale| Mobility.with_locale(locale) do permalink = "#{I18n.t('activerecord.models.licencia').downcase}/" post = site.posts(lang: locale).find_by(permalink: permalink) @@ -132,4 +131,3 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do params: params).update end end -# rubocop:enable Metrics/BlockLength diff --git a/app/views/posts/attributes/_locales.haml b/app/views/posts/attributes/_locales.haml index 6a6f063..8dd7adf 100644 --- a/app/views/posts/attributes/_locales.haml +++ b/app/views/posts/attributes/_locales.haml @@ -10,14 +10,14 @@ - next if post.lang.value == locale - locale_t = t("locales.#{locale}.name") - values = metadata.value.select do |x| - - metadata.values[locale.to_sym].values.include? x + - metadata.values[locale].values.include? x .form-group = label_tag "#{base}_#{attribute}_#{locale}", locale_t .mapable{ dir: t("locales.#{locale}.dir"), lang: locale, data: { values: values.to_json, - 'default-values': metadata.values[locale.to_sym].to_json, + 'default-values': metadata.values[locale].to_json, name: "#{base}[#{attribute}][]", list: id_for_datalist(attribute, locale), button: t('posts.attributes.add'), @@ -35,5 +35,5 @@ metadata: metadata %datalist{ id: id_for_datalist(attribute, locale) } - - metadata.values[locale.to_sym].keys.each do |value| + - metadata.values[locale].keys.each do |value| %option{ value: value }