active_model_serializers 0.10.6 → 0.10.7

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.
Files changed (36) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +28 -31
  3. data/CHANGELOG.md +47 -1
  4. data/Gemfile +20 -4
  5. data/README.md +1 -3
  6. data/active_model_serializers.gemspec +1 -1
  7. data/appveyor.yml +4 -6
  8. data/docs/general/adapters.md +6 -0
  9. data/docs/general/configuration_options.md +16 -0
  10. data/docs/general/rendering.md +1 -1
  11. data/docs/general/serializers.md +4 -1
  12. data/docs/howto/add_root_key.md +7 -0
  13. data/lib/active_model/serializer.rb +2 -1
  14. data/lib/active_model/serializer/collection_serializer.rb +4 -1
  15. data/lib/active_model/serializer/reflection.rb +1 -1
  16. data/lib/active_model/serializer/version.rb +1 -1
  17. data/lib/active_model_serializers.rb +1 -1
  18. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
  19. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +40 -21
  20. data/lib/active_model_serializers/adapter/json_api/relationship.rb +16 -4
  21. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +31 -25
  22. data/test/action_controller/json_api/deserialization_test.rb +1 -1
  23. data/test/action_controller/json_api/pagination_test.rb +14 -6
  24. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +32 -2
  25. data/test/adapter/json_api/pagination_links_test.rb +20 -7
  26. data/test/adapter/json_api/parse_test.rb +1 -1
  27. data/test/adapter/json_api/type_test.rb +168 -36
  28. data/test/adapter/polymorphic_test.rb +47 -0
  29. data/test/collection_serializer_test.rb +6 -2
  30. data/test/fixtures/active_record.rb +2 -2
  31. data/test/serializers/associations_test.rb +45 -1
  32. data/test/support/isolated_unit.rb +4 -2
  33. data/test/support/serialization_testing.rb +8 -0
  34. metadata +37 -41
  35. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  36. data/test/adapter/json_api/resource_identifier_test.rb +0 -110
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fbc0f811b036c328dccf411b6f8080b6605d8740
4
- data.tar.gz: d4a0f6f7799b074aff45bc103c9c19b9ea873ab1
2
+ SHA256:
3
+ metadata.gz: 0c3a5900ed1c671e57b90ea00e3130ad3a5fe4939a6a1c36e1481dbdeeef3199
4
+ data.tar.gz: 59ed63aa89861c650f87ba5bfeb5dfc90c997cd8fe4542129f28d1194d3c24ff
5
5
  SHA512:
6
- metadata.gz: 121ed054788e585e48e76af3e98490930fd26f18ebdd28d80d07cba78fe9b7c3aed8ea1e1f15ace59debb73562383657f89e4d52fa2c54391424ea53c3701a38
7
- data.tar.gz: 88ab9022b49348ec463547196300480bcf02139d67cc11adfcbf8c5e4e38c3e44b27cdde7687baab54f8f8ce4bbe4fa606079dffae51ad7be42427dc15955d6e
6
+ metadata.gz: 0ee2569a69953e286d63861bb01a37836ee9b25463980da1dc561815e0d7e7902aa1b1d344b265bb50f3c41b1e93ea6cfec2ba241c5314511d4a55a5082f5ce0
7
+ data.tar.gz: d011e2d950d8b7869d4f49139d23cf994f2b1f6e8319aa2a3ff018d4f1c578d24bb64cfe632c912e91edd92e0f5945fa58a1a7742b611ef33c5a4d0a14dfbc70
@@ -1,55 +1,52 @@
1
1
  language: ruby
2
-
3
2
  sudo: false
4
3
 
5
- rvm:
6
- - 2.1
7
- - 2.2.6
8
- - 2.3.3
9
- - ruby-head
10
- - jruby-9.1.5.0 # is precompiled per http://rubies.travis-ci.org/
11
- - jruby-head
12
-
13
- jdk:
14
- - oraclejdk8
15
-
16
- before_install:
17
- - gem update --system
18
- - rvm @global do gem uninstall bundler -a -x
19
- - rvm @global do gem install bundler -v 1.13.7
20
- install: bundle install --path=vendor/bundle --retry=3 --jobs=3
21
4
  cache:
22
5
  directories:
23
6
  - vendor/bundle
24
7
 
8
+ before_install:
9
+ - "travis_retry gem update --system"
10
+ - "travis_retry gem update bundler"
11
+ install: bundle install --path=vendor/bundle --retry=3 --jobs=3
12
+
25
13
  script:
26
14
  - bundle exec rake ci
27
15
  after_success:
28
16
  - codeclimate-test-reporter
17
+
29
18
  env:
30
- global:
31
- - "JRUBY_OPTS='--dev -J-Xmx1024M --debug'"
32
19
  matrix:
33
20
  - "RAILS_VERSION=4.1"
34
21
  - "RAILS_VERSION=4.2"
35
22
  - "RAILS_VERSION=5.0"
23
+ - "RAILS_VERSION=5.1"
36
24
  - "RAILS_VERSION=master"
37
25
 
26
+ rvm:
27
+ - 2.1.10
28
+ - 2.2.8
29
+ - 2.3.5
30
+ - 2.4.2
31
+ - ruby-head
32
+
38
33
  matrix:
34
+ include:
35
+ - { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=4.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
36
+ - { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=4.2 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
37
+ - { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=5.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
38
+ # See JRuby currently failing on Rails 5+ https://github.com/jruby/activerecord-jdbc-adapter/issues/708
39
+ # - { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=5.0 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
40
+ # - { rvm: jruby-head, jdk: oraclejdk8, env: "RAILS_VERSION=5.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
39
41
  exclude:
40
- - rvm: 2.1
41
- env: RAILS_VERSION=master
42
- - rvm: jruby-9.1.5.0
43
- env: RAILS_VERSION=master
44
- - rvm: jruby-head
45
- env: RAILS_VERSION=master
46
- - rvm: 2.1
47
- env: RAILS_VERSION=5.0
48
- - rvm: jruby-9.1.5.0
49
- env: RAILS_VERSION=5.0
50
- - rvm: jruby-head
51
- env: RAILS_VERSION=5.0
42
+ - { rvm: 2.1.10, env: RAILS_VERSION=master }
43
+ - { rvm: 2.1.10, env: RAILS_VERSION=5.0 }
44
+ - { rvm: 2.1.10, env: RAILS_VERSION=5.1 }
45
+ - { rvm: 2.4.2, env: RAILS_VERSION=4.1 }
46
+ - { rvm: ruby-head, env: RAILS_VERSION=4.1 }
52
47
  allow_failures:
53
48
  - rvm: ruby-head
54
49
  - rvm: jruby-head
50
+ # See JRuby currently failing on Rails 5+ https://github.com/jruby/activerecord-jdbc-adapter/issues/708
51
+ - { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=5.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
55
52
  fast_finish: true
@@ -1,6 +1,6 @@
1
1
  ## 0.10.x
2
2
 
3
- ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.6...master)
3
+ ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.7...0-10-stable)
4
4
 
5
5
  Breaking changes:
6
6
 
@@ -10,6 +10,52 @@ Fixes:
10
10
 
11
11
  Misc:
12
12
 
13
+ ### [v0.10.7 (2017-11-14)](https://github.com/rails-api/active_model_serializers/compare/v0.10.6...v0.10.7)
14
+
15
+ Regressions Fixed From v0.10.6:
16
+
17
+ - [#2211](https://github.com/rails-api/active_model_serializers/pull/2211). Fixes #2125, #2160. (@bf4)
18
+ - Fix polymorphic belongs_to tests; passes on v0.10.5, fails on v0.10.6
19
+ - Fix JSON:API polymorphic type regression from v0.10.5
20
+ - Fix JSON:API: for_type_and_id should always inflect_type
21
+ ```
22
+ Should Serializer._type ever be inflected?
23
+ Right now, it won't be, but association.serializer._type will be inflected.
24
+
25
+ That's the current behavior.
26
+ ```
27
+ - [#2216](https://github.com/rails-api/active_model_serializers/pull/2216). Fixes #2132, #2180. (@bf4)
28
+ - Fix JSON:API: Serialize resource type for unpersisted records (blank id)
29
+ - [#2218](https://github.com/rails-api/active_model_serializers/pull/2218). Fixes #2178. (@bf4)
30
+ - Fix JSON:API: Make using foreign key on belongs_to opt-in. No effect on polymorphic relationships.
31
+ ```
32
+ # set to true to opt-in
33
+ ActiveModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
34
+ ```
35
+
36
+ Features:
37
+
38
+ - [#2136](https://github.com/rails-api/active_model_serializers/pull/2136) Enable inclusion of sideloaded relationship objects by `key`. (@caomania)
39
+ - [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) ActiveModelSerializers::Model#attributes. Originally in [#1982](https://github.com/rails-api/active_model_serializers/pull/1982). (@bf4)
40
+ - [#2130](https://github.com/rails-api/active_model_serializers/pull/2130) Allow serialized ID to be overwritten for belongs-to relationships. (@greysteil)
41
+ - [#2189](https://github.com/rails-api/active_model_serializers/pull/2189)
42
+ Update version constraint for jsonapi-renderer to `['>= 0.1.1.beta1', '< 0.3']`
43
+ (@tagliala)
44
+
45
+ Fixes:
46
+
47
+ - [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4)
48
+ - [#2200](https://github.com/rails-api/active_model_serializers/pull/2200) Fix deserialization of polymorphic relationships. (@dennis95stumm)
49
+ - [#2214](https://github.com/rails-api/active_model_serializers/pull/2214) Fail if unable to infer collection type with json adapter. (@jmeredith16)
50
+ - [#2149](https://github.com/rails-api/active_model_serializers/pull/2149) Always include self, first, last pagination link. (@mecampbellsoup)
51
+ - [#2179](https://github.com/rails-api/active_model_serializers/pull/2179) Fixes #2173, Pass block to Enumerator.new. (@drn)
52
+
53
+ Misc:
54
+
55
+ - [#2176](https://github.com/rails-api/active_model_serializers/pull/2176) Documentation for global adapter config. (@mrpinsky)
56
+ - [#2215](https://github.com/rails-api/active_model_serializers/pull/2215) Update `serializers.md` documentation to denote alternate use cases for `scope`. (@stratigos)
57
+ - [#2212](https://github.com/rails-api/active_model_serializers/pull/2212) Remove legacy has_many_embed_ids test. (@bf4)
58
+
13
59
  ### [v0.10.6 (2017-05-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.5...v0.10.6)
14
60
 
15
61
  Fixes:
data/Gemfile CHANGED
@@ -12,6 +12,7 @@ version = ENV['RAILS_VERSION'] || '4.2'
12
12
  if version == 'master'
13
13
  gem 'rack', github: 'rack/rack'
14
14
  gem 'arel', github: 'rails/arel'
15
+ gem 'rails', github: 'rails/rails'
15
16
  git 'https://github.com/rails/rails.git' do
16
17
  gem 'railties'
17
18
  gem 'activesupport'
@@ -23,6 +24,7 @@ if version == 'master'
23
24
  end
24
25
  else
25
26
  gem_version = "~> #{version}.0"
27
+ gem 'rails', gem_version
26
28
  gem 'railties', gem_version
27
29
  gem 'activesupport', gem_version
28
30
  gem 'activemodel', gem_version
@@ -36,18 +38,32 @@ end
36
38
  # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
37
39
  gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
38
40
 
41
+ if ENV['CI']
42
+ if RUBY_VERSION < '2.4'
43
+ # Windows: An error occurred while installing nokogiri (1.8.0)
44
+ gem 'nokogiri', '< 1.7', platforms: @windows_platforms
45
+ end
46
+ end
47
+
39
48
  group :bench do
40
49
  # https://github.com/rails-api/active_model_serializers/commit/cb4459580a6f4f37f629bf3185a5224c8624ca76
41
50
  gem 'benchmark-ips', '>= 2.7.2', require: false, group: :development
42
51
  end
43
52
 
44
53
  group :test do
45
- gem 'sqlite3', platform: (@windows_platforms + [:ruby])
46
- gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
54
+ gem 'sqlite3', platform: (@windows_platforms + [:ruby])
55
+ platforms :jruby do
56
+ if version == 'master' || version >= '5'
57
+ gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.0' # github: 'jruby/activerecord-jdbc-adapter', branch: 'master'
58
+ else
59
+ gem 'activerecord-jdbcsqlite3-adapter'
60
+ end
61
+ end
47
62
  gem 'codeclimate-test-reporter', require: false
48
63
  gem 'm', '~> 1.5'
49
- gem 'pry', '~> 0.10'
50
- gem 'pry-byebug', '~> 3.4', platform: :ruby
64
+ gem 'pry', '>= 0.10'
65
+ gem 'byebug', '~> 8.2' if RUBY_VERSION < '2.2'
66
+ gem 'pry-byebug', platform: :ruby
51
67
  end
52
68
 
53
69
  group :development, :test do
data/README.md CHANGED
@@ -49,8 +49,6 @@ Check how to change the adapter in the sections below.
49
49
  `0.10.x` is based on the `0.8.0` code, but with a more flexible
50
50
  architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md)
51
51
 
52
- It is generally safe and recommended to use the master branch.
53
-
54
52
  ## Installation
55
53
 
56
54
  Add this line to your application's Gemfile:
@@ -187,7 +185,7 @@ and, if there is no serializer, primitives.
187
185
 
188
186
  ### ActiveModelSerializers::Adapter::Base
189
187
 
190
- The **`ActiveModelSerializeres::Adapter::Base`** describes the structure of the JSON document generated from a
188
+ The **`ActiveModelSerializers::Adapter::Base`** describes the structure of the JSON document generated from a
191
189
  serializer. For example, the `Attributes` example represents each serializer as its
192
190
  unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
193
191
  API](http://jsonapi.org/) document.
@@ -42,7 +42,7 @@ Gem::Specification.new do |spec|
42
42
  # 'minitest'
43
43
  # 'thread_safe'
44
44
 
45
- spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.2']
45
+ spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.3']
46
46
  spec.add_runtime_dependency 'case_transform', '>= 0.2'
47
47
 
48
48
  spec.add_development_dependency 'activerecord', rails_versions
@@ -5,19 +5,17 @@ skip_tags: true
5
5
  environment:
6
6
  JRUBY_OPTS: "--dev -J-Xmx1024M --debug"
7
7
  matrix:
8
- - ruby_version: "Ruby21"
9
- - ruby_version: "Ruby21-x64"
8
+ - ruby_version: "Ruby23"
9
+ - ruby_version: "Ruby23-x64"
10
10
 
11
11
  cache:
12
12
  - vendor/bundle
13
13
 
14
14
  install:
15
15
  - SET PATH=C:\%ruby_version%\bin;%PATH%
16
- - gem update --system
17
- - gem uninstall bundler -a -x
18
- - gem install bundler -v 1.13.7
19
16
  - bundle env
20
- - bundle install --path=vendor/bundle --retry=3 --jobs=3
17
+ - bundle check || bundle install --path=vendor/bundle --retry=3 --jobs=3
18
+ - bundle clean --force
21
19
 
22
20
  before_test:
23
21
  - ruby -v
@@ -36,6 +36,12 @@ The `Attributes` adapter does not include a root key. It is just the serialized
36
36
 
37
37
  Use either the `JSON` or `JSON API` adapters if you want the response document to have a root key.
38
38
 
39
+ ***IMPORTANT***: Adapter configuration has *no effect* on a serializer instance
40
+ being used directly. That is, `UserSerializer.new(user).as_json` will *always*
41
+ behave as if the adapter were the 'Attributes' adapter. See [Outside Controller
42
+ Usage](../howto/outside_controller_use.md) for more details on recommended
43
+ usage.
44
+
39
45
  ## Built in Adapters
40
46
 
41
47
  ### Attributes - Default
@@ -57,6 +57,7 @@ still prefer the render option `:key_transform` over this setting.
57
57
  application, setting `config.key_transform` to `:unaltered` will provide a performance boost.*
58
58
 
59
59
  ##### default_includes
60
+
60
61
  What relationships to serialize by default. Default: `'*'`, which includes one level of related
61
62
  objects. See [includes](adapters.md#included) for more info.
62
63
 
@@ -162,6 +163,21 @@ Default: `{}`.
162
163
 
163
164
  *Used when `jsonapi_include_toplevel_object` is `true`*
164
165
 
166
+ ##### jsonapi_use_foreign_key_on_belongs_to_relationship
167
+
168
+ When true, the relationship will determine its resource object identifier
169
+ without calling the association or its serializer. This can be useful when calling
170
+ the association object is triggering unnecessary queries.
171
+
172
+ For example, if a `comment` belongs to a `post`, and the comment
173
+ uses the foreign key `post_id`, we can determine the resource object
174
+ identifier `id` as `comment.post_id` and the `type` from the association options.
175
+ Or quite simply, it behaves as `belongs_to :post, type: :posts, foreign_key: :post_id`.
176
+
177
+ Note: This option has *no effect* on polymorphic associations as we cannot reliably
178
+ determine the associated object's type without instantiating it.
179
+
180
+ Default: `false`.
165
181
 
166
182
  ## Hooks
167
183
 
@@ -290,4 +290,4 @@ See [Usage outside of a controller](../howto/outside_controller_use.md#serializi
290
290
 
291
291
  ## Pagination
292
292
 
293
- See [How to add pagination links](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md).
293
+ See [How to add pagination links](../howto/add_pagination_links.md).
@@ -294,6 +294,8 @@ end
294
294
  Whether you write the method as above or as `object.comments.where(created_by: scope)`
295
295
  is a matter of preference (assuming `scope_name` has been set).
296
296
 
297
+ Keep in mind that the scope can be set to any available controller reference. This can be utilized to provide access to any other data scopes or presentation helpers.
298
+
297
299
  ##### Controller Authorization Context
298
300
 
299
301
  In the controller, the scope/scope_name options are equal to
@@ -311,7 +313,7 @@ current authorization scope when you call `render :json`.
311
313
  called on every request. This was [also a problem](https://github.com/rails-api/active_model_serializers/pull/1252#issuecomment-159810477)
312
314
  in [`0.9`](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope).
313
315
 
314
- We can change the scope from `current_user` to `view_context`.
316
+ We can change the scope from `current_user` to `view_context`, which is included in subclasses of `ActionController::Base`.
315
317
 
316
318
  ```diff
317
319
  class SomeController < ActionController::Base
@@ -379,6 +381,7 @@ class PostsController < ActionController::Base
379
381
  end
380
382
  end
381
383
  ```
384
+ Note that any controller reference which provides the desired scope is acceptable, such as another controller method for loading a different resource or reference to helpers. For example, `ActionController::API` does not include `ActionView::ViewContext`, and would need a different reference for passing any helpers into a serializer via `serialization_scope`.
382
385
 
383
386
  #### #read_attribute_for_serialization(key)
384
387
 
@@ -18,6 +18,13 @@ In order to add the root key you need to use the ```JSON``` Adapter, you can cha
18
18
  ActiveModelSerializers.config.adapter = :json
19
19
  ```
20
20
 
21
+ Note that adapter configuration has no effect on a serializer that is called
22
+ directly, e.g. in a serializer unit test. Instead, something like
23
+ `UserSerializer.new(user).as_json` will *always* behave as if the adapter were
24
+ the 'Attributes' adapter. See [Outside Controller
25
+ Usage](../howto/outside_controller_use.md) for more details on recommended
26
+ usage.
27
+
21
28
  You can also specify a class as adapter, as long as it complies with the ActiveModelSerializers adapters interface.
22
29
  It will add the root key to all your serialized endpoints.
23
30
 
@@ -142,6 +142,7 @@ module ActiveModel
142
142
  # Make JSON API top-level jsonapi member opt-in
143
143
  # ref: http://jsonapi.org/format/#document-top-level
144
144
  config.jsonapi_include_toplevel_object = false
145
+ config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
145
146
  config.include_data_default = true
146
147
 
147
148
  # For configuring how serializers are found.
@@ -337,7 +338,7 @@ module ActiveModel
337
338
  # @return [Enumerator<Association>]
338
339
  def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
339
340
  include_slice ||= include_directive
340
- return Enumerator.new unless object
341
+ return Enumerator.new {} unless object
341
342
 
342
343
  Enumerator.new do |y|
343
344
  self.class._reflections.each do |key, reflection|
@@ -46,7 +46,10 @@ module ActiveModel
46
46
  # 3. get from collection name, if a named collection
47
47
  key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
48
48
  # 4. key may be nil for empty collection and no serializer option
49
- key && key.pluralize
49
+ key &&= key.pluralize
50
+ # 5. fail if the key cannot be determined
51
+ key || fail(ArgumentError, 'Cannot infer root key from collection type. Please
52
+ specify the root or each_serializer option, or render a JSON String')
50
53
  end
51
54
  # rubocop:enable Metrics/CyclomaticComplexity
52
55
 
@@ -140,7 +140,7 @@ module ActiveModel
140
140
  def include_data?(include_slice)
141
141
  include_data_setting = options[:include_data_setting]
142
142
  case include_data_setting
143
- when :if_sideloaded then include_slice.key?(name)
143
+ when :if_sideloaded then include_slice.key?(options.fetch(:key, name))
144
144
  when true then true
145
145
  when false then false
146
146
  else fail ArgumentError, "Unknown include_data_setting '#{include_data_setting.inspect}'"
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Serializer
3
- VERSION = '0.10.6'.freeze
3
+ VERSION = '0.10.7'.freeze
4
4
  end
5
5
  end
@@ -49,5 +49,5 @@ module ActiveModelSerializers
49
49
  require 'active_model/serializer/version'
50
50
  require 'active_model/serializer'
51
51
  require 'active_model/serializable_resource'
52
- require 'active_model_serializers/railtie' if defined?(::Rails)
52
+ require 'active_model_serializers/railtie' if defined?(::Rails::Railtie)
53
53
  end
@@ -189,7 +189,7 @@ module ActiveModelSerializers
189
189
 
190
190
  polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym)
191
191
  if polymorphic
192
- hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'] : nil
192
+ hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'].classify : nil
193
193
  end
194
194
 
195
195
  hash
@@ -15,17 +15,18 @@ module ActiveModelSerializers
15
15
  JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext.
16
16
  Please pass a ':serialization_context' option or
17
17
  override CollectionSerializer#paginated? to return 'false'.
18
- EOF
18
+ EOF
19
19
  end
20
20
  end
21
21
 
22
22
  def as_json
23
- per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
24
- pages_from.each_with_object({}) do |(key, value), hash|
25
- params = query_parameters.merge(page: { number: value, size: per_page }).to_query
26
-
27
- hash[key] = "#{url(adapter_options)}?#{params}"
28
- end
23
+ {
24
+ self: location_url,
25
+ first: first_page_url,
26
+ prev: prev_page_url,
27
+ next: next_page_url,
28
+ last: last_page_url
29
+ }
29
30
  end
30
31
 
31
32
  protected
@@ -34,25 +35,39 @@ module ActiveModelSerializers
34
35
 
35
36
  private
36
37
 
37
- def pages_from
38
- return {} if collection.total_pages <= FIRST_PAGE
39
-
40
- {}.tap do |pages|
41
- pages[:self] = collection.current_page
38
+ def location_url
39
+ url_for_page(collection.current_page)
40
+ end
42
41
 
43
- unless collection.current_page == FIRST_PAGE
44
- pages[:first] = FIRST_PAGE
45
- pages[:prev] = collection.current_page - FIRST_PAGE
46
- end
42
+ def first_page_url
43
+ url_for_page(1)
44
+ end
47
45
 
48
- unless collection.current_page == collection.total_pages
49
- pages[:next] = collection.current_page + FIRST_PAGE
50
- pages[:last] = collection.total_pages
51
- end
46
+ def last_page_url
47
+ if collection.total_pages == 0
48
+ url_for_page(FIRST_PAGE)
49
+ else
50
+ url_for_page(collection.total_pages)
52
51
  end
53
52
  end
54
53
 
55
- def url(options)
54
+ def prev_page_url
55
+ return nil if collection.current_page == FIRST_PAGE
56
+ url_for_page(collection.current_page - FIRST_PAGE)
57
+ end
58
+
59
+ def next_page_url
60
+ return nil if collection.total_pages == 0 || collection.current_page == collection.total_pages
61
+ url_for_page(collection.next_page)
62
+ end
63
+
64
+ def url_for_page(number)
65
+ params = query_parameters.dup
66
+ params[:page] = { size: per_page, number: number }
67
+ "#{url(adapter_options)}?#{params.to_query}"
68
+ end
69
+
70
+ def url(options = {})
56
71
  @url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
57
72
  end
58
73
 
@@ -63,6 +78,10 @@ module ActiveModelSerializers
63
78
  def query_parameters
64
79
  @query_parameters ||= context.query_parameters
65
80
  end
81
+
82
+ def per_page
83
+ @per_page ||= collection.try(:per_page) || collection.try(:limit_value) || collection.size
84
+ end
66
85
  end
67
86
  end
68
87
  end