grape-active_model_serializers 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7f60aa1ac797001ff0756f457621d9ff5e99792
4
- data.tar.gz: 5ba457a7a9b482bae17e354bef91425659c0821c
3
+ metadata.gz: e8ff356c3449ed21a7317fa1aba4f2166bd37f08
4
+ data.tar.gz: d5d8c18b882b728fa2dc80ef8fc117f50514cfcf
5
5
  SHA512:
6
- metadata.gz: f3207365ca7d238a108f87045f21aa493155b836c00c54fbe141f7519feb851828ca7c94dced658404a01db3ae9053b579f2521c7a300b4d8e06f1ecef34b66c
7
- data.tar.gz: 6c9646fc114bb51c3f8acf6d30c890283b5a9d95a8c7f8328fcf6010e21970109aba81c9019ea2b5bbf6250d328d5f29ba8cb12e538a7a39f85f1677d64eb4de
6
+ metadata.gz: 1d759b25c58f6f4f822268643888949e4a499361c987ff2900b535ec59af5ca9926701cf87ade34f7346dca4a664c97836d08aabb6e704d022bb72dfb81bdca2
7
+ data.tar.gz: bcf1bebecde31d684cc11d7ae200caf6b9ff1ec254f328292f882162ac3c909efc03caace67a20925174fdd2d9aa1b6f89657bb76fd6e5a24d6f4164b98841b6
data/.rubocop.yml CHANGED
@@ -1,2 +1,9 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
+ AllCops:
4
+ Exclude:
5
+ - Guardfile
6
+ - grape-active_model_serializers.gemspec
7
+
8
+ Style/BlockDelimiters:
9
+ Enabled: false
data/.rubocop_todo.yml CHANGED
@@ -10,9 +10,6 @@
10
10
  Metrics/AbcSize:
11
11
  Max: 20
12
12
 
13
- Metrics/LineLength:
14
- Max: 179
15
-
16
13
  # Offense count: 7
17
14
  Style/Documentation:
18
15
  Enabled: false
@@ -21,7 +18,3 @@ Style/Documentation:
21
18
  # Configuration parameters: Exclude.
22
19
  Style/FileName:
23
20
  Enabled: false
24
-
25
- # Offense count: 4
26
- Style/RegexpLiteral:
27
- MaxSlashes: 0
data/.travis.yml CHANGED
@@ -24,7 +24,6 @@ matrix:
24
24
  env: GRAPE_VERSION=HEAD
25
25
  - rvm: 2.3.0
26
26
  - rvm: 2.2.5
27
- - rvm: 2.1
28
27
  - rvm: rbx-2
29
28
  - rvm: jruby-19mode
30
29
  - rvm: ruby-head
data/CHANGELOG.md CHANGED
@@ -1,39 +1,47 @@
1
1
  ## Changelog
2
2
 
3
- ### 1.5.0 (Next)
3
+ ### 1.5.1 (Next)
4
4
 
5
- * Your contribution here.
5
+ * ...
6
+
7
+ ### 1.5.0 (August 24, 2016)
8
+
9
+ * [#61](https://github.com/ruby-grape/grape-active_model_serializers/pull/61): Adds support for collection serializers - [@drn](https://github.com/drn).
10
+ * [#60](https://github.com/ruby-grape/grape-active_model_serializers/pull/60): Namespace serializer inference - [@drn](https://github.com/drn).
11
+ * [#59](https://github.com/ruby-grape/grape-active_model_serializers/pull/59): Refactor option and serializer resolution - [@drn](https://github.com/drn).
12
+ * [#57](https://github.com/ruby-grape/grape-active_model_serializers/pull/57): Solve line length linter issues - [@drn](https://github.com/drn).
13
+ * [#54](https://github.com/ruby-grape/grape-active_model_serializers/pull/54): Adding support for ASM v0.10. Drops support for ASM v0.9 - [@drn](https://github.com/drn).
6
14
 
7
15
  ### 1.4.0 (July 14, 2016)
8
16
 
9
- * [#49](https://github.com/jrhe/grape-active_model_serializers/pull/49): Adds support for active model serializer namespace - [@syntaxTerr0r](https://github.com/syntaxTerr0r).
10
- * [#36](https://github.com/jrhe/grape-active_model_serializers/pull/36), [#50](https://github.com/jrhe/grape-active_model_serializers/pull/50): Added support through Grape 0.16.x - [@dblock](https://github.com/dblock).
17
+ * [#49](https://github.com/ruby-grape/grape-active_model_serializers/pull/49): Adds support for active model serializer namespace - [@syntaxTerr0r](https://github.com/syntaxTerr0r).
18
+ * [#36](https://github.com/ruby-grape/grape-active_model_serializers/pull/36), [#50](https://github.com/jrhe/grape-active_model_serializers/pull/50): Added support through Grape 0.16.x - [@dblock](https://github.com/dblock).
11
19
 
12
20
  ### v1.3.2 (February 27, 2015)
13
21
 
14
- * [#39](https://github.com/jrhe/grape-active_model_serializers/pull/39): Look for namespace and other options to configure serializers - [@jwkoelewijn](https://github.com/jwkoelewijn).
15
- * [#40](https://github.com/jrhe/grape-active_model_serializers/pull/40): Use env to pass AMS meta around - [@dblock](https://github.com/dblock).
22
+ * [#40](https://github.com/ruby-grape/grape-active_model_serializers/pull/40): Use env to pass AMS meta around - [@dblock](https://github.com/dblock).
23
+ * [#39](https://github.com/ruby-grape/grape-active_model_serializers/pull/39): Look for namespace and other options to configure serializers - [@jwkoelewijn](https://github.com/jwkoelewijn).
16
24
 
17
25
  ### v1.3.1 (November 20, 2014)
18
26
 
19
- * [#30](https://github.com/jrhe/grape-active_model_serializers/pull/30): Read options from default_serializer_options - [@siong1987](https://github.com/siong1987).
20
- * [#24](https://github.com/jrhe/grape-active_model_serializers/pull/24): Makes it possible to use `current_user` within serializers - [@sonxurxo](https://github.com/sonxurxo).
27
+ * [#30](https://github.com/ruby-grape/grape-active_model_serializers/pull/30): Read options from default_serializer_options - [@siong1987](https://github.com/siong1987).
28
+ * [#24](https://github.com/ruby-grape/grape-active_model_serializers/pull/24): Makes it possible to use `current_user` within serializers - [@sonxurxo](https://github.com/sonxurxo).
21
29
 
22
30
  ### v1.2.1 (July 23, 2014)
23
31
 
24
- * [#21](https://github.com/jrhe/grape-active_model_serializers/pull/21): Correctly fetch serialization scope - [@radanskoric](https://github.com/radanskoric).
25
- * [#18](https://github.com/jrhe/grape-active_model_serializers/pull/18): Added support for active model serializer 0.9.x - [@sbounmy](https://github.com/sbounmy).
32
+ * [#21](https://github.com/ruby-grape/grape-active_model_serializers/pull/21): Correctly fetch serialization scope - [@radanskoric](https://github.com/radanskoric).
33
+ * [#18](https://github.com/ruby-grape/grape-active_model_serializers/pull/18): Added support for active model serializer 0.9.x - [@sbounmy](https://github.com/sbounmy).
26
34
 
27
- * [#15](https://github.com/jrhe/grape-active_model_serializers/pull/15): Added `render` syntactic sugar - [@zph](https://github.com/zph).
28
- * [#14](https://github.com/jrhe/grape-active_model_serializers/pull/14): Fix: `default_root` method to support symbol route in Grape - [@wjp2013](https://github.com/wjp2013).
29
- * [#12](https://github.com/jrhe/grape-active_model_serializers/pull/12): Added support for `current_user` - [@kpassapk](https://github.com/kpassapk).
30
- * [#11](https://github.com/jrhe/grape-active_model_serializers/pull/11): Fixed require path - [@schickling](https://github.com/schickling).
35
+ * [#15](https://github.com/ruby-grape/grape-active_model_serializers/pull/15): Added `render` syntactic sugar - [@zph](https://github.com/zph).
36
+ * [#14](https://github.com/ruby-grape/grape-active_model_serializers/pull/14): Fix: `default_root` method to support symbol route in Grape - [@wjp2013](https://github.com/wjp2013).
37
+ * [#12](https://github.com/ruby-grape/grape-active_model_serializers/pull/12): Added support for `current_user` - [@kpassapk](https://github.com/kpassapk).
38
+ * [#11](https://github.com/ruby-grape/grape-active_model_serializers/pull/11): Fixed require path - [@schickling](https://github.com/schickling).
31
39
 
32
40
  ### v1.0.1 (September 9, 2013)
33
41
 
34
- * [#6](https://github.com/jrhe/grape-active_model_serializers/pull/6): Conform to ActiveModel::Serializers way of determining array-ness - [@tfe](https://github.com/tfe).
35
- * [#4](https://github.com/jrhe/grape-active_model_serializers/pull/4): Support for namespace options and rely more on active_model_serializers - [@johnallen3d](https://github.com/johnallen3d).
36
- * [#1](https://github.com/jrhe/grape-active_model_serializers/pull/1): Fix: Grape::ActiveModelSerializers for models with compound names - [@george](https://github.com/george).
42
+ * [#6](https://github.com/ruby-grape/grape-active_model_serializers/pull/6): Conform to ActiveModel::Serializers way of determining array-ness - [@tfe](https://github.com/tfe).
43
+ * [#4](https://github.com/ruby-grape/grape-active_model_serializers/pull/4): Support for namespace options and rely more on active_model_serializers - [@johnallen3d](https://github.com/johnallen3d).
44
+ * [#1](https://github.com/ruby-grape/grape-active_model_serializers/pull/1): Fix: Grape::ActiveModelSerializers for models with compound names - [@george](https://github.com/george).
37
45
 
38
46
  ### v1.0.0
39
47
 
data/README.md CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  Use [active_model_serializers](https://github.com/rails-api/active_model_serializers) with [Grape](https://github.com/intridea/grape)!
4
4
 
5
- [![Build Status](https://api.travis-ci.org/ruby-grape/grape-active_model_serializers.svg)](http://travis-ci.org/ruby-grape/grape-active_model_serializers) [![Dependency Status](https://gemnasium.com/ruby-grape/grape-active_model_serializers.svg)](https://gemnasium.com/ruby-grape/grape-active_model_serializers) [![Code Climate](https://codeclimate.com/github/jrhe/grape-active_model_serializers.svg)](https://codeclimate.com/github/jrhe/grape-active_model_serializers)
5
+ [![Gem Version](https://badge.fury.io/rb/grape-active_model_serializers.svg)](https://badge.fury.io/rb/grape-active_model_serializers)
6
+ [![Build Status](https://api.travis-ci.org/ruby-grape/grape-active_model_serializers.svg)](http://travis-ci.org/ruby-grape/grape-active_model_serializers) [![Dependency Status](https://gemnasium.com/ruby-grape/grape-active_model_serializers.svg)](https://gemnasium.com/ruby-grape/grape-active_model_serializers) [![Code Climate](https://codeclimate.com/github/ruby-grape/grape-active_model_serializers.svg)](https://codeclimate.com/github/ruby-grape/grape-active_model_serializers)
6
7
 
7
8
  ## Installation
8
9
 
@@ -14,6 +15,12 @@ gem 'grape-active_model_serializers'
14
15
 
15
16
  See [UPGRADING](UPGRADING.md) if you're upgrading from a previous version.
16
17
 
18
+ ## Dependencies
19
+
20
+ * >= Ruby v2.2
21
+ * >= [grape](https://github.com/intridea/grape) v0.8.0
22
+ * >= [active_model_serializers](https://github.com/rails-api/active_model_serializers) v0.10.0
23
+
17
24
  ## Usage
18
25
 
19
26
  ### Require grape-active_model_serializers
data/UPGRADING.md CHANGED
@@ -1,12 +1,145 @@
1
- ### Upgrading to v.1.4.0
1
+ ### Upgrading to v1.5.0
2
+
3
+ #### ASM v0.10.x support added, v0.9.x support dropped
4
+
5
+ [ASM](https://github.com/rails-api/active_model_serializers) introduced
6
+ breaking changes with ASM v0.10.x. Support has been introduced in v1.5.0.
7
+ If you require support for older version of ASM, please lock your Gemfile
8
+ to the relevant version.
9
+
10
+ #### Non-collection Serializer Resolution
11
+
12
+ Serializer resolution now uses the following strategy:
13
+
14
+ 1. Defined by the resource
15
+
16
+ ```
17
+ class UsersApi < Grape::Api
18
+ resource :users do
19
+ get '/:id', serializer: SpecifiedUserSerializer do
20
+ current_user
21
+ end
22
+ end
23
+ end
24
+
25
+ class User
26
+ def serializer_class
27
+ SpecifiedUserSerializer
28
+ end
29
+ end
30
+ ```
31
+
32
+ 2. Specified by options
33
+
34
+ ```
35
+ class UsersApi < Grape::Api
36
+ resource :users do
37
+ get '/:id', serializer: SpecifiedUserSerializer do
38
+ current_user
39
+ end
40
+ end
41
+ end
42
+ ```
43
+
44
+ 2. Namespace inferred
45
+
46
+ ```
47
+ class V1::UsersApi < Grape::Api
48
+ resource :users do
49
+ get '/:id' do
50
+ current_user
51
+ end
52
+ end
53
+ end
54
+ # 'V1::UsersApi'.deconstantize => 'V1'
55
+ # searches for serializers in the V1:: namespace with the name UserSerializer
56
+
57
+ # used
58
+ class V1::UserSerializer
59
+ ...
60
+ end
61
+
62
+ # not used since V1::UserSerializer resolved first
63
+ class UserSerializer
64
+ ...
65
+ end
66
+ ```
67
+
68
+ 3. Version inferred
69
+
70
+ ```
71
+ class UsersApi < Grape::Api
72
+ version 'v2'
73
+
74
+ resource :users do
75
+ get '/:id' do
76
+ current_user
77
+ end
78
+ end
79
+ end
80
+ # 'v2'.classify => V2
81
+ # searches for serializers in the V2:: namespace with the name UserSerializer
82
+
83
+ # used
84
+ class V2::UserSerializer
85
+ ...
86
+ end
87
+
88
+ # not used since V2::UserSerializer resolved first
89
+ class UserSerializer
90
+ ...
91
+ end
92
+ ```
93
+
94
+ 4. Default ASM lookup
95
+
96
+ ```
97
+ class V1::UsersApi < Grape::Api
98
+ version 'v2'
99
+
100
+ resource :users do
101
+ get '/:id' do
102
+ current_user
103
+ end
104
+ end
105
+ end
106
+ # searches for serializers in the V1:: namespace, none found
107
+ # searches for serializers in the V2:: namespace, none found
108
+
109
+ # used as no other serializers were found
110
+ class UserSerializer
111
+ ...
112
+ end
113
+ ```
114
+
115
+ #### Collection Serializer Resolution
116
+
117
+ Serializer resolution for collections also uses the above strategy, but has
118
+ the added option of overriding the member serializer if the `each_serializer`
119
+ options is specified.
120
+
121
+ ```
122
+ class UsersApi < Grape::Api
123
+ resource :users do
124
+ get each_serializer: SpecifiedUserSerializer do
125
+ User.all
126
+ end
127
+ end
128
+ end
129
+ ```
130
+
131
+
132
+ ### Upgrading to v1.4.0
2
133
 
3
134
  #### Changes in Serializer Namespacing
4
135
 
5
- Version 1.4.0 introduced changes in serializer namespacing when using Grape API versioning.
136
+ Version 1.4.0 introduced changes in serializer namespacing when using Grape
137
+ API versioning.
6
138
 
7
139
  If you haven't declared an API version in Grape, nothing changed.
8
140
 
9
- If your Grape API is versioned, which means you have declared at least one version in Grape, you must namespace your serializers accordingly.
141
+ If your Grape API is versioned, which means you have declared at least one
142
+ version in Grape, you must namespace your serializers accordingly.
10
143
 
11
144
  For example, given the following API.
12
145
 
@@ -48,7 +181,8 @@ module Chocolate
48
181
  end
49
182
  ```
50
183
 
51
- This will allow you to keep your serializers easier to maintain and more organized.
184
+ This will allow you to keep your serializers easier to maintain and more
185
+ organized.
52
186
 
53
187
  ```
54
188
  app
@@ -73,13 +207,15 @@ or alternatively:
73
207
  └── candy_bar_user_serializer.rb
74
208
  ```
75
209
 
76
- Thus, ActiveModelSerializer will fetch automatically the right serializer to render.
210
+ Thus, ActiveModelSerializer will fetch automatically the right serializer to
211
+ render.
77
212
 
78
213
  ### Upgrading to v1.0.0
79
214
 
80
215
  #### Changes to Array Roots
81
216
 
82
- When serializing an array, the array root is set to the innermost namespace name if there is one, otherwise it is set to the route name.
217
+ When serializing an array, the array root is set to the innermost namespace
218
+ name if there is one, otherwise it is set to the route name.
83
219
 
84
220
  In the following example the array root is `users`.
85
221
 
@@ -16,13 +16,13 @@ Gem::Specification.new do |gem|
16
16
  gem.version = Grape::ActiveModelSerializers::VERSION
17
17
  gem.licenses = ['MIT']
18
18
 
19
- gem.add_dependency 'grape'
20
- gem.add_dependency 'active_model_serializers', '>= 0.9.0'
19
+ gem.add_dependency 'grape', '>= 0.8.0'
20
+ gem.add_dependency 'active_model_serializers', '>= 0.10.0'
21
21
 
22
22
  gem.add_development_dependency 'listen', '~> 3.0.7'
23
23
  gem.add_development_dependency 'rspec'
24
24
  gem.add_development_dependency 'rack-test'
25
25
  gem.add_development_dependency 'rake'
26
26
  gem.add_development_dependency 'guard-rspec'
27
- gem.add_development_dependency 'rubocop', '0.28.0'
27
+ gem.add_development_dependency 'rubocop', '0.41.2'
28
28
  end
@@ -2,4 +2,6 @@ require 'active_model_serializers'
2
2
  require 'grape'
3
3
  require 'grape-active_model_serializers/endpoint_extension'
4
4
  require 'grape-active_model_serializers/formatter'
5
+ require 'grape-active_model_serializers/serializer_resolver'
6
+ require 'grape-active_model_serializers/options_builder'
5
7
  require 'grape-active_model_serializers/version'
@@ -9,7 +9,7 @@ module Grape
9
9
  attr_accessor :controller_name
10
10
 
11
11
  def namespace_options
12
- if self.respond_to?(:inheritable_setting)
12
+ if respond_to?(:inheritable_setting)
13
13
  inheritable_setting.namespace
14
14
  else
15
15
  settings[:namespace] ? settings[:namespace].options : {}
@@ -17,7 +17,7 @@ module Grape
17
17
  end
18
18
 
19
19
  def route_options
20
- if self.respond_to?(:inheritable_setting)
20
+ if respond_to?(:inheritable_setting)
21
21
  inheritable_setting.route
22
22
  else
23
23
  options[:route_options]
@@ -30,7 +30,9 @@ module Grape
30
30
 
31
31
  base.class_eval do
32
32
  def serialization_scope
33
- send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true)
33
+ return unless _serialization_scope
34
+ return unless respond_to?(_serialization_scope, true)
35
+ send(_serialization_scope)
34
36
  end
35
37
  end
36
38
  end
@@ -3,60 +3,28 @@ module Grape
3
3
  module ActiveModelSerializers
4
4
  class << self
5
5
  def call(resource, env)
6
- serializer = fetch_serializer(resource, env)
6
+ options = build_options(resource, env)
7
+ serializer = fetch_serializer(resource, options)
7
8
 
8
9
  if serializer
9
- serializer.to_json
10
+ ::ActiveModelSerializers::Adapter.create(
11
+ serializer, options
12
+ ).to_json
10
13
  else
11
- Grape::Formatter::Json.call resource, env
14
+ Grape::Formatter::Json.call(resource, env)
12
15
  end
13
16
  end
14
17
 
15
- def fetch_serializer(resource, env)
16
- endpoint = env['api.endpoint']
17
- options = build_options_from_endpoint(endpoint)
18
- ams_options = {}.tap do |ns|
19
- # Extracting declared version from Grape
20
- ns[:namespace] = options[:version].try(:classify) if options.try(:[], :version)
21
- end
22
-
23
- serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource, ams_options))
24
- return nil unless serializer
25
-
26
- options[:scope] = endpoint unless options.key?(:scope)
27
- # ensure we have an root to fallback on
28
- options[:resource_name] = default_root(endpoint) if resource.respond_to?(:to_ary)
29
- serializer.new(resource, options.merge(other_options(env)))
30
- end
31
-
32
- def other_options(env)
33
- options = {}
34
- ams_meta = env['ams_meta'] || {}
35
- meta = ams_meta.delete(:meta)
36
- meta_key = ams_meta.delete(:meta_key)
37
- options[:meta_key] = meta_key if meta && meta_key
38
- options[meta_key || :meta] = meta if meta
39
- options
18
+ def build_options(resource, env)
19
+ Grape::ActiveModelSerializers::OptionsBuilder.new(
20
+ resource, env
21
+ ).options
40
22
  end
41
23
 
42
- def build_options_from_endpoint(endpoint)
43
- [endpoint.default_serializer_options || {}, endpoint.namespace_options, endpoint.route_options, endpoint.options, endpoint.options.fetch(:route_options)].reduce(:merge)
44
- end
45
-
46
- # array root is the innermost namespace name ('space') if there is one,
47
- # otherwise the route name (e.g. get 'name')
48
- def default_root(endpoint)
49
- innermost_scope = if endpoint.respond_to?(:namespace_stackable)
50
- endpoint.namespace_stackable(:namespace).last
51
- else
52
- endpoint.settings.peek[:namespace]
53
- end
54
-
55
- if innermost_scope
56
- innermost_scope.space
57
- else
58
- endpoint.options[:path][0].to_s.split('/')[-1]
59
- end
24
+ def fetch_serializer(resource, options)
25
+ Grape::ActiveModelSerializers::SerializerResolver.new(
26
+ resource, options
27
+ ).serializer
60
28
  end
61
29
  end
62
30
  end
@@ -0,0 +1,68 @@
1
+ module Grape
2
+ module ActiveModelSerializers
3
+ class OptionsBuilder
4
+ def initialize(resource, env)
5
+ self.resource = resource
6
+ self.env = env
7
+ end
8
+
9
+ def options
10
+ @options ||= (
11
+ options = endpoint_options
12
+ options[:scope] = endpoint unless options.key?(:scope)
13
+ options.merge!(default_root_options) unless options.key?(:root)
14
+ options.merge!(meta_options)
15
+ options
16
+ )
17
+ end
18
+
19
+ private
20
+
21
+ attr_accessor :resource, :env
22
+
23
+ def endpoint_options
24
+ [
25
+ endpoint.default_serializer_options || {},
26
+ endpoint.namespace_options,
27
+ endpoint.route_options,
28
+ endpoint.options,
29
+ endpoint.options.fetch(:route_options)
30
+ ].reduce(:merge)
31
+ end
32
+
33
+ def endpoint
34
+ @endpoint ||= env['api.endpoint']
35
+ end
36
+
37
+ # array root is the innermost namespace name ('space') if there is one,
38
+ # otherwise the route name (e.g. get 'name')
39
+ def default_root_options
40
+ return {} unless resource.respond_to?(:to_ary)
41
+
42
+ if innermost_scope
43
+ { root: innermost_scope.space }
44
+ else
45
+ { root: endpoint.options[:path].first.to_s.split('/').last }
46
+ end
47
+ end
48
+
49
+ def innermost_scope
50
+ if endpoint.respond_to?(:namespace_stackable)
51
+ endpoint.namespace_stackable(:namespace).last
52
+ else
53
+ endpoint.settings.peek[:namespace]
54
+ end
55
+ end
56
+
57
+ def meta_options
58
+ options = {}
59
+ ams_meta = env['ams_meta'] || {}
60
+ meta = ams_meta[:meta]
61
+ meta_key = ams_meta[:meta_key]
62
+ options[:meta] = meta if meta
63
+ options[:meta_key] = meta_key if meta && meta_key
64
+ options
65
+ end
66
+ end
67
+ end
68
+ end