Skip to content

Commit

Permalink
Allow serialization of dynamic conflicting attributes. Fixes #2023.
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed May 18, 2012
1 parent c6b527c commit de2139f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#2023 Allow serilialization of dynamic types that conflict with core
Ruby methods to still be serialized.

* \#2008 Presence validation should hit the db to check validity if the
relation in memory is blank.

Expand Down
6 changes: 4 additions & 2 deletions lib/mongoid/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ def serializable_hash(options = nil)

{}.tap do |attrs|
(attribute_names + method_names).each do |name|
value = send(name)
if relations.has_key?(name)
value = send(name)
attrs[name] = value.serializable_hash(options)
elsif attribute_names.include?(name.to_s)
attrs[name] = read_attribute(name)
else
attrs[name] = value
attrs[name] = send(name)
end
end
serialize_relations(attrs, options) if options[:include]
Expand Down
4 changes: 2 additions & 2 deletions spec/functional/mongoid/serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
context "when a dynamic attribute has the same name as a ruby method" do

before do
person[:loop] = true
person[:loop] = "testing"
end

let(:attributes) do
person.serializable_hash
end

it "grabs the attribute direct from the hash" do
attributes["loop"].should be_true
attributes["loop"].should eq("testing")
end
end

Expand Down

0 comments on commit de2139f

Please sign in to comment.