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

Properly serializing hashes #27

Merged
merged 2 commits into from
Oct 15, 2013
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Next Release
* [#18](https://github.com/intridea/grape-entity/pull/18): Add `safe` option to `expose`, will not raise error for a missing attribute - [@fixme](https://github.com/fixme).
* [#16](https://github.com/intridea/grape-entity/pull/16): Add `using` option to `expose SYMBOL BLOCK` - [@fahchen](https://github.com/fahchen).
* [#24](https://github.com/intridea/grape-entity/pull/24): Return documentation with `as` param considered - [@drakula2k](https://github.com/drakula2k).
* [#27](https://github.com/intridea/grape-entity/pull/27): Properly serializing hashes - [@clintonb](https://github.com/clintonb).
* Your contribution here.

0.3.0 (2013-03-29)
Expand Down
4 changes: 4 additions & 0 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ def serializable_hash(runtime_options = {})
partial_output.serializable_hash(runtime_options)
elsif partial_output.kind_of?(Array) && !partial_output.map {|o| o.respond_to? :serializable_hash}.include?(false)
partial_output.map {|o| o.serializable_hash}
elsif partial_output.kind_of?(Hash)
partial_output.each do |key, value|
partial_output[key] = value.serializable_hash if value.respond_to? :serializable_hash
end
else
partial_output
end
Expand Down
14 changes: 14 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ def serializable_hash(opts = {})
{ :abc => 'def' }
end
end
class EmbeddedExampleWithHash
def name
"abc"
end
def embedded
{ :a => nil, :b => EmbeddedExample.new }
end
end
class EmbeddedExampleWithMany
def name
"abc"
Expand Down Expand Up @@ -417,6 +425,12 @@ def embedded
presenter.serializable_hash.should == {:name => "abc", :embedded => [{:abc => "def"}, {:abc => "def"}]}
end

it 'serializes embedded hashes of objects which respond to #serializable_hash' do
fresh_class.expose :name, :embedded
presenter = fresh_class.new(EntitySpec::EmbeddedExampleWithHash.new)
presenter.serializable_hash.should == {:name => "abc", :embedded => {:a => nil, :b => {:abc => "def"}}}
end

end

end
Expand Down