diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/extensions/ignore_undeclared.rb | 7 | ||||
-rw-r--r-- | spec/hashie/extensions/ignore_undeclared_spec.rb | 3 |
3 files changed, 8 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 468ca54..f063809 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ scheme are considered to be bugs. * [#319](https://github.com/intridea/hashie/pull/319): Fix a regression from 3.4.1 where `Hashie::Extensions::DeepFind` is no longer indifference-aware - [@michaelherold](https://github.com/michaelherold). * [#322](https://github.com/intridea/hashie/pull/322): Fixed `reverse_merge` issue with `Mash` subclasses - [@marshall-lee](https://github.com/marshall-lee). * [#346](https://github.com/intridea/hashie/pull/346): Fixed `merge` breaking indifferent access - [@docwhat](https://github.com/docwhat), [@michaelherold](https://github.com/michaelherold). +* [#350](https://github.com/intridea/hashie/pull/350): Fixed from string translations used with `IgnoreUndeclared` - [@marshall-lee](https://github.com/marshall-lee). ### Security diff --git a/lib/hashie/extensions/ignore_undeclared.rb b/lib/hashie/extensions/ignore_undeclared.rb index e16e0a4..9b506dd 100644 --- a/lib/hashie/extensions/ignore_undeclared.rb +++ b/lib/hashie/extensions/ignore_undeclared.rb @@ -30,10 +30,13 @@ module Hashie # p.email # => NoMethodError module IgnoreUndeclared def initialize_attributes(attributes) + return unless attributes + klass = self.class + translations = klass.respond_to?(:translations) && klass.translations attributes.each_pair do |att, value| - next unless self.class.property?(att) || (self.class.respond_to?(:translations) && self.class.translations.include?(att.to_sym)) + next unless klass.property?(att) || (translations && translations.include?(att)) self[att] = value - end if attributes + end end def property_exists?(property) diff --git a/spec/hashie/extensions/ignore_undeclared_spec.rb b/spec/hashie/extensions/ignore_undeclared_spec.rb index f207aca..948a834 100644 --- a/spec/hashie/extensions/ignore_undeclared_spec.rb +++ b/spec/hashie/extensions/ignore_undeclared_spec.rb @@ -6,6 +6,7 @@ describe Hashie::Extensions::IgnoreUndeclared do include Hashie::Extensions::IgnoreUndeclared property :city property :state, from: :provence + property :str_state, from: 'str_provence' end subject { ForgivingTrash } @@ -19,7 +20,7 @@ describe Hashie::Extensions::IgnoreUndeclared do end it 'works with translated properties (with string keys)' do - expect(subject.new(provence: 'Ontario').state).to eq('Ontario') + expect(subject.new('str_provence' => 'Ontario').str_state).to eq('Ontario') end it 'requires properties to be declared on assignment' do |