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

Add Unexpose Method #109

Merged
merged 1 commit into from
Mar 9, 2015
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 @@ -12,7 +12,7 @@ Metrics/AbcSize:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 296
Max: 300

# Offense count: 4
Metrics/CyclomaticComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Next Release
============

* Your contribution here.
* [#109](https://github.com/intridea/grape-entity/pull/109): Add unexpose method - [@jonmchan](https://github.com/jonmchan).
* [#98](https://github.com/intridea/grape-entity/pull/98): Add nested conditionals - [@zbelzer](https://github.com/zbelzer).
* [#91](https://github.com/intridea/grape-entity/pull/91): Fix OpenStruct serializing - [@etehtsea](https://github.com/etehtsea).
* [#105](https://github.com/intridea/grape-entity/pull/105): Specify which attribute is missing in which Entity - [@jhollinger](https://github.com/jhollinger).
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,31 @@ private
end
```

#### Unexpose

To undefine an exposed field, use the ```.unexpose``` method. Useful for modifying inherited entities.

```ruby
class UserData < Grape::Entity
expose :name
expose :address1
expose :address2
expose :address_state
expose :address_city
expose :email
expose :phone
end

class MailingAddress < UserData
unexpose :email
unexpose :phone
end
```





#### Aliases

Expose under a different name with `:as`.
Expand Down
4 changes: 4 additions & 0 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def self.expose(*args, &block)
end
end

def self.unexpose(attribute)
exposures.delete(attribute)
end

# Set options that will be applied to any exposures declared inside the block.
#
# @example Multi-exposure if
Expand Down
42 changes: 42 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,48 @@ class Parent < Person
end
end

describe '.unexpose' do
it 'is able to remove exposed attributes' do
subject.expose :name, :email
subject.unexpose :email

expect(subject.exposures).to eq(name: {})
end

context 'inherited exposures' do
it 'when called from child class, only removes from the attribute from child' do
subject.expose :name, :email
child_class = Class.new(subject)
child_class.unexpose :email

expect(child_class.exposures).to eq(name: {})
expect(subject.exposures).to eq(name: {}, email: {})
end

# the following 2 behaviors are testing because it is not most intuitive and could be confusing
context 'when called from the parent class' do
it 'remove from parent and all child classes that have not locked down their attributes with an .exposures call' do
subject.expose :name, :email
child_class = Class.new(subject)
subject.unexpose :email

expect(subject.exposures).to eq(name: {})
expect(child_class.exposures).to eq(name: {})
end

it 'remove from parent and do not remove from child classes that have locked down their attributes with an .exposures call' do
subject.expose :name, :email
child_class = Class.new(subject)
child_class.exposures
subject.unexpose :email

expect(subject.exposures).to eq(name: {})
expect(child_class.exposures).to eq(name: {}, email: {})
end
end
end
end

describe '.with_options' do
it 'raises an error for unknown options' do
block = proc do
Expand Down