Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: springyweb/grape
...
head fork: springyweb/grape
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Dec 14, 2011
@evansj evansj Allow entity root key to be overridden
When calling represent, pass the :root => 'name'
option to use 'name' as the root key. Pass
:root => nil (or false) to represent with no root
key.
cb1db4f
@evansj evansj Always disable root element key for child entities
If you expose :children, :using => ChildEntity,
child entities will not be wrapped in a Hash with
a root key, even if this is configured as a default
for the ChildEntity class.
f951738
Showing with 42 additions and 2 deletions.
  1. +10 −2 lib/grape/entity.rb
  2. +32 −0 spec/grape/entity_spec.rb
View
12 lib/grape/entity.rb
@@ -138,6 +138,10 @@ def self.root(plural, singular=nil)
# @param objects [Object or Array] One or more objects to be represented.
# @param options [Hash] Options that will be passed through to each entity
# representation.
+ #
+ # @option options :root [String] override the default root name set for the
+ #  entity. Pass nil or false to represent the object or objects with no
+ # root name even if one is defined for the entity.
def self.represent(objects, options = {})
inner = if objects.is_a?(Array)
objects.map{|o| self.new(o, {:collection => true}.merge(options))}
@@ -145,7 +149,11 @@ def self.represent(objects, options = {})
self.new(objects, options)
end
- root_element = objects.is_a?(Array) ? @collection_root : @root
+ root_element = if options.has_key?(:root)
+ options[:root]
+ else
+ objects.is_a?(Array) ? @collection_root : @root
+ end
root_element ? { root_element => inner } : inner
end
@@ -187,7 +195,7 @@ def value_for(attribute, options = {})
if exposure_options[:proc]
exposure_options[:proc].call(object, options)
elsif exposure_options[:using]
- exposure_options[:using].represent(object.send(attribute))
+ exposure_options[:using].represent(object.send(attribute), :root => nil)
else
object.send(attribute)
end
View
32 spec/grape/entity_spec.rb
@@ -84,6 +84,23 @@
representation['things'].reject{|r| r.kind_of?(subject)}.should be_empty
end
end
+
+ context 'it can be overridden' do
+ it 'can be disabled' do
+ representation = subject.represent(4.times.map{Object.new}, :root=>false)
+ representation.should be_kind_of(Array)
+ representation.size.should == 4
+ representation.reject{|r| r.kind_of?(subject)}.should be_empty
+ end
+ it 'can use a different name' do
+ representation = subject.represent(4.times.map{Object.new}, :root=>'others')
+ representation.should be_kind_of(Hash)
+ representation.should have_key('others')
+ representation['others'].should be_kind_of(Array)
+ representation['others'].size.should == 4
+ representation['others'].reject{|r| r.kind_of?(subject)}.should be_empty
+ end
+ end
end
context 'with singular root key' do
@@ -194,6 +211,21 @@
rep.last.serializable_hash[:name].should == 'Friend 2'
end
+ it 'should disable root key name for child representations' do
+ class FriendEntity < Grape::Entity
+ root 'friends', 'friend'
+ expose :name, :email
+ end
+ fresh_class.class_eval do
+ expose :friends, :using => FriendEntity
+ end
+ rep = subject.send(:value_for, :friends)
+ rep.should be_kind_of(Array)
+ rep.reject{|r| r.is_a?(FriendEntity)}.should be_empty
+ rep.first.serializable_hash[:name].should == 'Friend 1'
+ rep.last.serializable_hash[:name].should == 'Friend 2'
+ end
+
it 'should call through to the proc if there is one' do
subject.send(:value_for, :computed, :awesome => 123).should == 123
end

No commit comments for this range

Something went wrong with that request. Please try again.