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

Entity#serializable_hash must also check if attribute is generated by a user supplied block #208

Merged
merged 2 commits into from
Jul 21, 2012
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.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Next Release
* [#181](https://github.com/intridea/grape/pull/181): Fix: Corrected JSON serialization of nested hashes containing `Grape::Entity` instances - [@benrosenblum](https://github.com/benrosenblum).
* [#203](https://github.com/intridea/grape/pull/203): Added a check to `Entity#serializable_hash` that verifies an entity exists on an object - [@adamgotterer](https://github.com/adamgotterer).
* [#204](https://github.com/intridea/grape/pull/204): Added ability to declare shared parameters at namespace level - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
* [#208](https://github.com/intridea/grape/pull/208): `Entity#serializable_hash` must also check if attribute is generated by a user supplied block - [@ppadron][https://github.com/ppadron].

0.2.1 (7/11/2012)
=================
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def serializable_hash(runtime_options = {})
return nil if object.nil?
opts = options.merge(runtime_options || {})
exposures.inject({}) do |output, (attribute, exposure_options)|
if object.respond_to?(attribute) && conditions_met?(exposure_options, opts)
if exposure_options.has_key?(:proc) || object.respond_to?(attribute) && conditions_met?(exposure_options, opts)
partial_output = value_for(attribute, opts)
output[key_for(attribute)] =
if partial_output.respond_to? :serializable_hash
Expand Down
11 changes: 11 additions & 0 deletions spec/grape/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@
subject{ fresh_class.new(model) }

describe '#serializable_hash' do



Copy link
Member

Choose a reason for hiding this comment

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

Space is cheap, but not that cheap :)

it 'should not throw an exception if a nil options object is passed' do
expect{ fresh_class.new(model).serializable_hash(nil) }.not_to raise_error
end
Expand Down Expand Up @@ -285,6 +288,14 @@
res.should_not have_key :non_existant_attribute
res.should_not have_key :non_existant_attribute2
end

it "should expose attributes that don't exist on the object only when they are generated by a block" do
fresh_class.expose :non_existant_attribute do |model, options|
"well, I do exist after all"
end
res = fresh_class.new(model).serializable_hash
res.should have_key :non_existant_attribute
end

context "#serializable_hash" do

Expand Down