fix issues locomotivecms/wagon#285 locomotivecms/coal#15
did
committed Dec 09, 2015
commit 3b8a163f115d0c98f238eed5b8fb1f87270cabe5
Showing 3
changed files with
16 additions
and 3 deletions
locomotive/steam/entities/content_entry.rb b/lib/locomotive/steam/entities/content_entry.rb
+1
-2
| @@ | @@ -156,8 +156,7 @@ module Locomotive::Steam |
| def _cast_convertor(name, &block) | |
| if (value = attributes[name]).respond_to?(:translations) | |
| - | value.each { |l, _value| value[l] = yield(_value) } |
| - | value |
| + | value.apply(&block) |
| else | |
| yield(value) | |
| end | |
locomotive/steam/models/i18n_field.rb b/lib/locomotive/steam/models/i18n_field.rb
+10
-1
| @@ | @@ -5,7 +5,7 @@ module Locomotive::Steam |
| extend Forwardable | |
| - | def_delegators :@translations, :values, :blank? |
| + | def_delegators :@translations, :values, :blank?, :default |
| attr_reader :name, :translations | |
| @@ | @@ -39,6 +39,15 @@ module Locomotive::Steam |
| @translations.each(&block) | |
| end | |
| + | def apply(&block) |
| + | if default |
| + | @translations = Hash.new(yield(default)) |
| + | else |
| + | each { |l, _value| self[l] = yield(_value) } |
| + | end |
| + | self |
| + | end |
| + | |
| alias :__translations__ :translations | |
| alias :to_hash :translations | |
spec/unit/entities/content_entry_spec.rb
+5
-0
| @@ | @@ -146,6 +146,11 @@ describe Locomotive::Steam::ContentEntry do |
| context 'localized' do | |
| let(:value) { build_i18n_field(en: '2007/06/29', fr: date) } | |
| it { expect(subject.translations).to eq('en' => date, 'fr' => date) } | |
| + | context 'with a single value for all the translations' do |
| + | let(:value) { build_i18n_field('2007/06/29') } |
| + | it { expect(subject[:fr]).to eq(date) } |
| + | it { expect(subject[:en]).to eq(date) } |
| + | end |
| end | |
| end | |