Skip to content

Commit

Permalink
Merge pull request mongodb#3890 from Shwetakale/set_feature
Browse files Browse the repository at this point in the history
refs #3889 #3888 checks added for :set for dynamic attributes and error
on non-existing field.
  • Loading branch information
arthurnn committed Jan 31, 2015
2 parents 06faf7c + d7d8459 commit 35aea60
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
For instructions on upgrading to newer versions, visit
[mongoid.org](http://mongoid.org/en/mongoid/docs/upgrading.html).

## 4.0.2 - Not released

### Resolved Issues

* \#3888 raise UnknownAttributeError when 'set' is called on non existing field and Mongoid::Attributes::Dynamic is not included in model. (Shweta Kale)

* \#3889 'set' will allow to set value of non existing field when Mongoid::Attributes::Dynamic is included in model. (Shweta Kale)

## 4.0.1

### Resolved Issues
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/persistable/settable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Settable
def set(setters)
prepare_atomic_operation do |ops|
process_atomic_operations(setters) do |field, value|
send("#{field}=", value)
process_attribute(field.to_s, value)
ops[atomic_attribute_name(field)] = attributes[field]
end
{ "$set" => ops }
Expand Down
23 changes: 23 additions & 0 deletions spec/mongoid/persistable/settable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,27 @@
end
end
end

context "when dynamic attributes are not enabled" do
let(:account) do
Account.create
end

it "raises exception for an unknown attribute " do
expect {
account.set(somethingnew: "somethingnew")
}.to raise_error(Mongoid::Errors::UnknownAttribute)
end
end

context "when dynamic attributes enabled" do
let(:person) do
Person.create
end

it "updates non existing attribute" do
person.set(somethingnew: "somethingnew")
expect(person.reload.somethingnew).to eq "somethingnew"
end
end
end

0 comments on commit 35aea60

Please sign in to comment.