Skip to content

Commit

Permalink
Fixes issue with class reopening and inheriting.
Browse files Browse the repository at this point in the history
Exposures and formatters should appear in inherited classes even if
added after inheriting.
  • Loading branch information
marshall-lee committed Aug 7, 2015
1 parent 079993f commit 56ae19e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-08-02 19:30:25 +0300 using RuboCop version 0.31.0.
# on 2015-08-07 13:25:47 +0300 using RuboCop version 0.31.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 6
Metrics/AbcSize:
Max: 33
Max: 36

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 198
Max: 208

# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 11

# Offense count: 208
# Offense count: 209
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 146

# Offense count: 8
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 28
Max: 31

# Offense count: 5
Metrics/PerceivedComplexity:
Expand Down
13 changes: 13 additions & 0 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,16 @@ class << self
# Returns all formatters that are registered for this and it's ancestors
# @return [Hash] of formatters
attr_accessor :formatters
attr_accessor :inherited_entities
end

@inherited_entities = []

def self.inherited(subclass)
subclass.root_exposure = root_exposure.try(:dup) || build_root_exposure
subclass.formatters = formatters.try(:dup) || {}
inherited_entities << subclass
subclass.inherited_entities = []
end

# This method is the primary means by which you will declare what attributes
Expand Down Expand Up @@ -173,6 +178,10 @@ def self.expose(*args, &block)
@nesting_stack.pop
end
end

inherited_entities.each do |entity|
entity.expose(*args, &block)
end
end

def self.build_root_exposure
Expand Down Expand Up @@ -264,6 +273,10 @@ def self.documentation
def self.format_with(name, &block)
fail ArgumentError, 'You must pass a block for formatters' unless block_given?
formatters[name.to_sym] = block

inherited_entities.each do |entity|
entity.format_with(name, &block)
end
end

# This allows you to set a root element name for your representation.
Expand Down
19 changes: 19 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ class Parent < Person
expect(subject.represent({ name: 'bar' }, serializable: true)).to eq(email: nil, name: 'bar')
expect(child_class.represent({ name: 'bar' }, serializable: true)).to eq(email: nil, name: 'foo')
end

it 'supports class reopening' do
subject.expose :name
child_class = Class.new(subject)
subject.expose :email

object = OpenStruct.new(name: 'bar', email: 'foo@bar')
expected = { name: 'bar', email: 'foo@bar' }

expect(subject.represent(object, serializable: true)).to eq(expected)
expect(child_class.represent(object, serializable: true)).to eq(expected)
end
end

context 'register formatters' do
Expand All @@ -290,6 +302,13 @@ class Parent < Person
expect(child_class.formatters).to eq subject.formatters
end

it 'inherits formatters from ancestors with reopening' do
child_class = Class.new(subject)
subject.format_with :timestamp, &date_formatter

expect(child_class.formatters).to eq subject.formatters
end

it 'does not allow registering a formatter without a block' do
expect { subject.format_with :foo }.to raise_error ArgumentError
end
Expand Down

0 comments on commit 56ae19e

Please sign in to comment.