active_model_serializers 0.10.4 → 0.10.5

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 (53) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +7 -3
  3. data/CHANGELOG.md +25 -1
  4. data/README.md +4 -4
  5. data/active_model_serializers.gemspec +1 -1
  6. data/appveyor.yml +9 -3
  7. data/docs/general/logging.md +7 -0
  8. data/docs/general/serializers.md +3 -3
  9. data/docs/howto/add_pagination_links.md +13 -13
  10. data/docs/howto/add_relationship_links.md +24 -21
  11. data/docs/howto/outside_controller_use.md +3 -2
  12. data/docs/howto/serialize_poro.md +46 -5
  13. data/docs/howto/upgrade_from_0_8_to_0_10.md +1 -1
  14. data/lib/active_model/serializer/version.rb +1 -1
  15. data/lib/active_model_serializers.rb +8 -0
  16. data/lib/active_model_serializers/model.rb +108 -30
  17. data/lib/active_model_serializers/test/schema.rb +2 -2
  18. data/lib/generators/rails/resource_override.rb +1 -1
  19. data/test/action_controller/adapter_selector_test.rb +11 -2
  20. data/test/action_controller/json_api/fields_test.rb +15 -6
  21. data/test/action_controller/json_api/transform_test.rb +11 -3
  22. data/test/action_controller/namespace_lookup_test.rb +14 -8
  23. data/test/action_controller/serialization_scope_name_test.rb +12 -6
  24. data/test/action_controller/serialization_test.rb +1 -1
  25. data/test/active_model_serializers/model_test.rb +122 -2
  26. data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
  27. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +37 -19
  28. data/test/adapter/attributes_test.rb +2 -5
  29. data/test/adapter/json/has_many_test.rb +10 -2
  30. data/test/adapter/json_api/fields_test.rb +11 -3
  31. data/test/adapter/json_api/has_many_test.rb +10 -2
  32. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +22 -5
  33. data/test/adapter/json_api/linked_test.rb +3 -3
  34. data/test/adapter/json_api/links_test.rb +1 -1
  35. data/test/adapter/json_api/relationship_test.rb +1 -1
  36. data/test/adapter/json_api/transform_test.rb +11 -3
  37. data/test/cache_test.rb +100 -28
  38. data/test/collection_serializer_test.rb +23 -10
  39. data/test/fixtures/active_record.rb +45 -10
  40. data/test/fixtures/poro.rb +115 -176
  41. data/test/generators/serializer_generator_test.rb +1 -0
  42. data/test/grape_test.rb +20 -2
  43. data/test/serializers/associations_test.rb +28 -7
  44. data/test/serializers/attribute_test.rb +4 -2
  45. data/test/serializers/caching_configuration_test_isolated.rb +6 -6
  46. data/test/serializers/options_test.rb +17 -6
  47. data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
  48. data/test/serializers/serialization_test.rb +2 -2
  49. data/test/serializers/serializer_for_test.rb +6 -6
  50. data/test/serializers/serializer_for_with_namespace_test.rb +6 -5
  51. data/test/support/rails_app.rb +2 -0
  52. data/test/test_helper.rb +12 -0
  53. metadata +13 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5839aadc9b4688e8c8d2005c19278080683d589
4
- data.tar.gz: acd53226d78b77f5d13b735fbfc8a3aef06b2827
3
+ metadata.gz: e38c0b5ccb4d32da05808ebff7112d20d7d8d451
4
+ data.tar.gz: 7e50bf4858b40a9f988d112a95f6afc34c38156b
5
5
  SHA512:
6
- metadata.gz: f2e27357e980e8886773280a95b411fd6fecc6c9c1cfa9033618e0b2d5cfe9725440d4c143cef062b5087b58679fa45de5769f77c82dc3e16fd28cbe94273526
7
- data.tar.gz: c08d8d8ec50bdf46dba315157f9cc3f75d3561e242aa70ea1892da527cb830d8558910b9208c5030098458c812081519419a58b43c72fed228fd3c2af80822a7
6
+ metadata.gz: 25710e210284167d23ebf77b024d31b06326d71a68c8bcbc1f4f4439baf0b50018a02060da44ebefcb077c9abc2a74bd632cb54662942cb3d17c464d2b420b79
7
+ data.tar.gz: 712546ebd0b8fd5d1243b32c76349fc92ab0610e2562848c42c7faff4e1fc5820282ea1492c5526d721137fda7eb6dc1dacc422720f493d36e75c9185c3bff79
data/.travis.yml CHANGED
@@ -7,12 +7,16 @@ rvm:
7
7
  - 2.2.6
8
8
  - 2.3.3
9
9
  - ruby-head
10
- - jruby-9.0.4.0
10
+ - jruby-9.1.5.0 # is precompiled per http://rubies.travis-ci.org/
11
11
  - jruby-head
12
12
 
13
13
  jdk:
14
14
  - oraclejdk8
15
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
16
20
  install: bundle install --path=vendor/bundle --retry=3 --jobs=3
17
21
  cache:
18
22
  directories:
@@ -35,13 +39,13 @@ matrix:
35
39
  exclude:
36
40
  - rvm: 2.1
37
41
  env: RAILS_VERSION=master
38
- - rvm: jruby-9.0.4.0
42
+ - rvm: jruby-9.1.5.0
39
43
  env: RAILS_VERSION=master
40
44
  - rvm: jruby-head
41
45
  env: RAILS_VERSION=master
42
46
  - rvm: 2.1
43
47
  env: RAILS_VERSION=5.0
44
- - rvm: jruby-9.0.4.0
48
+ - rvm: jruby-9.1.5.0
45
49
  env: RAILS_VERSION=5.0
46
50
  - rvm: jruby-head
47
51
  env: RAILS_VERSION=5.0
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.4...master)
3
+ ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.5...master)
4
4
 
5
5
  Breaking changes:
6
6
 
@@ -10,6 +10,30 @@ Fixes:
10
10
 
11
11
  Misc:
12
12
 
13
+ ### [v0.10.5 (2017-03-07)](https://github.com/rails-api/active_model_serializers/compare/v0.10.4...v0.10.5)
14
+
15
+ Breaking changes:
16
+
17
+ Features:
18
+
19
+ - [#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)
20
+ - [#2057](https://github.com/rails-api/active_model_serializers/pull/2057)
21
+ Update version constraint for jsonapi-renderer to `['>= 0.1.1.beta1', '< 0.2']`
22
+ (@jaredbeck)
23
+
24
+ Fixes:
25
+
26
+ - [#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)
27
+
28
+ Misc:
29
+
30
+ - [#2055](https://github.com/rails-api/active_model_serializers/pull/2055)
31
+ Replace deprecated dependency jsonapi with jsonapi-renderer. (@jaredbeck)
32
+ - [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) Make test attributes explicit. Tests have Model#associations. (@bf4)
33
+ - [#1981](https://github.com/rails-api/active_model_serializers/pull/1981) Fix relationship link documentation. (@groyoh)
34
+ - [#2035](https://github.com/rails-api/active_model_serializers/pull/2035) Document how to disable the logger. (@MSathieu)
35
+ - [#2039](https://github.com/rails-api/active_model_serializers/pull/2039) Documentation fixes. (@biow0lf)
36
+
13
37
  ### [v0.10.4 (2017-01-06)](https://github.com/rails-api/active_model_serializers/compare/v0.10.3...v0.10.4)
14
38
 
15
39
  Misc:
data/README.md CHANGED
@@ -90,8 +90,8 @@ reading documentation for our `master`, which may include features that have not
90
90
  been released yet. Please see below for the documentation relevant to you.
91
91
 
92
92
  - [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
93
- - [0.10.3 (latest release) Documentation](https://github.com/rails-api/active_model_serializers/tree/v0.10.3)
94
- - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/active_model_serializers/0.10.3)
93
+ - [0.10.4 (latest release) Documentation](https://github.com/rails-api/active_model_serializers/tree/v0.10.4)
94
+ - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/active_model_serializers/0.10.4)
95
95
  - [Guides](docs)
96
96
  - [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
97
97
  - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)
@@ -116,7 +116,7 @@ class SomeResource < ActiveRecord::Base
116
116
  end
117
117
  # or
118
118
  class SomeResource < ActiveModelSerializers::Model
119
- attr_accessor :title, :body
119
+ attributes :title, :body
120
120
  end
121
121
  ```
122
122
 
@@ -279,7 +279,7 @@ which is a simple serializable PORO (Plain-Old Ruby Object).
279
279
 
280
280
  ```ruby
281
281
  class MyModel < ActiveModelSerializers::Model
282
- attr_accessor :id, :name, :level
282
+ attributes :id, :name, :level
283
283
  end
284
284
  ```
285
285
 
@@ -42,7 +42,7 @@ Gem::Specification.new do |spec|
42
42
  # 'minitest'
43
43
  # 'thread_safe'
44
44
 
45
- spec.add_runtime_dependency 'jsonapi', '0.1.1.beta6'
45
+ spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.2']
46
46
  spec.add_runtime_dependency 'case_transform', '>= 0.2'
47
47
 
48
48
  spec.add_development_dependency 'activerecord', rails_versions
data/appveyor.yml CHANGED
@@ -1,4 +1,4 @@
1
- version: '{build}'
1
+ version: 1.0.{build}-{branch}
2
2
 
3
3
  skip_tags: true
4
4
 
@@ -7,17 +7,23 @@ environment:
7
7
  matrix:
8
8
  - ruby_version: "Ruby21"
9
9
  - ruby_version: "Ruby21-x64"
10
- - ruby_version: "jruby-9.0.0.0"
11
10
 
12
11
  cache:
13
12
  - vendor/bundle
14
13
 
15
14
  install:
16
15
  - SET PATH=C:\%ruby_version%\bin;%PATH%
17
- - gem install bundler
16
+ - gem update --system
17
+ - gem uninstall bundler -a -x
18
+ - gem install bundler -v 1.13.7
18
19
  - bundle env
19
20
  - bundle install --path=vendor/bundle --retry=3 --jobs=3
20
21
 
22
+ before_test:
23
+ - ruby -v
24
+ - gem -v
25
+ - bundle -v
26
+
21
27
  test_script:
22
28
  - bundle exec rake ci
23
29
 
@@ -12,3 +12,10 @@ You may customize the logger in an initializer, for example:
12
12
  ```ruby
13
13
  ActiveModelSerializers.logger = Logger.new(STDOUT)
14
14
  ```
15
+
16
+ You can also disable the logger, just put this in `config/initializers/active_model_serializers.rb`:
17
+
18
+ ```ruby
19
+ require 'active_model_serializers'
20
+ ActiveSupport::Notifications.unsubscribe(ActiveModelSerializers::Logging::RENDER_EVENT)
21
+ ```
@@ -225,10 +225,10 @@ With the `:json` adapter, the previous serializer would be rendered as:
225
225
  link :self do
226
226
  href "https://example.com/link_author/#{object.id}"
227
227
  end
228
- link :author { link_author_url(object) }
229
- link :link_authors { link_authors_url }
228
+ link(:author) { link_author_url(object) }
229
+ link(:link_authors) { link_authors_url }
230
230
  link :other, 'https://example.com/resource'
231
- link :posts { link_author_posts_url(object) }
231
+ link(:posts) { link_author_posts_url(object) }
232
232
  ```
233
233
 
234
234
  #### #object
@@ -77,13 +77,13 @@ If you are using `JSON` adapter, pagination links will not be included automatic
77
77
  Add this method to your base API controller.
78
78
 
79
79
  ```ruby
80
- def pagination_dict(object)
80
+ def pagination_dict(collection)
81
81
  {
82
- current_page: object.current_page,
83
- next_page: object.next_page,
84
- prev_page: object.prev_page, # use object.previous_page when using will_paginate
85
- total_pages: object.total_pages,
86
- total_count: object.total_count
82
+ current_page: collection.current_page,
83
+ next_page: collection.next_page,
84
+ prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
85
+ total_pages: collection.total_pages,
86
+ total_count: collection.total_count
87
87
  }
88
88
  end
89
89
  ```
@@ -117,18 +117,18 @@ ex.
117
117
  You can also achieve the same result if you have a helper method that adds the pagination info in the meta tag. For instance, in your action specify a custom serializer.
118
118
 
119
119
  ```ruby
120
- render json: @posts, each_serializer: PostPreviewSerializer, meta: meta_attributes(@post)
120
+ render json: @posts, each_serializer: PostPreviewSerializer, meta: meta_attributes(@posts)
121
121
  ```
122
122
 
123
123
  ```ruby
124
124
  #expects pagination!
125
- def meta_attributes(resource, extra_meta = {})
125
+ def meta_attributes(collection, extra_meta = {})
126
126
  {
127
- current_page: resource.current_page,
128
- next_page: resource.next_page,
129
- prev_page: resource.prev_page, # use resource.previous_page when using will_paginate
130
- total_pages: resource.total_pages,
131
- total_count: resource.total_count
127
+ current_page: collection.current_page,
128
+ next_page: collection.next_page,
129
+ prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
130
+ total_pages: collection.total_pages,
131
+ total_count: collection.total_count
132
132
  }.merge(extra_meta)
133
133
  end
134
134
  ```
@@ -15,40 +15,42 @@ class Api::V1::UsersController < ApplicationController
15
15
  serializer: Api::V1::UserSerializer,
16
16
  include: []
17
17
  end
18
+ end
18
19
  ```
19
20
 
20
21
  Bear in mind though that ActiveModelSerializers are [framework-agnostic](outside_controller_use.md), Rails is just a common example here.
21
22
 
22
23
  ### Links as an attribute of a resource
23
- **This is applicable to JSONAPI, JSON and Attributes adapters**
24
+ **This is applicable to JSON and Attributes adapters**
24
25
 
25
26
  You can define an attribute in the resource, named `links`.
26
27
 
27
28
  ```ruby
28
29
  class Api::V1::UserSerializer < ActiveModel::Serializer
29
- attributes :id, :name, :links
30
+ include Rails.application.routes.url_helpers
31
+
32
+ attributes :id, :name
30
33
 
31
- def links
34
+ attribute :links do
35
+ id = object.id
32
36
  {
33
- self: api_v1_user_path(object.id),
34
- microposts: api_v1_microposts_path(user_id: object.id)
37
+ self: api_v1_user_path(id),
38
+ microposts: api_v1_microposts_path(user_id: id)
35
39
  }
36
40
  end
37
41
  end
38
42
  ```
39
43
 
40
- This will result in (example is in JSONAPI adapter):
44
+ Using the `JSON` adapter, this will result in:
45
+
41
46
  ```json
42
47
  {
43
- "data": {
48
+ "user": {
44
49
  "id": "1",
45
- "type": "users",
46
- "attributes": {
47
- "name": "Example User",
48
- "links": {
49
- "self": "/api/v1/users/1",
50
- "microposts": "/api/v1/microposts?user_id=1"
51
- }
50
+ "name": "John",
51
+ "links": {
52
+ "self": "/api/v1/users/1",
53
+ "microposts": "/api/v1/microposts?user_id=1"
52
54
  }
53
55
  }
54
56
  }
@@ -58,7 +60,7 @@ This will result in (example is in JSONAPI adapter):
58
60
  ### Links as a property of the resource definiton
59
61
  **This is only applicable to JSONAPI adapter**
60
62
 
61
- You can use the `links` class method to define the links you need in the resource's primary data.
63
+ You can use the `link` class method to define the links you need in the resource's primary data.
62
64
 
63
65
  ```ruby
64
66
  class Api::V1::UserSerializer < ActiveModel::Serializer
@@ -69,7 +71,8 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
69
71
  end
70
72
  ```
71
73
 
72
- This will result in (example is in JSONAPI adapter):
74
+ Using the `JSONAPI` adapter, this will result in:
75
+
73
76
  ```json
74
77
  {
75
78
  "data": {
@@ -104,12 +107,12 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
104
107
 
105
108
  has_many :microposts, serializer: Api::V1::MicropostSerializer do
106
109
  link(:related) { api_v1_microposts_path(user_id: object.id) }
107
- end
108
110
 
109
- #this is needed to avoid n+1, gem core devs are working to remove this necessity
110
- #more on: https://github.com/rails-api/active_model_serializers/issues/1325
111
- def microposts
112
- object.microposts.loaded ? object.microposts : object.microposts.none
111
+ microposts = object.microposts
112
+ # The following code is needed to avoid n+1 queries.
113
+ # Core devs are working to remove this necessity.
114
+ # See: https://github.com/rails-api/active_model_serializers/issues/1325
115
+ microposts.loaded? ? microposts : microposts.none
113
116
  end
114
117
  end
115
118
  ```
@@ -10,8 +10,8 @@ In ActiveModelSerializers versions 0.10 or later, serializing resources outside
10
10
  # Create our resource
11
11
  post = Post.create(title: "Sample post", body: "I love Active Model Serializers!")
12
12
 
13
- # Optional options parameters
14
- options = {}
13
+ # Optional options parameters for both the serializer and instance
14
+ options = {serializer: PostDetailedSerializer, username: 'sample user'}
15
15
 
16
16
  # Create a serializable resource instance
17
17
  serializable_resource = ActiveModelSerializers::SerializableResource.new(post, options)
@@ -20,6 +20,7 @@ serializable_resource = ActiveModelSerializers::SerializableResource.new(post, o
20
20
  model_json = serializable_resource.as_json
21
21
  ```
22
22
  The object that is passed to `ActiveModelSerializers::SerializableResource.new` can be a single resource or a collection.
23
+ The additional options are the same options that are passed [through controllers](../general/rendering.md#explicit-serializer).
23
24
 
24
25
  ### Looking up the Serializer for a Resource
25
26
 
@@ -2,13 +2,16 @@
2
2
 
3
3
  # How to serialize a Plain-Old Ruby Object (PORO)
4
4
 
5
- When you are first getting started with ActiveModelSerializers, it may seem only `ActiveRecord::Base` objects can be serializable, but pretty much any object can be serializable with ActiveModelSerializers. Here is an example of a PORO that is serializable:
5
+ When you are first getting started with ActiveModelSerializers, it may seem only `ActiveRecord::Base` objects can be serializable,
6
+ but pretty much any object can be serializable with ActiveModelSerializers.
7
+ Here is an example of a PORO that is serializable in most situations:
8
+
6
9
  ```ruby
7
10
  # my_model.rb
8
11
  class MyModel
9
12
  alias :read_attribute_for_serialization :send
10
13
  attr_accessor :id, :name, :level
11
-
14
+
12
15
  def initialize(attributes)
13
16
  @id = attributes[:id]
14
17
  @name = attributes[:name]
@@ -21,12 +24,50 @@ class MyModel
21
24
  end
22
25
  ```
23
26
 
24
- Fortunately, ActiveModelSerializers provides a [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb) which you can use in production code that will make your PORO a lot cleaner. The above code now becomes:
27
+ The [ActiveModel::Serializer::Lint::Tests](../../lib/active_model/serializer/lint.rb)
28
+ define and validate which methods ActiveModelSerializers expects to be implemented.
29
+
30
+ An implementation of the complete spec is included either for use or as reference:
31
+ [`ActiveModelSerializers::Model`](../../lib/active_model_serializers/model.rb).
32
+ You can use in production code that will make your PORO a lot cleaner.
33
+
34
+ The above code now becomes:
35
+
25
36
  ```ruby
26
37
  # my_model.rb
27
38
  class MyModel < ActiveModelSerializers::Model
28
- attr_accessor :id, :name, :level
39
+ attributes :id, :name, :level
40
+ end
41
+ ```
42
+
43
+ The default serializer would be `MyModelSerializer`.
44
+
45
+ *IMPORTANT*: There is a surprising behavior (bug) in the current implementation of ActiveModelSerializers::Model that
46
+ prevents an accessor from modifying attributes on the instance. The fix for this bug
47
+ is a breaking change, so we have made an opt-in configuration.
48
+
49
+ New applications should set:
50
+
51
+ ```ruby
52
+ ActiveModelSerializers::Model.derive_attributes_from_names_and_fix_accessors
53
+ ```
54
+
55
+ Existing applications can use the fix *and* avoid breaking changes
56
+ by making a superclass for new models. For example:
57
+
58
+ ```ruby
59
+ class SerializablePoro < ActiveModelSerializers::Model
60
+ derive_attributes_from_names_and_fix_accessors
29
61
  end
30
62
  ```
31
63
 
32
- The default serializer would be `MyModelSerializer`.
64
+ So that `MyModel` above would inherit from `SerializablePoro`.
65
+
66
+ `derive_attributes_from_names_and_fix_accessors` prepends the `DeriveAttributesFromNamesAndFixAccessors`
67
+ module and does the following:
68
+
69
+ - `id` will *always* be in the attributes. (This is until we separate out the caching requirement for POROs.)
70
+ - Overwrites the `attributes` method to that it only returns declared attributes.
71
+ `attributes` will now be a frozen hash with indifferent access.
72
+
73
+ For more information, see [README: What does a 'serializable resource' look like?](../../README.md#what-does-a-serializable-resource-look-like).
@@ -107,7 +107,7 @@ end
107
107
 
108
108
  ```
109
109
  Add this class to your app however you see fit. This is the class that your existing serializers
110
- that inherit from `ActiveMode::Serializer` should inherit from.
110
+ that inherit from `ActiveModel::Serializer` should inherit from.
111
111
 
112
112
  ### 3. Add `ActiveModel::V08::CollectionSerializer`
113
113
  ```ruby
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Serializer
3
- VERSION = '0.10.4'.freeze
3
+ VERSION = '0.10.5'.freeze
4
4
  end
5
5
  end
@@ -38,6 +38,14 @@ module ActiveModelSerializers
38
38
  @default_include_directive ||= JSONAPI::IncludeDirective.new(config.default_includes, allow_wildcard: true)
39
39
  end
40
40
 
41
+ def self.silence_warnings
42
+ original_verbose = $VERBOSE
43
+ $VERBOSE = nil
44
+ yield
45
+ ensure
46
+ $VERBOSE = original_verbose
47
+ end
48
+
41
49
  require 'active_model/serializer/version'
42
50
  require 'active_model/serializer'
43
51
  require 'active_model/serializable_resource'