grape-entity 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +1 -1
- data/CHANGELOG.md +6 -0
- data/README.md +16 -0
- data/lib/grape_entity/entity.rb +2 -2
- data/lib/grape_entity/exposure/base.rb +4 -3
- data/lib/grape_entity/version.rb +1 -1
- data/spec/grape_entity/entity_spec.rb +10 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31f997094eb9145e0e34541cc1083562afbc7724f68c343d9bb8f897b1af8c5f
|
4
|
+
data.tar.gz: 271397df6ac51ffdd5e6380a1e4ab47affb3fbb14d805f6d00cc6715084d0f13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0916c3b71d39463de13549202659f8e2c60634e6e6951b9999d45a045307c716f7bcada5a86c100066029a8e4f7da7aea5bf296bc8659f9a871162118c196d3d'
|
7
|
+
data.tar.gz: ed8956de1f403808e504a60b3759e19a0012f9d91db9200fc63891f7d5f5bc29116084619bd47e41d8afce3ef8a98180c620c1f902b3f0dbd4eb42f37d4bb596
|
data/.rubocop_todo.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -8,6 +8,12 @@
|
|
8
8
|
|
9
9
|
* Your contribution here.
|
10
10
|
|
11
|
+
### 0.7.1 (2018-01-30)
|
12
|
+
|
13
|
+
#### Features
|
14
|
+
|
15
|
+
* [#292](https://github.com/ruby-grape/grape-entity/pull/297): Introduce `override` option for expose (fixes [#286](https://github.com/ruby-grape/grape-entity/issues/296)) - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
|
16
|
+
|
11
17
|
### 0.7.0 (2018-01-25)
|
12
18
|
|
13
19
|
#### Features
|
data/README.md
CHANGED
@@ -256,6 +256,22 @@ class MailingAddress < UserData
|
|
256
256
|
end
|
257
257
|
```
|
258
258
|
|
259
|
+
#### Overriding exposures
|
260
|
+
|
261
|
+
If you want to add one more exposure for the field but don't want the first one to be fired (for instance, when using inheritance), you can use the `override` flag. For instance:
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
class User < Grape::Entity
|
265
|
+
expose :name
|
266
|
+
end
|
267
|
+
|
268
|
+
class Employee < UserData
|
269
|
+
expose :name, as: :employee_name, override: true
|
270
|
+
end
|
271
|
+
```
|
272
|
+
|
273
|
+
`User` will return something like this `{ "name" : "John" }` while `Employee` will present the same data as `{ "employee_name" : "John" }` instead of `{ "name" : "John", "employee_name" : "John" }`.
|
274
|
+
|
259
275
|
#### Returning only the fields you want
|
260
276
|
|
261
277
|
After exposing the desired attributes, you can choose which one you need when representing some object or collection by using the only: and except: options. See the example:
|
data/lib/grape_entity/entity.rb
CHANGED
@@ -197,7 +197,7 @@ module Grape
|
|
197
197
|
|
198
198
|
exposure = Exposure.new(attribute, options)
|
199
199
|
|
200
|
-
exposure_list.delete_by(attribute) if
|
200
|
+
exposure_list.delete_by(attribute) if exposure.override?
|
201
201
|
|
202
202
|
exposure_list << exposure
|
203
203
|
|
@@ -527,7 +527,7 @@ module Grape
|
|
527
527
|
|
528
528
|
# All supported options.
|
529
529
|
OPTIONS = %i[
|
530
|
-
rewrite as if unless using with proc documentation format_with safe attr_path if_extras unless_extras merge expose_nil
|
530
|
+
rewrite as if unless using with proc documentation format_with safe attr_path if_extras unless_extras merge expose_nil override
|
531
531
|
].to_set.freeze
|
532
532
|
|
533
533
|
# Merges the given options with current block options.
|
@@ -4,7 +4,7 @@ module Grape
|
|
4
4
|
class Entity
|
5
5
|
module Exposure
|
6
6
|
class Base
|
7
|
-
attr_reader :attribute, :is_safe, :documentation, :conditions, :for_merge
|
7
|
+
attr_reader :attribute, :is_safe, :documentation, :override, :conditions, :for_merge
|
8
8
|
|
9
9
|
def self.new(attribute, options, conditions, *args, &block)
|
10
10
|
super(attribute, options, conditions).tap { |e| e.setup(*args, &block) }
|
@@ -19,6 +19,7 @@ module Grape
|
|
19
19
|
@for_merge = options[:merge]
|
20
20
|
@attr_path_proc = options[:attr_path]
|
21
21
|
@documentation = options[:documentation]
|
22
|
+
@override = options[:override]
|
22
23
|
@conditions = conditions
|
23
24
|
end
|
24
25
|
|
@@ -116,8 +117,8 @@ module Grape
|
|
116
117
|
end
|
117
118
|
end
|
118
119
|
|
119
|
-
def
|
120
|
-
|
120
|
+
def override?
|
121
|
+
@override
|
121
122
|
end
|
122
123
|
|
123
124
|
protected
|
data/lib/grape_entity/version.rb
CHANGED
@@ -477,11 +477,20 @@ describe Grape::Entity do
|
|
477
477
|
expect(child_class.represent({ name: 'bar' }, serializable: true)).to eq(email: nil, name: 'foo')
|
478
478
|
end
|
479
479
|
|
480
|
-
it 'overrides
|
480
|
+
it 'not overrides exposure by default' do
|
481
481
|
subject.expose :name
|
482
482
|
child_class = Class.new(subject)
|
483
483
|
child_class.expose :name, as: :child_name
|
484
484
|
|
485
|
+
expect(subject.represent({ name: 'bar' }, serializable: true)).to eq(name: 'bar')
|
486
|
+
expect(child_class.represent({ name: 'bar' }, serializable: true)).to eq(name: 'bar', child_name: 'bar')
|
487
|
+
end
|
488
|
+
|
489
|
+
it 'overrides parent class exposure when option is specified' do
|
490
|
+
subject.expose :name
|
491
|
+
child_class = Class.new(subject)
|
492
|
+
child_class.expose :name, as: :child_name, override: true
|
493
|
+
|
485
494
|
expect(subject.represent({ name: 'bar' }, serializable: true)).to eq(name: 'bar')
|
486
495
|
expect(child_class.represent({ name: 'bar' }, serializable: true)).to eq(child_name: 'bar')
|
487
496
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-entity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Bleigh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|