diff --git a/app/controllers/admin/site_customization/information_texts_controller.rb b/app/controllers/admin/site_customization/information_texts_controller.rb index 32483d5ed..292c6e8be 100644 --- a/app/controllers/admin/site_customization/information_texts_controller.rb +++ b/app/controllers/admin/site_customization/information_texts_controller.rb @@ -66,17 +66,11 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz translations = I18n.backend.send(:translations)[locale.to_sym] translations.each do |k, v| - @content[k.to_s] = flat_hash(v).keys - .map { |s| @existing_keys["#{k.to_s}.#{s}"].nil? ? - I18nContent.new(key: "#{k.to_s}.#{s}") : - @existing_keys["#{k.to_s}.#{s}"] } + @content[k.to_s] = I18nContent.flat_hash(v).keys + .map { |s| @existing_keys["#{k.to_s}.#{s}"].nil? ? + I18nContent.new(key: "#{k.to_s}.#{s}") : + @existing_keys["#{k.to_s}.#{s}"] } end end - def flat_hash(h, f = nil, g = {}) - return g.update({ f => h }) unless h.is_a? Hash - h.each { |k, r| flat_hash(r, [f,k].compact.join('.'), g) } - return g - end - end diff --git a/app/models/i18n_content.rb b/app/models/i18n_content.rb index d4ff2cf1e..f0bd292de 100644 --- a/app/models/i18n_content.rb +++ b/app/models/i18n_content.rb @@ -7,4 +7,35 @@ class I18nContent < ActiveRecord::Base translates :value, touch: true globalize_accessors locales: [:en, :es, :fr, :nl] + # flat_hash function returns a flattened hash, a hash of a single + # level of profundity in which each key is composed from the + # keys of the original hash (whose value is not a hash) by + # typing in the key the route from the first level of the + # original hash + # + # examples + # hash = {'key1' => 'value1', + # 'key2' => {'key3' => 'value2', + # 'key4' => {'key5' => 'value3'}}} + # + # I18nContent.flat_hash(hash) = {"key1"=>"value1", + # "key2.key3"=>"value2", + # "key2.key4.key5"=>"value3"} + # + # I18nContent.flat_hash(hash, 'string') = {"string.key1"=>"value1", + # "string.key2.key3"=>"value2", + # "string.key2.key4.key5"=>"value3"} + # + # I18nContent.flat_hash(hash, 'string', {'key6' => "value4"}) = + # {'key6' => "value4", + # "string.key1"=>"value1", + # "string.key2.key3"=>"value2", + # "string.key2.key4.key5"=>"value3"} + + def self.flat_hash(h, f = nil, g = {}) + return g.update({ f => h }) unless h.is_a? Hash + h.each { |k, r| flat_hash(r, [f,k].compact.join('.'), g) } + return g + end + end diff --git a/spec/models/i18n_content_spec.rb b/spec/models/i18n_content_spec.rb index 9b71f4b20..cc6ea19b8 100644 --- a/spec/models/i18n_content_spec.rb +++ b/spec/models/i18n_content_spec.rb @@ -73,4 +73,36 @@ RSpec.describe I18nContent, type: :model do expect(i18n_content.value).to eq('Texto en espaƱol') end end + + context 'flat_hash' do + + it 'using one parameter' do + expect(I18nContent.flat_hash(nil)).to eq({nil=>nil}) + expect(I18nContent.flat_hash('string')).to eq({nil=>'string'}) + expect(I18nContent.flat_hash({w: 'string'})).to eq({"w" => 'string'}) + expect(I18nContent.flat_hash({w: {p: 'string'}})).to eq({"w.p" => 'string'}) + end + + it 'using the two first parameters' do + expect(I18nContent.flat_hash('string', 'f')).to eq({'f'=>'string'}) + expect(I18nContent.flat_hash(nil, 'f')).to eq({"f" => nil}) + expect(I18nContent.flat_hash({w: 'string'}, 'f')).to eq({"f.w" => 'string'}) + expect(I18nContent.flat_hash({w: {p: 'string'}}, 'f')).to eq({"f.w.p" => 'string'}) + end + + it 'using the first and last parameters' do + expect {I18nContent.flat_hash('string', nil, 'not hash')}.to raise_error NoMethodError + expect(I18nContent.flat_hash(nil, nil, {q: 'other string'})).to eq({q: 'other string', nil => nil}) + expect(I18nContent.flat_hash({w: 'string'}, nil, {q: 'other string'})).to eq({q: 'other string', "w" => 'string'}) + expect(I18nContent.flat_hash({w: {p: 'string'}}, nil, {q: 'other string'})).to eq({q: 'other string', "w.p" => 'string'}) + end + + it 'using all parameters' do + expect {I18nContent.flat_hash('string', 'f', 'not hash')}.to raise_error NoMethodError + expect(I18nContent.flat_hash(nil, 'f', {q: 'other string'})).to eq({q: 'other string', "f" => nil}) + expect(I18nContent.flat_hash({w: 'string'}, 'f', {q: 'other string'})).to eq({q: 'other string', "f.w" => 'string'}) + expect(I18nContent.flat_hash({w: {p: 'string'}}, 'f', {q: 'other string'})).to eq({q: 'other string', "f.w.p" => 'string'}) + end + + end end