Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 2 commits into from

2 participants

Pedro Padron Daniel Doubrovkine (dB.) @dblockdotorg
Pedro Padron

Tested the latest updates to master with my app and ran into this issue. One of my entities contains a links attribute generated by a block, like this:

expose :links do |model, options|
  [
    { "whatever" => "/link?resource_id=#{model.id}" }
  ]
end

With #203 the :links attribute was not serialized.

Daniel Doubrovkine (dB.) @dblockdotorg dblock commented on the diff
spec/grape/entity_spec.rb
@@ -252,6 +252,9 @@
subject{ fresh_class.new(model) }
describe '#serializable_hash' do
+
+
+
Daniel Doubrovkine (dB.) @dblockdotorg Owner
dblock added a note

Space is cheap, but not that cheap :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Daniel Doubrovkine (dB.) @dblockdotorg
Owner

Thanks, merging.

Daniel Doubrovkine (dB.) @dblockdotorg dblock merged commit fd65b15 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 20, 2012
  1. Pedro Padron

    fixes a bug introduced by #203 where entities that expose attributes …

    ppadron authored
    …returned by a block (therefore absent from the object) would not be serialized
  2. Pedro Padron
This page is out of date. Refresh to see the latest.
1  CHANGELOG.markdown
View
@@ -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)
=================
2  lib/grape/entity.rb
View
@@ -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
11 spec/grape/entity_spec.rb
View
@@ -252,6 +252,9 @@
subject{ fresh_class.new(model) }
describe '#serializable_hash' do
+
+
+
Daniel Doubrovkine (dB.) @dblockdotorg Owner
dblock added a note

Space is cheap, but not that cheap :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
@@ -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
Something went wrong with that request. Please try again.