Skip to content

Commit

Permalink
Ensure nested documents multiple levels are included in json. [ fix #…
Browse files Browse the repository at this point in the history
…2187 ]
  • Loading branch information
durran committed Jul 11, 2012
1 parent 5ca798f commit 34724ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,8 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#2187 Ensure all levels of nested documents are serialized in json.

* \#2184 Allow names of relations that conflict with ruby core kernel
methods to pass existence checks.

Expand Down
51 changes: 8 additions & 43 deletions lib/mongoid/serialization.rb
Expand Up @@ -31,56 +31,21 @@ def serializable_hash(options = nil)

names = field_names(options)

_serializing do
method_names = Array.wrap(options[:methods]).map do |name|
name.to_s if respond_to?(name)
end.compact
method_names = Array.wrap(options[:methods]).map do |name|
name.to_s if respond_to?(name)
end.compact

(names + method_names).each do |name|
without_autobuild do
serialize_attribute(attrs, name, names, options)
end
(names + method_names).each do |name|
without_autobuild do
serialize_attribute(attrs, name, names, options)
end
serialize_relations(attrs, options) if options[:include]
end
serialize_relations(attrs, options) if options[:include]
attrs
end

private

# Enter the serialization block.
#
# @api private
#
# @example Begin serialization.
# document._serializing do
# end
#
# @return [ Object ] The result of the yield.
#
# @since 3.0.0
def _serializing
Threaded.begin("serialization")
yield
ensure
Threaded.exit("serialization")
end

# Are we in a serialization block? We use this to protect multiple
# unnecessary calls to #as_document.
#
# @api private
#
# @example Are we in serialization?
# document._serializing?
#
# @return [ true, false ] If we are serializing.
#
# @since 3.0.0
def _serializing?
Threaded.executing?("serialization")
end

# Get the names of all fields that will be serialized.
#
# @api private
Expand All @@ -92,7 +57,7 @@ def _serializing?
#
# @since 3.0.0
def field_names(options)
names = (_serializing? ? attribute_names : as_document.keys + attribute_names).uniq.sort
names = (as_document.keys + attribute_names).uniq.sort

only = Array.wrap(options[:only]).map(&:to_s)
except = Array.wrap(options[:except]).map(&:to_s)
Expand Down
15 changes: 15 additions & 0 deletions spec/mongoid/serialization_spec.rb
Expand Up @@ -84,6 +84,21 @@
address_attributes["id"].should eq(address.id)
end
end

context "when nested multiple levels" do

let!(:location) do
address.locations.build(name: "home")
end

let(:attributes) do
person.serializable_hash
end

it "includes the deeply nested document" do
attributes["addresses"][0]["locations"].should_not be_empty
end
end
end

context "when the model has attributes that need conversion" do
Expand Down

0 comments on commit 34724ba

Please sign in to comment.