Skip to content

Commit 3b2a190

Browse files
committed
Allow for strings in 'only' and 'except' options
With the addition of the `only` and `except` options, it opens up the possiblities to the [partial response](http://bit.ly/1fxWuXJ) design pattern where the consumer of the API can specifically request only the fields that it desires. A common pattern to request these fields would be via `params`, which will arrive as strings. Currently only an array of symbols (or hash containing symbols) is supported to define the desired fields. This commit updates Grape::Entity to support an array of symbols _or_ strings to be passed as the `only` or `except` options of `represent`.
1 parent 5a6d1b6 commit 3b2a190

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [#121](https://github.com/intridea/grape-entity/pull/122): Sublcassed Entity#documentation properly handles unexposed params - [@dan-corneanu](https://github.com/dan-corneanu).
77
* [#134](https://github.com/intridea/grape-entity/pull/134): Subclasses no longer affected in all cases by `unexpose` in parent - [@etehtsea](https://github.com/etehtsea).
88
* [#135](https://github.com/intridea/grape-entity/pull/135): Added `except` option - [@dan-corneanu](https://github.com/dan-corneanu).
9+
* [#136](https://github.com/intridea/grape-entity/pull/136): Allow for strings in `only` and `except` options - [@bswinnerton](https://github.com/bswinnerton).
910
* Your contribution here.
1011

1112
0.4.5 (2015-03-10)

lib/grape_entity/entity.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def only_fields(options, for_attribute = nil)
465465
else
466466
allowed_fields[attribute] = true
467467
end
468-
end
468+
end.symbolize_keys
469469

470470
if for_attribute && @only_fields[for_attribute].is_a?(Array)
471471
@only_fields[for_attribute]
@@ -486,7 +486,7 @@ def except_fields(options, for_attribute = nil)
486486
else
487487
allowed_fields[attribute] = true
488488
end
489-
end
489+
end.symbolize_keys
490490

491491
if for_attribute && @except_fields[for_attribute].is_a?(Array)
492492
@except_fields[for_attribute]

spec/grape_entity/entity_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,48 @@ class Parent < Person
517517
expect(representation).to eq(name: nil)
518518
end
519519

520+
context 'with strings or symbols passed to only and except' do
521+
let(:object) { OpenStruct.new(user: {}) }
522+
523+
before do
524+
user_entity = Class.new(Grape::Entity)
525+
user_entity.expose(:id, :name, :email)
526+
527+
subject.expose(:id, :name, :phone, :address)
528+
subject.expose(:user, using: user_entity)
529+
end
530+
531+
it 'can specify "only" option attributes as strings' do
532+
representation = subject.represent(object, only: ['id', 'name', { 'user' => ['email'] }], serializable: true)
533+
expect(representation).to eq(id: nil, name: nil, user: { email: nil })
534+
end
535+
536+
it 'can specify "except" option attributes as strings' do
537+
representation = subject.represent(object, except: ['id', 'name', { 'user' => ['email'] }], serializable: true)
538+
expect(representation).to eq(phone: nil, address: nil, user: { id: nil, name: nil })
539+
end
540+
541+
it 'can specify "only" option attributes as symbols' do
542+
representation = subject.represent(object, only: [:name, :phone, { user: [:name] }], serializable: true)
543+
expect(representation).to eq(name: nil, phone: nil, user: { name: nil })
544+
end
545+
546+
it 'can specify "except" option attributes as symbols' do
547+
representation = subject.represent(object, except: [:name, :phone, { user: [:name] }], serializable: true)
548+
expect(representation).to eq(id: nil, address: nil, user: { id: nil, email: nil })
549+
end
550+
551+
it 'can specify "only" attributes as strings and symbols' do
552+
representation = subject.represent(object, only: [:id, 'address', { user: [:id, 'name'] }], serializable: true)
553+
expect(representation).to eq(id: nil, address: nil, user: { id: nil, name: nil })
554+
end
555+
556+
it 'can specify "except" attributes as strings and symbols' do
557+
representation = subject.represent(object, except: [:id, 'address', { user: [:id, 'name'] }], serializable: true)
558+
expect(representation).to eq(name: nil, phone: nil, user: { email: nil })
559+
end
560+
end
561+
520562
it 'can specify children attributes with only' do
521563
user_entity = Class.new(Grape::Entity)
522564
user_entity.expose(:id, :name, :email)

0 commit comments

Comments
 (0)