grape-active_model_serializers 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/grape-active_model_serializers.gemspec +1 -1
- data/lib/grape-active_model_serializers/endpoint_extension.rb +18 -1
- data/lib/grape-active_model_serializers/formatter.rb +39 -3
- data/lib/grape-active_model_serializers/version.rb +1 -1
- data/spec/features/grape-active_model_serializers/render_spec.rb +65 -0
- data/spec/grape-active_model_serializers/endpoint_extension_spec.rb +39 -0
- data/spec/grape-active_model_serializers/formatter_spec.rb +28 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e9d5adc2e060c4ff6cb0ff20b716af5ac60b65f
|
4
|
+
data.tar.gz: 1175bcd15b520733dca73731304eb07c1ef41ff1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2253fc66827baafc811df441f44e6d44119a19a6c5b3c926d3a643259a6cafe3059eb9cf2c9523a973900884093dd30ba374de6c178d333477462e67efc501d3
|
7
|
+
data.tar.gz: e5e75c33e502cd1b41d1e94ad2b9489cd26cc47f3dfff68e1692445d7366f61d1eb4bb91bd681bf5391b78dce23c315f1a93166790a9f99e77408a1b061a93f3
|
data/README.md
CHANGED
@@ -99,6 +99,16 @@ namespace 'foo', :serializer => :bar do
|
|
99
99
|
end
|
100
100
|
```
|
101
101
|
|
102
|
+
### Custom metadata along with the resources
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
# Control any additional metadata using meta and meta_key
|
106
|
+
get "/homes"
|
107
|
+
collection = Home.all
|
108
|
+
render collection, { meta: { page: 5, current_page: 3 }, meta_key: :pagination_info }
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
102
112
|
### current_user
|
103
113
|
|
104
114
|
One of the nice features of ActiveModel::Serializers is that it
|
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.licenses = ['MIT']
|
18
18
|
|
19
19
|
gem.add_dependency "grape", "~> 0.3"
|
20
|
-
gem.add_dependency "active_model_serializers", ">= 0.
|
20
|
+
gem.add_dependency "active_model_serializers", ">= 0.9.0.alpha1"
|
21
21
|
|
22
22
|
gem.add_development_dependency "rspec"
|
23
23
|
gem.add_development_dependency "rack-test"
|
@@ -27,9 +27,26 @@ module Grape
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
def render(resources, meta={})
|
31
|
+
set_meta_and_meta_key(meta)
|
32
|
+
resources
|
33
|
+
end
|
34
|
+
|
30
35
|
def default_serializer_options; end
|
36
|
+
|
31
37
|
def url_options; end
|
32
|
-
end
|
33
38
|
|
39
|
+
private
|
40
|
+
|
41
|
+
def set_meta_and_meta_key(meta)
|
42
|
+
if meta.has_key?(:meta)
|
43
|
+
Formatter::ActiveModelSerializers.meta = meta[:meta]
|
44
|
+
if meta.has_key?(:meta_key)
|
45
|
+
Formatter::ActiveModelSerializers.meta_key = meta[:meta_key]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
34
51
|
Endpoint.send(:include, EndpointExtension)
|
35
52
|
end
|
@@ -16,11 +16,43 @@ module Grape
|
|
16
16
|
endpoint = env['api.endpoint']
|
17
17
|
options = build_options_from_endpoint(endpoint)
|
18
18
|
|
19
|
-
if
|
19
|
+
if serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
|
20
|
+
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
20
21
|
# ensure we have an root to fallback on
|
21
|
-
|
22
|
+
options[:resource_name] = default_root(endpoint) if resource.respond_to?(:to_ary)
|
23
|
+
serializer.new(resource, options.merge(other_options))
|
22
24
|
end
|
23
|
-
|
25
|
+
end
|
26
|
+
|
27
|
+
def other_options
|
28
|
+
options = {}
|
29
|
+
if @meta_content_items
|
30
|
+
if @meta_key
|
31
|
+
key_option = @meta_key[:meta_key]
|
32
|
+
@meta_key.delete(:meta_key)
|
33
|
+
options[:meta_key] = key_option if key_option
|
34
|
+
end
|
35
|
+
meta_option = @meta_content_items[:meta]
|
36
|
+
@meta_content_items.delete(:meta)
|
37
|
+
options[key_option || :meta] = meta_option if meta_option
|
38
|
+
end
|
39
|
+
options
|
40
|
+
end
|
41
|
+
|
42
|
+
def meta
|
43
|
+
@meta_content_items || {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def meta=(meta_content)
|
47
|
+
@meta_content_items = { meta: meta_content } if meta_content
|
48
|
+
end
|
49
|
+
|
50
|
+
def meta_key
|
51
|
+
@meta_key || {}
|
52
|
+
end
|
53
|
+
|
54
|
+
def meta_key=(key)
|
55
|
+
@meta_key = { meta_key: key } if key
|
24
56
|
end
|
25
57
|
|
26
58
|
def build_options_from_endpoint(endpoint)
|
@@ -38,6 +70,10 @@ module Grape
|
|
38
70
|
endpoint.options[:path][0].to_s.split('/')[-1]
|
39
71
|
end
|
40
72
|
end
|
73
|
+
|
74
|
+
def serialization_scope
|
75
|
+
:current_user
|
76
|
+
end
|
41
77
|
end
|
42
78
|
end
|
43
79
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/models/user'
|
3
|
+
require 'support/serializers/user_serializer'
|
4
|
+
require 'grape-active_model_serializers'
|
5
|
+
require 'securerandom'
|
6
|
+
|
7
|
+
describe '#render' do
|
8
|
+
let(:app) { Class.new(Grape::API) }
|
9
|
+
|
10
|
+
before do
|
11
|
+
app.format :json
|
12
|
+
app.formatter :json, Grape::Formatter::ActiveModelSerializers
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_resource_with(meta)
|
16
|
+
url = "/#{SecureRandom.hex}"
|
17
|
+
app.get(url) do
|
18
|
+
render User.new(first_name: 'Jeff'), meta
|
19
|
+
end
|
20
|
+
get url
|
21
|
+
JSON.parse last_response.body
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'with meta key' do
|
25
|
+
it 'includes meta key and content' do
|
26
|
+
result = get_resource_with({ meta: { total: 2 }})
|
27
|
+
expect(result).to have_key('meta')
|
28
|
+
expect(result.fetch('meta')).to eq({ 'total' => 2 })
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with a custom meta_key' do
|
33
|
+
|
34
|
+
it 'includes the custom meta key name' do
|
35
|
+
result = get_resource_with({ meta: { total: 2 }, meta_key: :custom_key_name })
|
36
|
+
expect(result).to have_key('custom_key_name')
|
37
|
+
expect(result.fetch('custom_key_name')).to eq({ 'total' => 2 })
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'ignores a lonely meta_key' do
|
41
|
+
result = get_resource_with({ meta_key: :custom_key_name })
|
42
|
+
expect(result).not_to have_key('meta')
|
43
|
+
expect(result).not_to have_key('custom_key_name')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'junk keys' do
|
48
|
+
|
49
|
+
it 'ignores junk keys' do
|
50
|
+
result = get_resource_with({ junk_key: { total: 2 } })
|
51
|
+
expect(result).not_to have_key('junk_key')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'ignores empty meta_key' do
|
55
|
+
result = get_resource_with({ meta: { total: 2 }, meta_key: nil })
|
56
|
+
expect(result).to have_key('meta')
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'ignores empty meta' do
|
60
|
+
result = get_resource_with({ meta: nil })
|
61
|
+
expect(result).not_to have_key('meta')
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Grape::EndpointExtension' do
|
4
|
+
|
5
|
+
subject { Grape::Endpoint.new(nil, {path: '/', method: 'foo'}) }
|
6
|
+
|
7
|
+
let(:serializer) { Grape::Formatter::ActiveModelSerializers }
|
8
|
+
|
9
|
+
let(:user) do
|
10
|
+
Object.new do
|
11
|
+
def name
|
12
|
+
'sven'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:users) { [user, user] }
|
18
|
+
|
19
|
+
describe "#render" do
|
20
|
+
it { should respond_to(:render) }
|
21
|
+
let (:meta_content) { { total: 2 } }
|
22
|
+
let (:meta_full) { { meta: meta_content } }
|
23
|
+
context 'supplying meta' do
|
24
|
+
it 'passes through the Resource and uses given meta settings' do
|
25
|
+
expect(serializer).to receive(:meta=).with(meta_content)
|
26
|
+
expect(subject.render(users, meta_full)).to eq(users)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
context 'supplying meta and key' do
|
30
|
+
let (:meta_key) { { meta_key: :custom_key_name } }
|
31
|
+
it 'passes through the Resource and uses given meta settings' do
|
32
|
+
expect(serializer).to receive(:meta=).with(meta_content)
|
33
|
+
expect(serializer).to receive(:meta_key=).with(meta_key[:meta_key])
|
34
|
+
expect(subject.render(users, meta_full.merge(meta_key))).to eq(users)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -2,5 +2,33 @@ require 'spec_helper'
|
|
2
2
|
require 'grape-active_model_serializers/formatter'
|
3
3
|
|
4
4
|
describe Grape::Formatter::ActiveModelSerializers do
|
5
|
+
subject { Grape::Formatter::ActiveModelSerializers }
|
6
|
+
it { should respond_to(:meta) }
|
7
|
+
it { should respond_to(:meta=) }
|
8
|
+
it { should respond_to(:meta_key) }
|
9
|
+
it { should respond_to(:meta_key=) }
|
5
10
|
|
11
|
+
context '#meta' do
|
12
|
+
it 'will silently accept falsy input but return empty Hash' do
|
13
|
+
subject.meta = nil
|
14
|
+
expect(subject.meta).to eq({})
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'will wrap valid input in the meta: {} wrapper' do
|
18
|
+
subject.meta = { total: 2 }
|
19
|
+
expect(subject.meta).to eq({ meta: { total: 2 } })
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context '#meta_key' do
|
24
|
+
it 'will silently accept falsy input but return empty Hash' do
|
25
|
+
subject.meta_key = nil
|
26
|
+
expect(subject.meta_key).to eq({})
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'will wrap valid input in the meta_key: {} wrapper' do
|
30
|
+
subject.meta_key = :custom_key_name
|
31
|
+
expect(subject.meta_key).to eq({ meta_key: :custom_key_name })
|
32
|
+
end
|
33
|
+
end
|
6
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-active_model_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Richard Henry Evans
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.9.0.alpha1
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
40
|
+
version: 0.9.0.alpha1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,6 +129,8 @@ files:
|
|
129
129
|
- lib/grape-active_model_serializers/endpoint_extension.rb
|
130
130
|
- lib/grape-active_model_serializers/formatter.rb
|
131
131
|
- lib/grape-active_model_serializers/version.rb
|
132
|
+
- spec/features/grape-active_model_serializers/render_spec.rb
|
133
|
+
- spec/grape-active_model_serializers/endpoint_extension_spec.rb
|
132
134
|
- spec/grape-active_model_serializers/formatter_spec.rb
|
133
135
|
- spec/grape-active_model_serializers_spec.rb
|
134
136
|
- spec/old_grape_ams_spec.rb
|
@@ -162,6 +164,8 @@ signing_key:
|
|
162
164
|
specification_version: 4
|
163
165
|
summary: Use active_model_serializer in grape
|
164
166
|
test_files:
|
167
|
+
- spec/features/grape-active_model_serializers/render_spec.rb
|
168
|
+
- spec/grape-active_model_serializers/endpoint_extension_spec.rb
|
165
169
|
- spec/grape-active_model_serializers/formatter_spec.rb
|
166
170
|
- spec/grape-active_model_serializers_spec.rb
|
167
171
|
- spec/old_grape_ams_spec.rb
|