Move flat_hash to I18nContent model

also add unit tests for this function and a description
into the model of the behaviour of the function
This commit is contained in:
Raúl Fuentes
2018-08-09 11:15:21 +02:00
committed by Javi Martín
parent 8bba09aac3
commit bdf12ebdfb
3 changed files with 67 additions and 10 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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