Skip to content

Commit

Permalink
Merge pull request #350 from marshall-lee/fix_ignore_undeclared
Browse files Browse the repository at this point in the history
Stringified translations wasn't working in IgnoreUndeclared.
  • Loading branch information
dblock committed Feb 8, 2016
2 parents 6cfa234 + c4fc51d commit f63fc99
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions lib/hashie/extensions/ignore_undeclared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ module Extensions
# 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)
Expand Down
3 changes: 2 additions & 1 deletion spec/hashie/extensions/ignore_undeclared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ForgivingTrash < Hashie::Trash
include Hashie::Extensions::IgnoreUndeclared
property :city
property :state, from: :provence
property :str_state, from: 'str_provence'
end

subject { ForgivingTrash }
Expand All @@ -19,7 +20,7 @@ class ForgivingTrash < Hashie::Trash
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
Expand Down

0 comments on commit f63fc99

Please sign in to comment.