diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index cf92c519..db230f45 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -12,7 +12,7 @@ Metrics/AbcSize: # Offense count: 1 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 296 + Max: 300 # Offense count: 4 Metrics/CyclomaticComplexity: diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 06a2ca0a..7261c2cc 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -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 diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index f762ba24..26b28c86 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -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