Skip to content

Commit

Permalink
Merge pull request #208 from ppadron/master
Browse files Browse the repository at this point in the history
Entity#serializable_hash must also check if attribute is generated by a user supplied block
  • Loading branch information
dblock committed Jul 21, 2012
2 parents 483a7a8 + 033e425 commit fd65b15
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
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



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

0 comments on commit fd65b15

Please sign in to comment.