Skip to content

Commit

Permalink
Allow global settings on Grape::Entity.
Browse files Browse the repository at this point in the history
This fixes a regression introduced after merging a ruby-grape#134.
It's something undocumented before but it worked before and I don't see
anything harmful in the presence of this feature.
  • Loading branch information
marshall-lee committed Aug 10, 2015
1 parent 7235364 commit 2693258
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2693258

Please sign in to comment.