Skip to content

Commit

Permalink
Merge abd8c93 into 65d7650
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryTsepelev committed Jan 17, 2018
2 parents 65d7650 + abd8c93 commit 322ae57
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Metrics/AbcSize:
# Offense count: 35
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 1489
Max: 1496

# Offense count: 2
# Configuration parameters: CountComments.
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#### Fixes

* [#288](https://github.com/ruby-grape/grape-entity/pull/288) Fix wrong argument exception when &:block passed to the expose method - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
* [#291](https://github.com/ruby-grape/grape-entity/pull/291) Refactor and simplify various classes and modules
* [#291](https://github.com/ruby-grape/grape-entity/pull/291) Refactor and simplify various classes and modules - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
* [#292](https://github.com/ruby-grape/grape-entity/pull/292): Allow replace non-conditional non-nesting exposures in child classes (fixes [#286](https://github.com/ruby-grape/grape-entity/issues/286)) - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
* Your contribution here.

### 0.6.1 (2017-01-09)
Expand Down
31 changes: 15 additions & 16 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,25 +185,24 @@ def self.expose(*args, &block)

@documentation = nil
@nesting_stack ||= []
args.each { |attribute| build_exposure_for_attribute(attribute, @nesting_stack, options, block) }
end

# rubocop:disable Style/Next
args.each do |attribute|
exposure = Exposure.new(attribute, options)
def self.build_exposure_for_attribute(attribute, nesting_stack, options, block)
exposure_list = nesting_stack.empty? ? root_exposures : nesting_stack.last.nested_exposures

if @nesting_stack.empty?
root_exposures << exposure
else
@nesting_stack.last.nested_exposures << exposure
end
exposure = Exposure.new(attribute, options)

# Nested exposures are given in a block with no parameters.
if exposure.nesting?
@nesting_stack << exposure
block.call
@nesting_stack.pop
end
end
# rubocop:enable Style/Next
exposure_list.delete_by(attribute) if exposure_list.select_by(attribute).all? { |exp| exp.replaceable_by?(exposure) }

exposure_list << exposure

# Nested exposures are given in a block with no parameters.
return unless exposure.nesting?

nesting_stack << exposure
block.call
nesting_stack.pop
end

# Returns exposures that have been declared for this Entity on the top level.
Expand Down
4 changes: 4 additions & 0 deletions lib/grape_entity/exposure/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def with_attr_path(entity, options)
end
end

def replaceable_by?(other)
!nesting? && !conditional? && !other.nesting? && !other.conditional?
end

protected

attr_reader :options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def find_by(attribute)
@exposures.find { |e| e.attribute == attribute }
end

def select_by(attribute)
@exposures.select { |e| e.attribute == attribute }
end

def <<(exposure)
reset_memoization!
@exposures << exposure
Expand Down
9 changes: 9 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,15 @@ 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 'overrides parent class exposure' do
subject.expose :name
child_class = Class.new(subject)
child_class.expose :name, as: :child_name

expect(subject.represent({ name: 'bar' }, serializable: true)).to eq(name: 'bar')
expect(child_class.represent({ name: 'bar' }, serializable: true)).to eq(child_name: 'bar')
end
end

context 'register formatters' do
Expand Down

0 comments on commit 322ae57

Please sign in to comment.