active_model_serializers 0.10.4 → 0.10.5

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -67,6 +67,7 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
67
67
  yield
68
68
  ensure
69
69
  String.class_eval do
70
+ undef_method :safe_constantize
70
71
  alias_method :safe_constantize, :old
71
72
  undef_method :old
72
73
  end
data/test/grape_test.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'test_helper'
2
- require 'grape'
2
+ TestHelper.silence_warnings do
3
+ require 'grape'
4
+ end
3
5
  require 'grape/active_model_serializers'
4
6
  require 'kaminari'
5
7
  require 'kaminari/hooks'
@@ -53,7 +55,15 @@ module ActiveModelSerializers
53
55
 
54
56
  class GrapeTest < Grape::API
55
57
  format :json
56
- include Grape::ActiveModelSerializers
58
+ TestHelper.silence_warnings do
59
+ include Grape::ActiveModelSerializers
60
+ end
61
+
62
+ def self.resources(*)
63
+ TestHelper.silence_warnings do
64
+ super
65
+ end
66
+ end
57
67
 
58
68
  resources :grape do
59
69
  get '/render' do
@@ -93,6 +103,14 @@ module ActiveModelSerializers
93
103
  Grape::Middleware::Globals.new(GrapeTest.new)
94
104
  end
95
105
 
106
+ extend Minitest::Assertions
107
+ def self.run_one_method(*)
108
+ _, stderr = capture_io do
109
+ super
110
+ end
111
+ fail Minitest::Assertion, stderr if stderr !~ /grape/
112
+ end
113
+
96
114
  def test_formatter_returns_json
97
115
  get '/grape/render'
98
116
 
@@ -2,13 +2,17 @@ require 'test_helper'
2
2
  module ActiveModel
3
3
  class Serializer
4
4
  class AssociationsTest < ActiveSupport::TestCase
5
+ class ModelWithoutSerializer < ::Model
6
+ attributes :id, :name
7
+ end
8
+
5
9
  def setup
6
10
  @author = Author.new(name: 'Steve K.')
7
11
  @author.bio = nil
8
12
  @author.roles = []
9
13
  @blog = Blog.new(name: 'AMS Blog')
10
14
  @post = Post.new(title: 'New Post', body: 'Body')
11
- @tag = Tag.new(name: '#hashtagged')
15
+ @tag = ModelWithoutSerializer.new(id: 'tagid', name: '#hashtagged')
12
16
  @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
13
17
  @post.comments = [@comment]
14
18
  @post.tags = [@tag]
@@ -46,14 +50,18 @@ module ActiveModel
46
50
  end
47
51
 
48
52
  def test_has_many_with_no_serializer
49
- PostWithTagsSerializer.new(@post).associations.each do |association|
53
+ post_serializer_class = Class.new(ActiveModel::Serializer) do
54
+ attributes :id
55
+ has_many :tags
56
+ end
57
+ post_serializer_class.new(@post).associations.each do |association|
50
58
  key = association.key
51
59
  serializer = association.serializer
52
60
  options = association.options
53
61
 
54
62
  assert_equal :tags, key
55
63
  assert_nil serializer
56
- assert_equal [{ name: '#hashtagged' }].to_json, options[:virtual_value].to_json
64
+ assert_equal [{ id: 'tagid', name: '#hashtagged' }].to_json, options[:virtual_value].to_json
57
65
  end
58
66
  end
59
67
 
@@ -62,7 +70,13 @@ module ActiveModel
62
70
  .associations
63
71
  .detect { |assoc| assoc.key == :comments }
64
72
 
65
- assert association.serializer.first.custom_options[:custom_options]
73
+ comment_serializer = association.serializer.first
74
+ class << comment_serializer
75
+ def custom_options
76
+ instance_options
77
+ end
78
+ end
79
+ assert comment_serializer.custom_options.fetch(:custom_options)
66
80
  end
67
81
 
68
82
  def test_belongs_to
@@ -159,7 +173,9 @@ module ActiveModel
159
173
 
160
174
  class NamespacedResourcesTest < ActiveSupport::TestCase
161
175
  class ResourceNamespace
162
- class Post < ::Model; end
176
+ class Post < ::Model
177
+ associations :comments, :author, :description
178
+ end
163
179
  class Comment < ::Model; end
164
180
  class Author < ::Model; end
165
181
  class Description < ::Model; end
@@ -200,7 +216,9 @@ module ActiveModel
200
216
  end
201
217
 
202
218
  class NestedSerializersTest < ActiveSupport::TestCase
203
- class Post < ::Model; end
219
+ class Post < ::Model
220
+ associations :comments, :author, :description
221
+ end
204
222
  class Comment < ::Model; end
205
223
  class Author < ::Model; end
206
224
  class Description < ::Model; end
@@ -240,7 +258,10 @@ module ActiveModel
240
258
 
241
259
  # rubocop:disable Metrics/AbcSize
242
260
  def test_conditional_associations
243
- model = ::Model.new(true: true, false: false)
261
+ model = Class.new(::Model) do
262
+ attributes :true, :false
263
+ associations :association
264
+ end.new(true: true, false: false)
244
265
 
245
266
  scenarios = [
246
267
  { options: { if: :true }, included: true },
@@ -81,7 +81,7 @@ module ActiveModel
81
81
  assert_equal('custom', hash[:blog][:id])
82
82
  end
83
83
 
84
- class PostWithVirtualAttribute < ::Model; end
84
+ class PostWithVirtualAttribute < ::Model; attributes :first_name, :last_name end
85
85
  class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
86
86
  attribute :name do
87
87
  "#{object.first_name} #{object.last_name}"
@@ -98,7 +98,9 @@ module ActiveModel
98
98
 
99
99
  # rubocop:disable Metrics/AbcSize
100
100
  def test_conditional_associations
101
- model = ::Model.new(true: true, false: false)
101
+ model = Class.new(::Model) do
102
+ attributes :true, :false, :attribute
103
+ end.new(true: true, false: false)
102
104
 
103
105
  scenarios = [
104
106
  { options: { if: :true }, included: true },
@@ -69,9 +69,9 @@ class CachingConfigurationTest < ActiveSupport::TestCase
69
69
  end
70
70
 
71
71
  test 'the non-cached serializer cache_store is nil' do
72
- assert_equal nil, @non_cached_serializer._cache
73
- assert_equal nil, @non_cached_serializer.cache_store
74
- assert_equal nil, @non_cached_serializer._cache
72
+ assert_nil @non_cached_serializer._cache
73
+ assert_nil @non_cached_serializer.cache_store
74
+ assert_nil @non_cached_serializer._cache
75
75
  end
76
76
 
77
77
  test 'the non-cached serializer does not have cache_enabled?' do
@@ -136,9 +136,9 @@ class CachingConfigurationTest < ActiveSupport::TestCase
136
136
  end
137
137
 
138
138
  test 'the non-cached serializer cache_store is nil' do
139
- assert_equal nil, @non_cached_serializer._cache
140
- assert_equal nil, @non_cached_serializer.cache_store
141
- assert_equal nil, @non_cached_serializer._cache
139
+ assert_nil @non_cached_serializer._cache
140
+ assert_nil @non_cached_serializer.cache_store
141
+ assert_nil @non_cached_serializer._cache
142
142
  end
143
143
 
144
144
  test 'the non-cached serializer does not have cache_enabled?' do
@@ -3,18 +3,29 @@ require 'test_helper'
3
3
  module ActiveModel
4
4
  class Serializer
5
5
  class OptionsTest < ActiveSupport::TestCase
6
- def setup
7
- @profile = Profile.new(name: 'Name 1', description: 'Description 1')
6
+ class ModelWithOptions < ActiveModelSerializers::Model
7
+ attributes :name, :description
8
+ end
9
+ class ModelWithOptionsSerializer < ActiveModel::Serializer
10
+ attributes :name, :description
11
+
12
+ def arguments_passed_in?
13
+ instance_options[:my_options] == :accessible
14
+ end
15
+ end
16
+
17
+ setup do
18
+ @model_with_options = ModelWithOptions.new(name: 'Name 1', description: 'Description 1')
8
19
  end
9
20
 
10
21
  def test_options_are_accessible
11
- @profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
12
- assert @profile_serializer.arguments_passed_in?
22
+ model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options, my_options: :accessible)
23
+ assert model_with_options_serializer.arguments_passed_in?
13
24
  end
14
25
 
15
26
  def test_no_option_is_passed_in
16
- @profile_serializer = ProfileSerializer.new(@profile)
17
- refute @profile_serializer.arguments_passed_in?
27
+ model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options)
28
+ refute model_with_options_serializer.arguments_passed_in?
18
29
  end
19
30
  end
20
31
  end
@@ -5,10 +5,10 @@ module ActiveModel
5
5
  class ReadAttributeForSerializationTest < ActiveSupport::TestCase
6
6
  # https://github.com/rails-api/active_model_serializers/issues/1653
7
7
  class Parent < ActiveModelSerializers::Model
8
- attr_accessor :id
8
+ attributes :id
9
9
  end
10
10
  class Child < Parent
11
- attr_accessor :name
11
+ attributes :name
12
12
  end
13
13
  class ParentSerializer < ActiveModel::Serializer
14
14
  attributes :$id
@@ -30,7 +30,7 @@ module ActiveModel
30
30
 
31
31
  # https://github.com/rails-api/active_model_serializers/issues/1658
32
32
  class ErrorResponse < ActiveModelSerializers::Model
33
- attr_accessor :error
33
+ attributes :error
34
34
  end
35
35
  class ApplicationSerializer < ActiveModel::Serializer
36
36
  attributes :status
@@ -2,10 +2,10 @@ module ActiveModel
2
2
  class Serializer
3
3
  class SerializationTest < ActiveSupport::TestCase
4
4
  class Blog < ActiveModelSerializers::Model
5
- attr_accessor :id, :name, :authors
5
+ attributes :id, :name, :authors
6
6
  end
7
7
  class Author < ActiveModelSerializers::Model
8
- attr_accessor :id, :name
8
+ attributes :id, :name
9
9
  end
10
10
  class BlogSerializer < ActiveModel::Serializer
11
11
  attributes :id
@@ -59,7 +59,7 @@ module ActiveModel
59
59
 
60
60
  def test_serializer_for_non_ams_serializer
61
61
  serializer = ActiveModel::Serializer.serializer_for(@tweet)
62
- assert_equal nil, serializer
62
+ assert_nil serializer
63
63
  end
64
64
 
65
65
  def test_serializer_for_existing_serializer
@@ -71,12 +71,12 @@ module ActiveModel
71
71
  serializer = with_serializer_lookup_disabled do
72
72
  ActiveModel::Serializer.serializer_for(@profile)
73
73
  end
74
- assert_equal nil, serializer
74
+ assert_nil serializer
75
75
  end
76
76
 
77
77
  def test_serializer_for_not_existing_serializer
78
78
  serializer = ActiveModel::Serializer.serializer_for(@model)
79
- assert_equal nil, serializer
79
+ assert_nil serializer
80
80
  end
81
81
 
82
82
  def test_serializer_inherited_serializer
@@ -88,7 +88,7 @@ module ActiveModel
88
88
  serializer = with_serializer_lookup_disabled do
89
89
  ActiveModel::Serializer.serializer_for(@my_profile)
90
90
  end
91
- assert_equal nil, serializer
91
+ assert_nil serializer
92
92
  end
93
93
 
94
94
  def test_serializer_custom_serializer
@@ -114,7 +114,7 @@ module ActiveModel
114
114
  serializer = with_serializer_lookup_disabled do
115
115
  ActiveModel::Serializer.serializer_for(post)
116
116
  end
117
- assert_equal nil, serializer
117
+ assert_nil serializer
118
118
  end
119
119
 
120
120
  def test_serializer_for_nested_resource
@@ -128,7 +128,7 @@ module ActiveModel
128
128
  serializer = with_serializer_lookup_disabled do
129
129
  ResourceNamespace::PostSerializer.serializer_for(comment)
130
130
  end
131
- assert_equal nil, serializer
131
+ assert_nil serializer
132
132
  end
133
133
  end
134
134
  end
@@ -3,9 +3,12 @@ require 'test_helper'
3
3
  module ActiveModel
4
4
  class Serializer
5
5
  class SerializerForWithNamespaceTest < ActiveSupport::TestCase
6
- class Book < ::Model; end
7
- class Page < ::Model; end
8
- class Publisher < ::Model; end
6
+ class Book < ::Model
7
+ attributes :title, :author_name
8
+ associations :publisher, :pages
9
+ end
10
+ class Page < ::Model; attributes :number, :text end
11
+ class Publisher < ::Model; attributes :name end
9
12
 
10
13
  module Api
11
14
  module V3
@@ -18,8 +21,6 @@ module ActiveModel
18
21
 
19
22
  class PageSerializer < ActiveModel::Serializer
20
23
  attributes :number, :text
21
-
22
- belongs_to :book
23
24
  end
24
25
 
25
26
  class PublisherSerializer < ActiveModel::Serializer
@@ -6,6 +6,8 @@ module ActiveModelSerializers
6
6
  config.active_support.test_order = :random
7
7
  config.action_controller.perform_caching = true
8
8
  config.action_controller.cache_store = :memory_store
9
+
10
+ config.filter_parameters += [:password]
9
11
  end
10
12
 
11
13
  app.routes.default_url_options = { host: 'example.com' }
data/test/test_helper.rb CHANGED
@@ -40,6 +40,18 @@ require 'minitest'
40
40
  require 'minitest/autorun'
41
41
  Minitest.backtrace_filter = Minitest::BacktraceFilter.new
42
42
 
43
+ module TestHelper
44
+ module_function
45
+
46
+ def silence_warnings
47
+ original_verbose = $VERBOSE
48
+ $VERBOSE = nil
49
+ yield
50
+ ensure
51
+ $VERBOSE = original_verbose
52
+ end
53
+ end
54
+
43
55
  require 'support/rails_app'
44
56
 
45
57
  # require "rails/test_help"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.4
4
+ version: 0.10.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Klabnik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-06 00:00:00.000000000 Z
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -71,19 +71,25 @@ dependencies:
71
71
  - !ruby/object:Gem::Version
72
72
  version: '6'
73
73
  - !ruby/object:Gem::Dependency
74
- name: jsonapi
74
+ name: jsonapi-renderer
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
- - - '='
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 0.1.1.beta1
80
+ - - "<"
78
81
  - !ruby/object:Gem::Version
79
- version: 0.1.1.beta6
82
+ version: '0.2'
80
83
  type: :runtime
81
84
  prerelease: false
82
85
  version_requirements: !ruby/object:Gem::Requirement
83
86
  requirements:
84
- - - '='
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.1.1.beta1
90
+ - - "<"
85
91
  - !ruby/object:Gem::Version
86
- version: 0.1.1.beta6
92
+ version: '0.2'
87
93
  - !ruby/object:Gem::Dependency
88
94
  name: case_transform
89
95
  requirement: !ruby/object:Gem::Requirement