active_model_serializers 0.10.7 → 0.10.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -0
  3. data/.travis.yml +14 -3
  4. data/CHANGELOG.md +29 -1
  5. data/Gemfile +5 -3
  6. data/README.md +2 -2
  7. data/Rakefile +2 -0
  8. data/active_model_serializers.gemspec +2 -1
  9. data/docs/general/serializers.md +16 -4
  10. data/lib/action_controller/serialization.rb +2 -0
  11. data/lib/active_model/serializable_resource.rb +2 -0
  12. data/lib/active_model/serializer/adapter/attributes.rb +2 -0
  13. data/lib/active_model/serializer/adapter/base.rb +2 -0
  14. data/lib/active_model/serializer/adapter/json.rb +2 -0
  15. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  16. data/lib/active_model/serializer/adapter/null.rb +2 -0
  17. data/lib/active_model/serializer/adapter.rb +2 -0
  18. data/lib/active_model/serializer/array_serializer.rb +2 -0
  19. data/lib/active_model/serializer/association.rb +2 -0
  20. data/lib/active_model/serializer/attribute.rb +2 -0
  21. data/lib/active_model/serializer/belongs_to_reflection.rb +2 -0
  22. data/lib/active_model/serializer/collection_serializer.rb +6 -6
  23. data/lib/active_model/serializer/concerns/caching.rb +5 -1
  24. data/lib/active_model/serializer/error_serializer.rb +2 -0
  25. data/lib/active_model/serializer/errors_serializer.rb +2 -0
  26. data/lib/active_model/serializer/field.rb +2 -0
  27. data/lib/active_model/serializer/fieldset.rb +2 -0
  28. data/lib/active_model/serializer/has_many_reflection.rb +2 -0
  29. data/lib/active_model/serializer/has_one_reflection.rb +2 -0
  30. data/lib/active_model/serializer/lazy_association.rb +6 -2
  31. data/lib/active_model/serializer/link.rb +23 -0
  32. data/lib/active_model/serializer/lint.rb +2 -0
  33. data/lib/active_model/serializer/null.rb +2 -0
  34. data/lib/active_model/serializer/reflection.rb +5 -0
  35. data/lib/active_model/serializer/version.rb +3 -1
  36. data/lib/active_model/serializer.rb +22 -14
  37. data/lib/active_model_serializers/adapter/attributes.rb +2 -0
  38. data/lib/active_model_serializers/adapter/base.rb +2 -0
  39. data/lib/active_model_serializers/adapter/json.rb +2 -0
  40. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +2 -0
  41. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  42. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  43. data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
  44. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  45. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +2 -0
  46. data/lib/active_model_serializers/adapter/json_api/relationship.rb +2 -0
  47. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +2 -0
  48. data/lib/active_model_serializers/adapter/json_api.rb +14 -9
  49. data/lib/active_model_serializers/adapter/null.rb +2 -0
  50. data/lib/active_model_serializers/adapter.rb +3 -1
  51. data/lib/active_model_serializers/callbacks.rb +2 -0
  52. data/lib/active_model_serializers/deprecate.rb +2 -0
  53. data/lib/active_model_serializers/deserialization.rb +2 -0
  54. data/lib/active_model_serializers/json_pointer.rb +2 -0
  55. data/lib/active_model_serializers/logging.rb +2 -0
  56. data/lib/active_model_serializers/lookup_chain.rb +2 -0
  57. data/lib/active_model_serializers/model.rb +2 -0
  58. data/lib/active_model_serializers/railtie.rb +4 -0
  59. data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
  60. data/lib/active_model_serializers/serializable_resource.rb +4 -2
  61. data/lib/active_model_serializers/serialization_context.rb +2 -0
  62. data/lib/active_model_serializers/test/schema.rb +2 -0
  63. data/lib/active_model_serializers/test/serializer.rb +2 -0
  64. data/lib/active_model_serializers/test.rb +2 -0
  65. data/lib/active_model_serializers.rb +20 -10
  66. data/lib/generators/rails/resource_override.rb +2 -0
  67. data/lib/generators/rails/serializer_generator.rb +2 -0
  68. data/lib/grape/active_model_serializers.rb +2 -0
  69. data/lib/grape/formatters/active_model_serializers.rb +2 -0
  70. data/lib/grape/helpers/active_model_serializers.rb +2 -0
  71. data/lib/tasks/rubocop.rake +2 -0
  72. data/test/action_controller/adapter_selector_test.rb +2 -0
  73. data/test/action_controller/explicit_serializer_test.rb +2 -0
  74. data/test/action_controller/json/include_test.rb +2 -0
  75. data/test/action_controller/json_api/deserialization_test.rb +2 -0
  76. data/test/action_controller/json_api/errors_test.rb +2 -0
  77. data/test/action_controller/json_api/fields_test.rb +2 -0
  78. data/test/action_controller/json_api/linked_test.rb +2 -0
  79. data/test/action_controller/json_api/pagination_test.rb +2 -0
  80. data/test/action_controller/json_api/transform_test.rb +2 -0
  81. data/test/action_controller/lookup_proc_test.rb +2 -0
  82. data/test/action_controller/namespace_lookup_test.rb +2 -0
  83. data/test/action_controller/serialization_scope_name_test.rb +2 -0
  84. data/test/action_controller/serialization_test.rb +11 -3
  85. data/test/active_model_serializers/adapter_for_test.rb +2 -0
  86. data/test/active_model_serializers/json_pointer_test.rb +2 -0
  87. data/test/active_model_serializers/logging_test.rb +2 -0
  88. data/test/active_model_serializers/model_test.rb +2 -0
  89. data/test/active_model_serializers/railtie_test_isolated.rb +2 -0
  90. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +2 -0
  91. data/test/active_model_serializers/serialization_context_test_isolated.rb +2 -0
  92. data/test/active_model_serializers/test/schema_test.rb +2 -0
  93. data/test/active_model_serializers/test/serializer_test.rb +2 -0
  94. data/test/active_record_test.rb +2 -0
  95. data/test/adapter/attributes_test.rb +2 -0
  96. data/test/adapter/deprecation_test.rb +2 -0
  97. data/test/adapter/json/belongs_to_test.rb +2 -0
  98. data/test/adapter/json/collection_test.rb +2 -0
  99. data/test/adapter/json/has_many_test.rb +2 -0
  100. data/test/adapter/json/transform_test.rb +2 -0
  101. data/test/adapter/json_api/belongs_to_test.rb +2 -0
  102. data/test/adapter/json_api/collection_test.rb +2 -0
  103. data/test/adapter/json_api/errors_test.rb +2 -0
  104. data/test/adapter/json_api/fields_test.rb +2 -0
  105. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -0
  106. data/test/adapter/json_api/has_many_test.rb +2 -0
  107. data/test/adapter/json_api/has_one_test.rb +2 -0
  108. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +2 -0
  109. data/test/adapter/json_api/json_api_test.rb +2 -0
  110. data/test/adapter/json_api/linked_test.rb +2 -0
  111. data/test/adapter/json_api/links_test.rb +18 -1
  112. data/test/adapter/json_api/pagination_links_test.rb +2 -0
  113. data/test/adapter/json_api/parse_test.rb +2 -0
  114. data/test/adapter/json_api/relationship_test.rb +2 -0
  115. data/test/adapter/json_api/resource_meta_test.rb +2 -0
  116. data/test/adapter/json_api/toplevel_jsonapi_test.rb +2 -0
  117. data/test/adapter/json_api/transform_test.rb +2 -0
  118. data/test/adapter/json_api/type_test.rb +2 -0
  119. data/test/adapter/json_test.rb +2 -0
  120. data/test/adapter/null_test.rb +2 -0
  121. data/test/adapter/polymorphic_test.rb +2 -0
  122. data/test/adapter_test.rb +2 -0
  123. data/test/array_serializer_test.rb +2 -0
  124. data/test/benchmark/app.rb +2 -0
  125. data/test/benchmark/benchmarking_support.rb +2 -0
  126. data/test/benchmark/bm_active_record.rb +2 -0
  127. data/test/benchmark/bm_adapter.rb +2 -0
  128. data/test/benchmark/bm_caching.rb +2 -0
  129. data/test/benchmark/bm_lookup_chain.rb +2 -0
  130. data/test/benchmark/bm_transform.rb +2 -0
  131. data/test/benchmark/controllers.rb +2 -0
  132. data/test/benchmark/fixtures.rb +2 -0
  133. data/test/cache_test.rb +69 -3
  134. data/test/collection_serializer_test.rb +2 -0
  135. data/test/fixtures/active_record.rb +2 -0
  136. data/test/fixtures/poro.rb +2 -0
  137. data/test/generators/scaffold_controller_generator_test.rb +2 -0
  138. data/test/generators/serializer_generator_test.rb +2 -0
  139. data/test/grape_test.rb +2 -0
  140. data/test/lint_test.rb +2 -0
  141. data/test/logger_test.rb +2 -0
  142. data/test/poro_test.rb +2 -0
  143. data/test/serializable_resource_test.rb +2 -0
  144. data/test/serializers/association_macros_test.rb +2 -0
  145. data/test/serializers/associations_test.rb +52 -0
  146. data/test/serializers/attribute_test.rb +2 -0
  147. data/test/serializers/attributes_test.rb +2 -0
  148. data/test/serializers/caching_configuration_test_isolated.rb +2 -0
  149. data/test/serializers/configuration_test.rb +2 -0
  150. data/test/serializers/fieldset_test.rb +2 -0
  151. data/test/serializers/meta_test.rb +2 -0
  152. data/test/serializers/options_test.rb +2 -0
  153. data/test/serializers/read_attribute_for_serialization_test.rb +2 -0
  154. data/test/serializers/reflection_test.rb +54 -0
  155. data/test/serializers/root_test.rb +2 -0
  156. data/test/serializers/serialization_test.rb +2 -0
  157. data/test/serializers/serializer_for_test.rb +2 -0
  158. data/test/serializers/serializer_for_with_namespace_test.rb +2 -0
  159. data/test/support/isolated_unit.rb +2 -0
  160. data/test/support/rails5_shims.rb +2 -0
  161. data/test/support/rails_app.rb +2 -0
  162. data/test/support/serialization_testing.rb +2 -0
  163. data/test/test_helper.rb +2 -0
  164. metadata +56 -35
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c3a5900ed1c671e57b90ea00e3130ad3a5fe4939a6a1c36e1481dbdeeef3199
4
- data.tar.gz: 59ed63aa89861c650f87ba5bfeb5dfc90c997cd8fe4542129f28d1194d3c24ff
3
+ metadata.gz: e365d9b8e66fd40b66555a2e57faad48fee5bfd82b988ab42aacda73930bb53c
4
+ data.tar.gz: bbad118b340ea31c7a09f1f0661d0e53866135a99923276182dc159d4eeb2f9c
5
5
  SHA512:
6
- metadata.gz: 0ee2569a69953e286d63861bb01a37836ee9b25463980da1dc561815e0d7e7902aa1b1d344b265bb50f3c41b1e93ea6cfec2ba241c5314511d4a55a5082f5ce0
7
- data.tar.gz: d011e2d950d8b7869d4f49139d23cf994f2b1f6e8319aa2a3ff018d4f1c578d24bb64cfe632c912e91edd92e0f5945fa58a1a7742b611ef33c5a4d0a14dfbc70
6
+ metadata.gz: 011fcf63a00a7cfc27a54679551cf6c03b3dd4ff177192fca7d2be34bfbb2c3b6c6ba40bc92395997a392ed9bf1d91823da15afa7bde374d08f5e9d69982d054
7
+ data.tar.gz: 8311b1ed270c60ae5d00beaa2e421a350201bec131a44096190f40dfb61a84f11d50ace18c357cc77e364863bef94eebaad717dc78a156c923049679c5295d1d
data/.rubocop.yml CHANGED
@@ -59,6 +59,10 @@ Style/MissingElse:
59
59
  Style/EmptyElse:
60
60
  EnforcedStyle: empty
61
61
 
62
+ Style/FrozenStringLiteralComment:
63
+ Enabled: true
64
+ EnforcedStyle: always
65
+
62
66
  Style/MultilineOperationIndentation:
63
67
  EnforcedStyle: indented
64
68
 
data/.travis.yml CHANGED
@@ -6,9 +6,9 @@ cache:
6
6
  - vendor/bundle
7
7
 
8
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
9
+ - "travis_retry gem update --system 2.7.8"
10
+ - "travis_retry gem install bundler -v '1.17.3'"
11
+ install: BUNDLER_VERSION=1.17.3 bundle install --path=vendor/bundle --retry=3 --jobs=3
12
12
 
13
13
  script:
14
14
  - bundle exec rake ci
@@ -21,6 +21,7 @@ env:
21
21
  - "RAILS_VERSION=4.2"
22
22
  - "RAILS_VERSION=5.0"
23
23
  - "RAILS_VERSION=5.1"
24
+ - "RAILS_VERSION=5.2"
24
25
  - "RAILS_VERSION=master"
25
26
 
26
27
  rvm:
@@ -28,8 +29,13 @@ rvm:
28
29
  - 2.2.8
29
30
  - 2.3.5
30
31
  - 2.4.2
32
+ - 2.5.3
31
33
  - ruby-head
32
34
 
35
+ branches:
36
+ only:
37
+ - 0-10-stable
38
+
33
39
  matrix:
34
40
  include:
35
41
  - { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=4.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
@@ -40,9 +46,14 @@ matrix:
40
46
  # - { rvm: jruby-head, jdk: oraclejdk8, env: "RAILS_VERSION=5.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
41
47
  exclude:
42
48
  - { rvm: 2.1.10, env: RAILS_VERSION=master }
49
+ - { rvm: 2.2.8, env: RAILS_VERSION=master }
50
+ - { rvm: 2.3.5, env: RAILS_VERSION=master }
51
+ - { rvm: 2.4.2, env: RAILS_VERSION=master }
43
52
  - { rvm: 2.1.10, env: RAILS_VERSION=5.0 }
44
53
  - { rvm: 2.1.10, env: RAILS_VERSION=5.1 }
54
+ - { rvm: 2.1.10, env: RAILS_VERSION=5.2 }
45
55
  - { rvm: 2.4.2, env: RAILS_VERSION=4.1 }
56
+ - { rvm: 2.5.3, env: RAILS_VERSION=4.1 }
46
57
  - { rvm: ruby-head, env: RAILS_VERSION=4.1 }
47
58
  allow_failures:
48
59
  - rvm: ruby-head
data/CHANGELOG.md CHANGED
@@ -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.7...0-10-stable)
3
+ ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.9...0-10-stable)
4
4
 
5
5
  Breaking changes:
6
6
 
@@ -10,6 +10,34 @@ Fixes:
10
10
 
11
11
  Misc:
12
12
 
13
+ ### [v0.10.9 (2019-02-08)](https://github.com/rails-api/active_model_serializers/compare/v0.10.8...v0.10.9)
14
+
15
+
16
+ Fixes:
17
+
18
+ - [#2288](https://github.com/rails-api/active_model_serializers/pull/2288)
19
+ Change the fetch method to deal with recyclable key cache strategy.
20
+ Fixes #2287. (@cintamani, @wasifhossain)
21
+ - [#2307](https://github.com/rails-api/active_model_serializers/pull/2307) Falsey attribute values should not be reevaluated.
22
+
23
+ Misc:
24
+
25
+ - [#2309](https://github.com/rails-api/active_model_serializers/pull/2309) Performance and memory usage fixes
26
+
27
+ ### [v0.10.8 (2018-11-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.7...v0.10.8)
28
+
29
+ Features:
30
+ - [#2279](https://github.com/rails-api/active_model_serializers/pull/2279) Support condition options in serializer link statements
31
+
32
+ Fixes:
33
+
34
+ - [#2296](https://github.com/rails-api/active_model_serializers/pull/2296) Fixes #2295 (@Hirurg103)
35
+ - Fix finding of namespaced serializer and non-namespaced model.
36
+ - [#2289](https://github.com/rails-api/active_model_serializers/pull/2289) Fixes #2255 (@f-mer)
37
+ - Fix autoloading race condition, especially in Rails 5.
38
+ - [#2299](https://github.com/rails-api/active_model_serializers/pull/2299) Fixes #2270 (@chau-bao-long via #2276)
39
+ - Fix reflection thread-safety bug
40
+
13
41
  ### [v0.10.7 (2017-11-14)](https://github.com/rails-api/active_model_serializers/compare/v0.10.6...v0.10.7)
14
42
 
15
43
  Regressions Fixed From v0.10.6:
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
  #
3
5
  # Add a Gemfile.local to locally bundle gems outside of version control
@@ -51,12 +53,12 @@ group :bench do
51
53
  end
52
54
 
53
55
  group :test do
54
- gem 'sqlite3', platform: (@windows_platforms + [:ruby])
56
+ gem 'sqlite3', '~> 1.3.13', platform: (@windows_platforms + [:ruby])
55
57
  platforms :jruby do
56
58
  if version == 'master' || version >= '5'
57
- gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.0' # github: 'jruby/activerecord-jdbc-adapter', branch: 'master'
59
+ gem 'activerecord-jdbcsqlite3-adapter', '~> 50'
58
60
  else
59
- gem 'activerecord-jdbcsqlite3-adapter'
61
+ gem 'activerecord-jdbcsqlite3-adapter', '~> 1.3.0'
60
62
  end
61
63
  end
62
64
  gem 'codeclimate-test-reporter', require: false
data/README.md CHANGED
@@ -266,8 +266,8 @@ to know about, but not part of ActiveModelSerializers.)
266
266
 
267
267
  - An `ActiveRecord::Base` object.
268
268
  - Any Ruby object that passes the
269
- [Lint](http://www.rubydoc.info/github/rails-api/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
270
- [code](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/lint.rb).
269
+ [Lint](https://www.rubydoc.info/gems/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
270
+ [(code)](lib/active_model/serializer/lint.rb).
271
271
 
272
272
  ActiveModelSerializers provides a
273
273
  [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb),
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'bundler/setup'
3
5
  rescue LoadError
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # coding: utf-8
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
@@ -39,7 +40,7 @@ Gem::Specification.new do |spec|
39
40
  # 'activesupport', rails_versions
40
41
  # 'i18n,
41
42
  # 'tzinfo'
42
- # 'minitest'
43
+ spec.add_development_dependency 'minitest', ['~> 5.0', '< 5.11']
43
44
  # 'thread_safe'
44
45
 
45
46
  spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.3']
@@ -65,7 +65,7 @@ Where:
65
65
  - `virtual_value:`
66
66
  - `polymorphic:` defines if polymorphic relation type should be nested in serialized association.
67
67
  - `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship.
68
- - `class_name:` used to determine `type` when `type` not given
68
+ - `class_name:` the (String) model name used to determine `type`, when `type` is not given. e.g. `class_name: "Comment"` would imply the type `comments`
69
69
  - `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object.
70
70
  - `namespace:` used when looking up the serializer and `serializer` is not given. Falls back to the parent serializer's `:namespace` instance options, which, when present, comes from the render options. See [Rendering#namespace](rendering.md#namespace] for more details.
71
71
  - optional: `&block` is a context that returns the association's attributes.
@@ -81,6 +81,7 @@ e.g.
81
81
  ```ruby
82
82
  has_one :bio
83
83
  has_one :blog, key: :site
84
+ has_one :blog, class_name: "Blog"
84
85
  has_one :maker, virtual_value: { id: 1 }
85
86
 
86
87
  has_one :blog do |serializer|
@@ -114,6 +115,7 @@ e.g.
114
115
  has_many :comments
115
116
  has_many :comments, key: :reviews
116
117
  has_many :comments, serializer: CommentPreviewSerializer
118
+ has_many :comments, class_name: "Comment"
117
119
  has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
118
120
  has_many :comments, key: :last_comments do
119
121
  last(1)
@@ -127,6 +129,7 @@ e.g.
127
129
  ```ruby
128
130
  belongs_to :author, serializer: AuthorPreviewSerializer
129
131
  belongs_to :author, key: :writer
132
+ belongs_to :author, class_name: "Author"
130
133
  belongs_to :post
131
134
  belongs_to :blog
132
135
  def blog
@@ -235,6 +238,15 @@ link :other, 'https://example.com/resource'
235
238
  link(:posts) { link_author_posts_url(object) }
236
239
  ```
237
240
 
241
+ Just like attributes, links also support conditions in options
242
+ ```ruby
243
+ link(:secret, if: :internal?) { object.secret_link }
244
+
245
+ def internal?
246
+ instance_options[:context] == :internal
247
+ end
248
+ ```
249
+
238
250
  #### #object
239
251
 
240
252
  The object being serialized.
@@ -294,7 +306,7 @@ end
294
306
  Whether you write the method as above or as `object.comments.where(created_by: scope)`
295
307
  is a matter of preference (assuming `scope_name` has been set).
296
308
 
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.
309
+ 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
310
 
299
311
  ##### Controller Authorization Context
300
312
 
@@ -381,7 +393,7 @@ class PostsController < ActionController::Base
381
393
  end
382
394
  end
383
395
  ```
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`.
396
+ 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`.
385
397
 
386
398
  #### #read_attribute_for_serialization(key)
387
399
 
@@ -474,7 +486,7 @@ the `ActiveModel::Serializer.serializer_for` method to return a serializer class
474
486
  ```ruby
475
487
  class MySerializer < ActiveModel::Serializer
476
488
  def self.serializer_for(model, options)
477
- return SparseAdminSerializer if model.class == 'Admin'
489
+ return SparseAdminSerializer if model.class.name == 'Admin'
478
490
  super
479
491
  end
480
492
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/class/attribute'
2
4
  require 'active_model_serializers/serialization_context'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'set'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model_serializers/adapter'
2
4
  require 'active_model_serializers/deprecate'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/collection_serializer'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/lazy_association'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/field'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # @api private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class CollectionSerializer
@@ -19,11 +21,10 @@ module ActiveModel
19
21
 
20
22
  # @api private
21
23
  def serializable_hash(adapter_options, options, adapter_instance)
22
- include_directive = ActiveModel::Serializer.include_directive_from_options(adapter_options)
23
- adapter_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, include_directive)
24
- adapter_opts = adapter_options.merge(include_directive: include_directive)
24
+ options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
25
+ options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, options[:include_directive])
25
26
  serializers.map do |serializer|
26
- serializer.serializable_hash(adapter_opts, options, adapter_instance)
27
+ serializer.serializable_hash(adapter_options, options, adapter_instance)
27
28
  end
28
29
  end
29
30
 
@@ -48,8 +49,7 @@ module ActiveModel
48
49
  # 4. key may be nil for empty collection and no serializer option
49
50
  key &&= key.pluralize
50
51
  # 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')
52
+ key || fail(ArgumentError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
53
53
  end
54
54
  # rubocop:enable Metrics/CyclomaticComplexity
55
55
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  UndefinedCacheKey = Class.new(StandardError)
@@ -281,7 +283,9 @@ module ActiveModel
281
283
  # Use object's cache_key if available, else derive a key from the object
282
284
  # Pass the `key` option to the `cache` declaration or override this method to customize the cache key
283
285
  def object_cache_key
284
- if object.respond_to?(:cache_key)
286
+ if object.respond_to?(:cache_key_with_version)
287
+ object.cache_key_with_version
288
+ elsif object.respond_to?(:cache_key)
285
289
  object.cache_key
286
290
  elsif (serializer_cache_key = (serializer_class._cache_key || serializer_class._cache_options[:key]))
287
291
  object_time_safe = object.updated_at
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class ErrorSerializer < ActiveModel::Serializer
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/error_serializer'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # Holds all the meta-data about a field (i.e. attribute or association) as it was
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class Fieldset
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # @api private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # @api private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # @api private
@@ -7,11 +9,12 @@ module ActiveModel
7
9
  delegate :collection?, to: :reflection
8
10
 
9
11
  def reflection_options
10
- @reflection_options ||= reflection.options.dup.reject { |k, _| !REFLECTION_OPTIONS.include?(k) }
12
+ @reflection_options ||= reflection.options.select { |k, _| REFLECTION_OPTIONS.include?(k) }
11
13
  end
12
14
 
13
15
  def object
14
- @object ||= reflection.value(
16
+ return @object if defined?(@object)
17
+ @object = reflection.value(
15
18
  association_options.fetch(:parent_serializer),
16
19
  association_options.fetch(:include_slice)
17
20
  )
@@ -76,6 +79,7 @@ module ActiveModel
76
79
  serializer_options[:serializer_context_class] = association_options.fetch(:parent_serializer).class
77
80
  serializer = reflection_options.fetch(:serializer, nil)
78
81
  serializer_options[:serializer] = serializer if serializer
82
+ serializer_options[:namespace] = reflection_options[:namespace] if reflection_options[:namespace]
79
83
  serializer_class.new(object, serializer_options)
80
84
  end
81
85
 
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model/serializer/field'
4
+
5
+ module ActiveModel
6
+ class Serializer
7
+ # Holds all the data about a serializer link
8
+ #
9
+ # @example
10
+ # class PostSerializer < ActiveModel::Serializer
11
+ # link :callback, if: :internal? do
12
+ # object.callback_link
13
+ # end
14
+ #
15
+ # def internal?
16
+ # instance_options[:internal] == true
17
+ # end
18
+ # end
19
+ #
20
+ class Link < Field
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  module Lint
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class Null < Serializer
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializer/field'
2
4
  require 'active_model/serializer/association'
3
5
 
@@ -151,6 +153,9 @@ module ActiveModel
151
153
  # @yield [ActiveModel::Serializer]
152
154
  # @return [:nil, associated resource or resource collection]
153
155
  def value(serializer, include_slice)
156
+ # NOTE(BF): This method isn't thread-safe because the _reflections class attribute is not thread-safe
157
+ # Therefore, when we build associations from reflections, we dup the entire reflection instance.
158
+ # Better solutions much appreciated!
154
159
  @object = serializer.object
155
160
  @scope = serializer.scope
156
161
 
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
- VERSION = '0.10.7'.freeze
5
+ VERSION = '0.10.9'.freeze
4
6
  end
5
7
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'thread_safe'
2
4
  require 'jsonapi/include_directive'
3
5
  require 'active_model/serializer/collection_serializer'
@@ -18,16 +20,17 @@ module ActiveModel
18
20
  # @see #serializable_hash for more details on these valid keys.
19
21
  SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
20
22
  extend ActiveSupport::Autoload
21
- autoload :Adapter
22
- autoload :Null
23
- autoload :Attribute
24
- autoload :Association
25
- autoload :Reflection
26
- autoload :SingularReflection
27
- autoload :CollectionReflection
28
- autoload :BelongsToReflection
29
- autoload :HasOneReflection
30
- autoload :HasManyReflection
23
+ eager_autoload do
24
+ autoload :Adapter
25
+ autoload :Null
26
+ autoload :Attribute
27
+ autoload :Link
28
+ autoload :Association
29
+ autoload :Reflection
30
+ autoload :BelongsToReflection
31
+ autoload :HasOneReflection
32
+ autoload :HasManyReflection
33
+ end
31
34
  include ActiveSupport::Configurable
32
35
  include Caching
33
36
 
@@ -275,9 +278,14 @@ module ActiveModel
275
278
  # link(:self) { "http://example.com/resource/#{object.id}" }
276
279
  # @example
277
280
  # link :resource, "http://example.com/resource"
281
+ # @example
282
+ # link(:callback, if: :internal?), { "http://example.com/callback" }
278
283
  #
279
- def self.link(name, value = nil, &block)
280
- _links[name] = block || value
284
+ def self.link(name, *args, &block)
285
+ options = args.extract_options!
286
+ # For compatibility with the use case of passing link directly as string argument
287
+ # without block, we are creating a wrapping block
288
+ _links[name] = Link.new(name, options, block || ->(_serializer) { args.first })
281
289
  end
282
290
 
283
291
  # Set the JSON API meta attribute of a serializer.
@@ -341,7 +349,7 @@ module ActiveModel
341
349
  return Enumerator.new {} unless object
342
350
 
343
351
  Enumerator.new do |y|
344
- self.class._reflections.each do |key, reflection|
352
+ (self.instance_reflections ||= self.class._reflections.deep_dup).each do |key, reflection|
345
353
  next if reflection.excluded?(self)
346
354
  next unless include_directive.key?(key)
347
355
 
@@ -405,6 +413,6 @@ module ActiveModel
405
413
 
406
414
  protected
407
415
 
408
- attr_accessor :instance_options
416
+ attr_accessor :instance_options, :instance_reflections
409
417
  end
410
418
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class Attributes < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'case_transform'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class Json < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class JsonApi < Base