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 #134.
It's something undocumented before but it worked before and I don't see
anything harmful in the presence of this feature.

Fixes #166.

Conflicts:
	.rubocop_todo.yml
	CHANGELOG.md
	lib/grape_entity/entity.rb
	lib/grape_entity/version.rb
	spec/grape_entity/entity_spec.rb
  • Loading branch information
marshall-lee committed Aug 10, 2015
1 parent a7d0396 commit ae847ca
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-08-02 19:30:25 +0300 using RuboCop version 0.31.0.
# on 2015-08-10 13:14:22 +0300 using RuboCop version 0.31.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -12,13 +12,13 @@ Metrics/AbcSize:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 198
Max: 202

# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 11

# Offense count: 208
# Offense count: 210
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 146
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* [#151](https://github.com/ruby-grape/grape-entity/pull/151): Fix: deep projections (`:only`, `:except`) were unaware of nesting: [#156](https://github.com/ruby-grape/grape-entity/issues/156) - [@marshall-lee](https://github.com/marshall-lee).
* Your contribution here.

0.4.8 (2015-08-10)
==================
* [#167](https://github.com/ruby-grape/grape-entity/pull/167): Regression: global settings (exposures, formatters) on `Grape::Entity` should work: [#166](https://github.com/ruby-grape/grape-entity/issues/166) - [@marshall-lee](http://github.com/marshall-lee).

0.4.7 (2015-08-03)
==================
* [#164](https://github.com/ruby-grape/grape-entity/pull/164): Regression: entity instance methods were exposed with `NoMethodError`: [#163](https://github.com/ruby-grape/grape-entity/issues/163) - [@marshall-lee](http://github.com/marshall-lee).
Expand Down
23 changes: 15 additions & 8 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,26 @@ def entity(options = {})
end

class << self
attr_accessor :root_exposure
def root_exposure
@root_exposure ||= Exposure.new(nil, nesting: true)
end

attr_writer :root_exposure

# Returns all formatters that are registered for this and it's ancestors
# @return [Hash] of formatters
attr_accessor :formatters
def formatters
@formatters ||= {}
end

attr_writer :formatters
end

@formatters = {}

def self.inherited(subclass)
subclass.root_exposure = root_exposure.try(:dup) || build_root_exposure
subclass.formatters = formatters.try(:dup) || {}
subclass.root_exposure = root_exposure.dup
subclass.formatters = formatters.dup
end

# This method is the primary means by which you will declare what attributes
Expand Down Expand Up @@ -175,10 +186,6 @@ def self.expose(*args, &block)
end
end

def self.build_root_exposure
Exposure.new(nil, nesting: true)
end

# Returns exposures that have been declared for this Entity on the top level.
# @return [Array] of exposures
def self.root_exposures
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 @@ -322,6 +322,26 @@ class Parent < Person
subject.expose(:size, format_with: :size_formatter)
expect(subject.represent(object).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).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).value_for(:x)).to eq 11
subject.expose :y
expect(subject.represent(object).value_for(:x)).to eq 11
expect(subject.represent(object).value_for(:y)).to eq 22
Grape::Entity.unexpose :x
end
end

Expand Down Expand Up @@ -370,6 +390,13 @@ class Parent < Person
end
end.to raise_error(/You cannot call 'unexpose`/)
end

it 'works global on Grape::Entity' do
Grape::Entity.expose :x
expect(Grape::Entity.root_exposures[0].attribute).to eq(:x)
Grape::Entity.unexpose :x
expect(Grape::Entity.root_exposures).to eq([])
end
end

describe '.with_options' do
Expand Down

0 comments on commit ae847ca

Please sign in to comment.