Skip to content

Commit f7d1c5c

Browse files
committed
Merge pull request #116 from guidoprincess/root-node-inheritance
Root node inheritance
2 parents 6375f15 + 685b5e6 commit f7d1c5c

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Metrics/AbcSize:
1212
# Offense count: 1
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 323
15+
Max: 330
1616

1717
# Offense count: 5
1818
Metrics/CyclomaticComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
============
33

44
* [#114](https://github.com/intridea/grape-entity/pull/114): Added 'only' option that selects which attributes should be returned - [@estevaoam](https://github.com/estevaoam).
5+
* [#115](https://github.com/intridea/grape-entity/pull/115): Allowing 'root' to be inherited from parent to child entities - [@guidoprincess](https://github.com/guidoprincess).
56
* Your contribution here.
67

78
0.4.5 (2015-03-10)

lib/grape_entity/entity.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ def self.present_collection(present_collection = false, collection_name = :items
407407
# @option options :only [Array] all the fields that should be returned
408408
def self.represent(objects, options = {})
409409
if objects.respond_to?(:to_ary) && ! @present_collection
410-
root_element = @collection_root
410+
root_element = root_element(:collection_root)
411411
inner = objects.to_ary.map { |object| new(object, { collection: true }.merge(options)).presented }
412412
else
413413
objects = { @collection_name => objects } if @present_collection
414-
root_element = @root
414+
root_element = root_element(:root)
415415
inner = new(objects, options).presented
416416
end
417417

@@ -420,6 +420,16 @@ def self.represent(objects, options = {})
420420
root_element ? { root_element => inner } : inner
421421
end
422422

423+
# This method returns the entity's root or collection root node, or its parent's
424+
# @param root_type: either :collection_root or just :root
425+
def self.root_element(root_type)
426+
if instance_variable_get("@#{root_type}")
427+
instance_variable_get("@#{root_type}")
428+
elsif superclass.respond_to? :root_element
429+
superclass.root_element(root_type)
430+
end
431+
end
432+
423433
def presented
424434
if options[:serializable]
425435
serializable_hash

spec/grape_entity/entity_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,30 @@ class Parent < Person
664664
end
665665
end
666666
end
667+
668+
context 'inheriting from parent entity' do
669+
before(:each) do
670+
subject.root 'things', 'thing'
671+
end
672+
673+
it 'inherits single root' do
674+
child_class = Class.new(subject)
675+
representation = child_class.represent(Object.new)
676+
expect(representation).to be_kind_of Hash
677+
expect(representation).to have_key 'thing'
678+
expect(representation['thing']).to be_kind_of(child_class)
679+
end
680+
681+
it 'inherits array root root' do
682+
child_class = Class.new(subject)
683+
representation = child_class.represent(4.times.map { Object.new })
684+
expect(representation).to be_kind_of Hash
685+
expect(representation).to have_key('things')
686+
expect(representation['things']).to be_kind_of Array
687+
expect(representation['things'].size).to eq 4
688+
expect(representation['things'].reject { |r| r.is_a?(child_class) }).to be_empty
689+
end
690+
end
667691
end
668692

669693
describe '#initialize' do

0 commit comments

Comments
 (0)