Skip to content

Commit fd65b15

Browse files
committed
Merge pull request ruby-grape#208 from ppadron/master
Entity#serializable_hash must also check if attribute is generated by a user supplied block
2 parents 483a7a8 + 033e425 commit fd65b15

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Next Release
66
* [#181](https://github.com/intridea/grape/pull/181): Fix: Corrected JSON serialization of nested hashes containing `Grape::Entity` instances - [@benrosenblum](https://github.com/benrosenblum).
77
* [#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).
88
* [#204](https://github.com/intridea/grape/pull/204): Added ability to declare shared parameters at namespace level - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
9+
* [#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].
910

1011
0.2.1 (7/11/2012)
1112
=================

lib/grape/entity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def serializable_hash(runtime_options = {})
259259
return nil if object.nil?
260260
opts = options.merge(runtime_options || {})
261261
exposures.inject({}) do |output, (attribute, exposure_options)|
262-
if object.respond_to?(attribute) && conditions_met?(exposure_options, opts)
262+
if exposure_options.has_key?(:proc) || object.respond_to?(attribute) && conditions_met?(exposure_options, opts)
263263
partial_output = value_for(attribute, opts)
264264
output[key_for(attribute)] =
265265
if partial_output.respond_to? :serializable_hash

spec/grape/entity_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@
252252
subject{ fresh_class.new(model) }
253253

254254
describe '#serializable_hash' do
255+
256+
257+
255258
it 'should not throw an exception if a nil options object is passed' do
256259
expect{ fresh_class.new(model).serializable_hash(nil) }.not_to raise_error
257260
end
@@ -285,6 +288,14 @@
285288
res.should_not have_key :non_existant_attribute
286289
res.should_not have_key :non_existant_attribute2
287290
end
291+
292+
it "should expose attributes that don't exist on the object only when they are generated by a block" do
293+
fresh_class.expose :non_existant_attribute do |model, options|
294+
"well, I do exist after all"
295+
end
296+
res = fresh_class.new(model).serializable_hash
297+
res.should have_key :non_existant_attribute
298+
end
288299

289300
context "#serializable_hash" do
290301

0 commit comments

Comments
 (0)