diff --git a/app/controllers/admin/site_customization/information_texts_controller.rb b/app/controllers/admin/site_customization/information_texts_controller.rb index 292c6e8be..f5908571f 100644 --- a/app/controllers/admin/site_customization/information_texts_controller.rb +++ b/app/controllers/admin/site_customization/information_texts_controller.rb @@ -13,7 +13,7 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz unless values.empty? values.each do |key, value| - locale = key.split("_").last + locale = key.split('_').last if value == t(content[:id], locale: locale) || value.match(/translation missing/) next @@ -44,6 +44,7 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz def delete_translations languages_to_delete = params[:enabled_translations].select { |_, v| v == '0' } .keys + languages_to_delete.each do |locale| I18nContentTranslation.destroy_all(locale: locale) end @@ -53,9 +54,9 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz @existing_keys = {} @tab = params[:tab] || :debates - I18nContent.begins_with_key(@tab) - .all - .map{ |content| @existing_keys[content.key] = content } + I18nContent.begins_with_key(@tab).map { |content| + @existing_keys[content.key] = content + } end def append_or_create_keys @@ -66,10 +67,10 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz translations = I18n.backend.send(:translations)[locale.to_sym] translations.each do |k, v| - @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}"] } + @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 diff --git a/app/models/i18n_content.rb b/app/models/i18n_content.rb index f0bd292de..c018cf4dc 100644 --- a/app/models/i18n_content.rb +++ b/app/models/i18n_content.rb @@ -1,4 +1,5 @@ class I18nContent < ActiveRecord::Base + scope :by_key, ->(key) { where(key: key) } scope :begins_with_key, ->(key) { where("key ILIKE ?", "#{key}%") } @@ -7,34 +8,41 @@ 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 + # flat_hash returns a flattened hash, a hash with a single level of + # depth in which each key is composed from the keys of the original + # hash (whose value is not a hash) by typing in the key of the route + # from the first level of the original hash # - # examples - # hash = {'key1' => 'value1', - # 'key2' => {'key3' => 'value2', - # 'key4' => {'key5' => 'value3'}}} + # Examples: # - # I18nContent.flat_hash(hash) = {"key1"=>"value1", - # "key2.key3"=>"value2", - # "key2.key4.key5"=>"value3"} + # hash = { + # 'key1' => 'value1', + # 'key2' => { 'key3' => 'value2', + # 'key4' => { 'key5' => 'value3' } } + # } # - # I18nContent.flat_hash(hash, 'string') = {"string.key1"=>"value1", - # "string.key2.key3"=>"value2", - # "string.key2.key4.key5"=>"value3"} + # I18nContent.flat_hash(hash) = { + # 'key1' => 'value1', + # 'key2.key3' => 'value2', + # '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"} + # 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) } + h.map { |k, r| flat_hash(r, [f, k].compact.join('.'), g) } return g end diff --git a/spec/models/i18n_content_spec.rb b/spec/models/i18n_content_spec.rb index cc6ea19b8..61f0d5c35 100644 --- a/spec/models/i18n_content_spec.rb +++ b/spec/models/i18n_content_spec.rb @@ -74,35 +74,83 @@ RSpec.describe I18nContent, type: :model do end end - context 'flat_hash' do + describe '#flat_hash' do + it 'uses one parameter' do + expect(I18nContent.flat_hash(nil)).to eq({ + nil => nil + }) - 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'}) + 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'}) + it 'uses the first two 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'}) + it 'uses 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 + it 'uses 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