Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow replace non-conditional non-nesting exposures in child classes #292

Merged
merged 1 commit into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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