Skip to content

Commit 1742bbc

Browse files
committed
Merge pull request ruby-grape#249 from petergoldstein/feature/add_options_for_child_representations
Pass options when using child representations in Grape::Entity
2 parents 6801de0 + 4df1767 commit 1742bbc

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

lib/grape/entity.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ def value_for(attribute, options = {})
342342
if exposure_options[:proc]
343343
exposure_options[:proc].call(object, options)
344344
elsif exposure_options[:using]
345-
exposure_options[:using].represent(object.send(attribute), :root => nil)
345+
using_options = options.dup
346+
using_options.delete(:collection)
347+
using_options[:root] = nil
348+
exposure_options[:using].represent(object.send(attribute), using_options)
346349
elsif exposure_options[:format_with]
347350
format_with = exposure_options[:format_with]
348351

spec/grape/entity_spec.rb

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -375,24 +375,73 @@ def timestamp(date)
375375
rep.last.serializable_hash[:name].should == 'Friend 2'
376376
end
377377

378-
it 'should disable root key name for child representations' do
379-
380-
module EntitySpec
381-
class FriendEntity < Grape::Entity
382-
root 'friends', 'friend'
383-
expose :name, :email
384-
end
385-
end
378+
context 'child representations' do
379+
it 'should disable root key name for child representations' do
386380

387-
fresh_class.class_eval do
388-
expose :friends, :using => EntitySpec::FriendEntity
381+
module EntitySpec
382+
class FriendEntity < Grape::Entity
383+
root 'friends', 'friend'
384+
expose :name, :email
385+
end
386+
end
387+
388+
fresh_class.class_eval do
389+
expose :friends, :using => EntitySpec::FriendEntity
390+
end
391+
392+
rep = subject.send(:value_for, :friends)
393+
rep.should be_kind_of(Array)
394+
rep.reject{|r| r.is_a?(EntitySpec::FriendEntity)}.should be_empty
395+
rep.first.serializable_hash[:name].should == 'Friend 1'
396+
rep.last.serializable_hash[:name].should == 'Friend 2'
397+
end
398+
399+
it 'should pass through custom options' do
400+
module EntitySpec
401+
class FriendEntity < Grape::Entity
402+
root 'friends', 'friend'
403+
expose :name
404+
expose :email, :if => { :user_type => :admin }
405+
end
406+
end
407+
408+
fresh_class.class_eval do
409+
expose :friends, :using => EntitySpec::FriendEntity
410+
end
411+
412+
rep = subject.send(:value_for, :friends)
413+
rep.should be_kind_of(Array)
414+
rep.reject{|r| r.is_a?(EntitySpec::FriendEntity)}.should be_empty
415+
rep.first.serializable_hash[:email].should be_nil
416+
rep.last.serializable_hash[:email].should be_nil
417+
418+
rep = subject.send(:value_for, :friends, { :user_type => :admin })
419+
rep.should be_kind_of(Array)
420+
rep.reject{|r| r.is_a?(EntitySpec::FriendEntity)}.should be_empty
421+
rep.first.serializable_hash[:email].should == '[email protected]'
422+
rep.last.serializable_hash[:email].should == '[email protected]'
423+
end
424+
425+
it 'should ignore the :collection parameter in the source options' do
426+
module EntitySpec
427+
class FriendEntity < Grape::Entity
428+
root 'friends', 'friend'
429+
expose :name
430+
expose :email, :if => { :collection => true }
431+
end
432+
end
433+
434+
fresh_class.class_eval do
435+
expose :friends, :using => EntitySpec::FriendEntity
436+
end
437+
438+
rep = subject.send(:value_for, :friends, { :collection => false })
439+
rep.should be_kind_of(Array)
440+
rep.reject{|r| r.is_a?(EntitySpec::FriendEntity)}.should be_empty
441+
rep.first.serializable_hash[:email].should == '[email protected]'
442+
rep.last.serializable_hash[:email].should == '[email protected]'
389443
end
390-
391-
rep = subject.send(:value_for, :friends)
392-
rep.should be_kind_of(Array)
393-
rep.reject{|r| r.is_a?(EntitySpec::FriendEntity)}.should be_empty
394-
rep.first.serializable_hash[:name].should == 'Friend 1'
395-
rep.last.serializable_hash[:name].should == 'Friend 2'
444+
396445
end
397446

398447
it 'should call through to the proc if there is one' do

0 commit comments

Comments
 (0)