From 269325857dcf7631b837c97413753b5f3d5e56a2 Mon Sep 17 00:00:00 2001 From: Vladimir Kochnev Date: Mon, 10 Aug 2015 12:25:21 +0300 Subject: [PATCH] Allow global settings on Grape::Entity. This fixes a regression introduced after merging a #134. It's something undocumented before but it worked before and I don't see anything harmful in the presence of this feature. --- lib/grape_entity/entity.rb | 19 ++++++++++++++----- spec/grape_entity/entity_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 644d928d..88327b9b 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -112,12 +112,18 @@ class << self attr_accessor :nested_exposures end + @exposures = {} + @root_exposures = {} + @nested_exposures = {} + @nested_attribute_names = {} + @formatters = {} + def self.inherited(subclass) - subclass.exposures = exposures.try(:dup) || {} - subclass.root_exposures = root_exposures.try(:dup) || {} - subclass.nested_exposures = nested_exposures.try(:dup) || {} - subclass.nested_attribute_names = nested_attribute_names.try(:dup) || {} - subclass.formatters = formatters.try(:dup) || {} + subclass.exposures = exposures.try(:dup) + subclass.root_exposures = root_exposures.try(:dup) + subclass.nested_exposures = nested_exposures.try(:dup) + subclass.nested_attribute_names = nested_attribute_names.try(:dup) + subclass.formatters = formatters.try(:dup) end # This method is the primary means by which you will declare what attributes @@ -183,7 +189,10 @@ def self.expose(*args, &block) end def self.unexpose(attribute) + root_exposures.delete(attribute) exposures.delete(attribute) + nested_exposures.delete(attribute) + nested_attribute_names.delete(attribute) end # Set options that will be applied to any exposures declared inside the block. diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index af4ac549..d344aab6 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -278,6 +278,26 @@ class Parent < Person subject.expose(:size, format_with: :size_formatter) expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s end + + it 'works global on Grape::Entity' do + Grape::Entity.format_with :size_formatter do |_date| + self.object.class.to_s + end + object = {} + + subject.expose(:size, format_with: :size_formatter) + expect(subject.represent(object).send(:value_for, :size)).to eq object.class.to_s + end + end + + it 'works global on Grape::Entity' do + Grape::Entity.expose :x + object = { x: 11, y: 22 } + expect(Grape::Entity.represent(object).send(:value_for, :x)).to eq 11 + subject.expose :y + expect(subject.represent(object).send(:value_for, :x)).to eq 11 + expect(subject.represent(object).send(:value_for, :y)).to eq 22 + Grape::Entity.unexpose :x end end @@ -310,6 +330,13 @@ class Parent < Person end end end + + it 'works global on Grape::Entity' do + Grape::Entity.expose :x + expect(Grape::Entity.exposures).to eq(x: {}) + Grape::Entity.unexpose :x + expect(Grape::Entity.exposures).to eq({}) + end end describe '.with_options' do