Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Settable: allow setting a nested value to a non-string (for 6.4 stable) #4510

Merged
merged 2 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/mongoid/persistable/settable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def set(setters)

field_and_value_hash = hasherizer(field.split('.'), value)
field = field_and_value_hash.keys.first.to_s
value = field_and_value_hash[field]

if fields[field] && fields[field].type == Hash && attributes.key?(field) && !value.empty?
if fields[field] && fields[field].type == Hash && attributes.key?(field) && Hash === value && !value.empty?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think respond_to?(:empty?) would make more sense here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think we specifically want to check for a Hash... for example we don't want to enter this block for a String, even though String implements empty?.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK that makes sense. Is this behavior documented somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. The behavior in this PR seems to make sense: only merge an existing Hash with a new value if that value is a non-empty Hash. The PR that introduced the regression intended to support setting field values to an empty Hash, which remains supported.

merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
value = (attributes[field] || {}).merge(field_and_value_hash[field], &merger)
process_attribute(field.to_s, value)
else
process_attribute(field.to_s, field_and_value_hash[field])
value = (attributes[field] || {}).merge(value, &merger)
end

process_attribute(field.to_s, value)

unless relations.include?(field.to_s)
ops[atomic_attribute_name(field)] = attributes[field]
end
Expand Down
19 changes: 19 additions & 0 deletions spec/mongoid/persistable/settable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,25 @@
end
end

context 'when a leaf value in the nested hash is updated to a number' do

let(:church) do
Church.new.tap do |a|
a.location = {'address' => {'city' => 'Berlin', 'street' => 'Yorckstr'}}
a.name = 'Church1'
a.save
end
end

before do
church.set('location.address.city' => 12345)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding correctly, this is where the exception would have occurred before the change? If so, let's put this inside a let block instead and invoke inside the it block below so that any future regression occurs in a place that's more easily identified.

Copy link
Contributor Author

@mdehoog mdehoog Jul 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do, but this way it's more consistent with all the examples around it. I think an exception raised anywhere in the spec is fine for failing a test; it doesn't necessarily need to be raised in the it block. However, happy to change it if this blocks merging.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough; I don't feel strongly enough to block merging on this, so it's fine to leave as-is.

end

it 'updates the nested value to the correct value' do
expect(church.name).to eq('Church1')
expect(church.location).to eql({'address' => {'city' => 12345, 'street' => 'Yorckstr'}})
end
end

context 'when the nested hash is many levels deep' do

Expand Down