active_model_serializers 0.8.3 → 0.9.1

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 (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -5
  3. data/CONTRIBUTING.md +20 -0
  4. data/DESIGN.textile +4 -4
  5. data/{MIT-LICENSE.txt → MIT-LICENSE} +0 -0
  6. data/README.md +395 -95
  7. data/lib/action_controller/serialization.rb +50 -12
  8. data/lib/action_controller/serialization_test_case.rb +79 -0
  9. data/lib/active_model/array_serializer.rb +46 -78
  10. data/lib/active_model/default_serializer.rb +32 -0
  11. data/lib/active_model/serializable/utils.rb +16 -0
  12. data/lib/active_model/serializable.rb +62 -0
  13. data/lib/active_model/serializer/association/has_many.rb +39 -0
  14. data/lib/active_model/serializer/association/has_one.rb +25 -0
  15. data/lib/active_model/serializer/association.rb +58 -0
  16. data/lib/active_model/serializer/config.rb +31 -0
  17. data/lib/active_model/serializer/generators/resource_override.rb +13 -0
  18. data/lib/{generators → active_model/serializer/generators}/serializer/USAGE +0 -0
  19. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
  20. data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +37 -0
  21. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
  22. data/lib/active_model/serializer/generators/serializer/templates/serializer.rb +8 -0
  23. data/lib/active_model/serializer/railtie.rb +18 -0
  24. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  25. data/lib/active_model/serializer.rb +209 -422
  26. data/lib/active_model/serializer_support.rb +5 -0
  27. data/lib/active_model_serializers.rb +12 -87
  28. data/test/fixtures/active_record.rb +92 -0
  29. data/test/fixtures/poro.rb +184 -0
  30. data/test/fixtures/template.html.erb +1 -0
  31. data/test/integration/action_controller/namespaced_serialization_test.rb +96 -0
  32. data/test/integration/action_controller/serialization_test.rb +303 -0
  33. data/test/integration/action_controller/serialization_test_case_test.rb +71 -0
  34. data/test/integration/active_record/active_record_test.rb +77 -0
  35. data/test/integration/generators/resource_generator_test.rb +26 -0
  36. data/test/integration/generators/scaffold_controller_generator_test.rb +64 -0
  37. data/test/integration/generators/serializer_generator_test.rb +41 -0
  38. data/test/test_app.rb +14 -0
  39. data/test/test_helper.rb +10 -18
  40. data/test/tmp/app/serializers/account_serializer.rb +3 -0
  41. data/test/unit/active_model/array_serializer/except_test.rb +18 -0
  42. data/test/unit/active_model/array_serializer/key_format_test.rb +18 -0
  43. data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
  44. data/test/unit/active_model/array_serializer/only_test.rb +18 -0
  45. data/test/unit/active_model/array_serializer/root_test.rb +102 -0
  46. data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
  47. data/test/unit/active_model/array_serializer/serialization_test.rb +199 -0
  48. data/test/unit/active_model/default_serializer_test.rb +13 -0
  49. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +36 -0
  50. data/test/unit/active_model/serializer/associations_test.rb +19 -0
  51. data/test/unit/active_model/serializer/attributes_test.rb +57 -0
  52. data/test/unit/active_model/serializer/config_test.rb +91 -0
  53. data/test/unit/active_model/serializer/filter_test.rb +69 -0
  54. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +189 -0
  55. data/test/unit/active_model/serializer/has_many_test.rb +265 -0
  56. data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +27 -0
  57. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +196 -0
  58. data/test/unit/active_model/serializer/has_one_test.rb +239 -0
  59. data/test/unit/active_model/serializer/key_format_test.rb +25 -0
  60. data/test/unit/active_model/serializer/meta_test.rb +39 -0
  61. data/test/unit/active_model/serializer/options_test.rb +34 -0
  62. data/test/unit/active_model/serializer/root_test.rb +117 -0
  63. data/test/unit/active_model/serializer/scope_test.rb +49 -0
  64. data/test/unit/active_model/serializer/url_helpers_test.rb +35 -0
  65. metadata +104 -63
  66. data/.gitignore +0 -18
  67. data/.travis.yml +0 -28
  68. data/Gemfile +0 -4
  69. data/Gemfile.edge +0 -9
  70. data/Rakefile +0 -18
  71. data/active_model_serializers.gemspec +0 -24
  72. data/bench/perf.rb +0 -43
  73. data/cruft.md +0 -19
  74. data/lib/active_model/serializer/associations.rb +0 -233
  75. data/lib/active_record/serializer_override.rb +0 -16
  76. data/lib/generators/resource_override.rb +0 -13
  77. data/lib/generators/serializer/serializer_generator.rb +0 -42
  78. data/lib/generators/serializer/templates/serializer.rb +0 -19
  79. data/test/array_serializer_test.rb +0 -75
  80. data/test/association_test.rb +0 -592
  81. data/test/caching_test.rb +0 -96
  82. data/test/generators_test.rb +0 -85
  83. data/test/no_serialization_scope_test.rb +0 -34
  84. data/test/serialization_scope_name_test.rb +0 -67
  85. data/test/serialization_test.rb +0 -392
  86. data/test/serializer_support_test.rb +0 -51
  87. data/test/serializer_test.rb +0 -1465
  88. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+ require 'active_model/serializer'
3
+
4
+ module ActiveModel
5
+ class ArraySerializer
6
+ class MetaTest < Minitest::Test
7
+ def setup
8
+ @profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
+ @profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
10
+ @serializer = ArraySerializer.new([@profile1, @profile2], root: 'profiles')
11
+ end
12
+
13
+ def test_meta
14
+ @serializer.meta = { total: 10 }
15
+
16
+ assert_equal({
17
+ 'profiles' => [
18
+ {
19
+ name: 'Name 1',
20
+ description: 'Description 1'
21
+ }, {
22
+ name: 'Name 2',
23
+ description: 'Description 2'
24
+ }
25
+ ],
26
+ meta: {
27
+ total: 10
28
+ }
29
+ }, @serializer.as_json)
30
+ end
31
+
32
+ def test_meta_using_meta_key
33
+ @serializer.meta_key = :my_meta
34
+ @serializer.meta = { total: 10 }
35
+
36
+ assert_equal({
37
+ 'profiles' => [
38
+ {
39
+ name: 'Name 1',
40
+ description: 'Description 1'
41
+ }, {
42
+ name: 'Name 2',
43
+ description: 'Description 2'
44
+ }
45
+ ],
46
+ my_meta: {
47
+ total: 10
48
+ }
49
+ }, @serializer.as_json)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class ArraySerializer
5
+ class OnlyTest < Minitest::Test
6
+ def test_array_serializer_pass_only_to_items_serializers
7
+ array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
8
+ Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
9
+ serializer = ArraySerializer.new(array, only: [:name])
10
+
11
+ expected = [{ name: 'Name 1' },
12
+ { name: 'Name 2' }]
13
+
14
+ assert_equal expected, serializer.serializable_array
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,102 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class ArraySerializer
5
+ class RootAsOptionTest < Minitest::Test
6
+ def setup
7
+ @old_root = ArraySerializer._root
8
+ @profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
+ @profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
10
+ @serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
11
+ end
12
+
13
+ def teardown
14
+ ArraySerializer._root = @old_root
15
+ end
16
+
17
+ def test_root_is_not_displayed_using_serializable_array
18
+ assert_equal([
19
+ { name: 'Name 1', description: 'Description 1' },
20
+ { name: 'Name 2', description: 'Description 2' }
21
+ ], @serializer.serializable_array)
22
+ end
23
+
24
+ def test_root_using_as_json
25
+ assert_equal({
26
+ initialize: [
27
+ { name: 'Name 1', description: 'Description 1' },
28
+ { name: 'Name 2', description: 'Description 2' }
29
+ ]
30
+ }, @serializer.as_json)
31
+ end
32
+
33
+ def test_root_as_argument_takes_precedence
34
+ assert_equal({
35
+ argument: [
36
+ { name: 'Name 1', description: 'Description 1' },
37
+ { name: 'Name 2', description: 'Description 2' }
38
+ ]
39
+ }, @serializer.as_json(root: :argument))
40
+ end
41
+
42
+ def test_using_false_root_in_initialize_takes_precedence
43
+ ArraySerializer._root = 'root'
44
+ @serializer = ArraySerializer.new([@profile1, @profile2], root: false)
45
+
46
+ assert_equal([
47
+ { name: 'Name 1', description: 'Description 1' },
48
+ { name: 'Name 2', description: 'Description 2' }
49
+ ], @serializer.as_json)
50
+ end
51
+ end
52
+
53
+ class RootInSerializerTest < Minitest::Test
54
+ def setup
55
+ @old_root = ArraySerializer._root
56
+ ArraySerializer._root = :in_serializer
57
+ @profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
58
+ @profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
59
+ @serializer = ArraySerializer.new([@profile1, @profile2])
60
+ @rooted_serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
61
+ end
62
+
63
+ def teardown
64
+ ArraySerializer._root = @old_root
65
+ end
66
+
67
+ def test_root_is_not_displayed_using_serializable_hash
68
+ assert_equal([
69
+ { name: 'Name 1', description: 'Description 1' },
70
+ { name: 'Name 2', description: 'Description 2' }
71
+ ], @serializer.serializable_array)
72
+ end
73
+
74
+ def test_root_using_as_json
75
+ assert_equal({
76
+ in_serializer: [
77
+ { name: 'Name 1', description: 'Description 1' },
78
+ { name: 'Name 2', description: 'Description 2' }
79
+ ]
80
+ }, @serializer.as_json)
81
+ end
82
+
83
+ def test_root_in_initializer_takes_precedence
84
+ assert_equal({
85
+ initialize: [
86
+ { name: 'Name 1', description: 'Description 1' },
87
+ { name: 'Name 2', description: 'Description 2' }
88
+ ]
89
+ }, @rooted_serializer.as_json)
90
+ end
91
+
92
+ def test_root_as_argument_takes_precedence
93
+ assert_equal({
94
+ argument: [
95
+ { name: 'Name 1', description: 'Description 1' },
96
+ { name: 'Name 2', description: 'Description 2' }
97
+ ]
98
+ }, @rooted_serializer.as_json(root: :argument))
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class ArraySerializer
5
+ class ScopeTest < Minitest::Test
6
+ def test_array_serializer_pass_options_to_items_serializers
7
+ array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
8
+ Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
9
+ serializer = ArraySerializer.new(array, scope: current_user)
10
+
11
+ expected = [{ name: 'Name 1', description: 'Description 1 - user' },
12
+ { name: 'Name 2', description: 'Description 2 - user' }]
13
+
14
+ assert_equal expected, serializer.serializable_array
15
+ end
16
+
17
+ private
18
+
19
+ def current_user
20
+ 'user'
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,199 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class ArraySerializer
5
+ class BasicObjectsSerializationTest < Minitest::Test
6
+ def setup
7
+ array = [1, 2, 3]
8
+ @serializer = Serializer.serializer_for(array).new(array)
9
+ end
10
+
11
+ def test_serializer_for_array_returns_appropriate_type
12
+ assert_kind_of ActiveModel::ArraySerializer, @serializer
13
+ end
14
+
15
+ def test_array_serializer_serializes_simple_objects
16
+ assert_equal [1, 2, 3], @serializer.serializable_array
17
+ assert_equal [1, 2, 3], @serializer.as_json
18
+ end
19
+ end
20
+
21
+ class CustomArraySerializerSupport < Minitest::Test
22
+ def setup
23
+ Object.const_set(:ArraySerializer, Class.new)
24
+
25
+ array = [1, 2, 3]
26
+ @serializer_class = Serializer.serializer_for(array)
27
+ end
28
+
29
+ def teardown
30
+ Object.send(:remove_const, :ArraySerializer)
31
+ end
32
+
33
+ def test_serializer_for_array_returns_appropriate_type
34
+ assert_equal ::ArraySerializer, @serializer_class
35
+ end
36
+ end
37
+
38
+ class ModelSerializationTest < Minitest::Test
39
+ def test_array_serializer_serializes_models
40
+ array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
41
+ Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
42
+ serializer = ArraySerializer.new(array)
43
+
44
+ expected = [{ name: 'Name 1', description: 'Description 1' },
45
+ { name: 'Name 2', description: 'Description 2' }]
46
+
47
+ assert_equal expected, serializer.serializable_array
48
+ assert_equal expected, serializer.as_json
49
+ end
50
+
51
+ def test_array_serializers_each_serializer
52
+ array = [::Model.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
53
+ ::Model.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
54
+ serializer = ArraySerializer.new(array, each_serializer: ProfileSerializer)
55
+
56
+ expected = [{ name: 'Name 1', description: 'Description 1' },
57
+ { name: 'Name 2', description: 'Description 2' }]
58
+
59
+ assert_equal expected, serializer.serializable_array
60
+ assert_equal expected, serializer.as_json
61
+ end
62
+
63
+ def test_associated_objects_of_multiple_instances_embedded_in_root
64
+ @association = PostSerializer._associations[:comments]
65
+ @old_association = @association.dup
66
+
67
+ @association.embed = :ids
68
+ @association.embed_in_root = true
69
+
70
+ @post1 = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
71
+ @post2 = Post.new({ title: 'Title 2', body: 'Body 2', date: '1/1/2000' })
72
+
73
+ class << @post2
74
+ attr_writer :comments
75
+ end
76
+
77
+ @post2.comments = [
78
+ Comment.new(content: 'C3'),
79
+ Comment.new(content: 'C4')
80
+ ]
81
+
82
+ @serializer = ArraySerializer.new([@post1, @post2], root: :posts)
83
+ assert_equal({
84
+ posts: [
85
+ {title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
86
+ {title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
87
+ ],
88
+ 'comments' => [
89
+ {content: "C1"},
90
+ {content: "C2"},
91
+ {content: "C3"},
92
+ {content: "C4"}
93
+ ]
94
+ }, @serializer.as_json)
95
+ ensure
96
+ PostSerializer._associations[:comments] = @old_association
97
+ end
98
+
99
+ def test_embed_object_for_has_one_association_with_nil_value
100
+ @association = UserSerializer._associations[:profile]
101
+ @old_association = @association.dup
102
+
103
+ @association.embed = :objects
104
+
105
+ @user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
106
+ @user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
107
+
108
+ class << @user1
109
+ def profile
110
+ nil
111
+ end
112
+ end
113
+
114
+ class << @user2
115
+ def profile
116
+ @profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
117
+ end
118
+ end
119
+
120
+ @serializer = ArraySerializer.new([@user1, @user2]) #, root: :posts)
121
+ assert_equal([
122
+ { name: "User 1", email: "email1@server.com", profile: nil },
123
+ { name: "User 2", email: "email2@server.com", profile: { name: 'Name 1', description: 'Desc 1' } }
124
+ ], @serializer.as_json)
125
+ ensure
126
+ UserSerializer._associations[:profile] = @old_association
127
+ end
128
+
129
+ def test_embed_object_in_root_for_has_one_association_with_nil_value
130
+ @association = UserSerializer._associations[:profile]
131
+ @old_association = @association.dup
132
+
133
+ @association.embed = :ids
134
+ @association.embed_in_root = true
135
+
136
+ @user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
137
+ @user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
138
+
139
+ class << @user1
140
+ def profile
141
+ nil
142
+ end
143
+ end
144
+
145
+ class << @user2
146
+ def profile
147
+ @profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
148
+ end
149
+ end
150
+
151
+ @serializer = ArraySerializer.new([@user1, @user2], root: :users)
152
+ assert_equal({
153
+ users: [
154
+ { name: "User 1", email: "email1@server.com", 'profile_id' => nil },
155
+ { name: "User 2", email: "email2@server.com", 'profile_id' => @user2.profile.object_id }
156
+ ],
157
+ 'profiles' => [
158
+ { name: 'Name 1', description: 'Desc 1' }
159
+ ]
160
+ }, @serializer.as_json)
161
+ ensure
162
+ UserSerializer._associations[:profile] = @old_association
163
+ end
164
+
165
+ def test_embed_object_in_root_for_has_one_association_with_all_nil_values
166
+ @association = UserSerializer._associations[:profile]
167
+ @old_association = @association.dup
168
+
169
+ @association.embed = :ids
170
+ @association.embed_in_root = true
171
+
172
+ @user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
173
+ @user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
174
+
175
+ class << @user1
176
+ def profile
177
+ nil
178
+ end
179
+ end
180
+
181
+ class << @user2
182
+ def profile
183
+ nil
184
+ end
185
+ end
186
+
187
+ @serializer = ArraySerializer.new([@user1, @user2], root: :users)
188
+ assert_equal({
189
+ users: [
190
+ { name: "User 1", email: "email1@server.com", 'profile_id' => nil },
191
+ { name: "User 2", email: "email2@server.com", 'profile_id' => nil }
192
+ ]
193
+ }, @serializer.as_json)
194
+ ensure
195
+ UserSerializer._associations[:profile] = @old_association
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class DefaultSerializer
5
+ class Test < Minitest::Test
6
+ def test_serialize_objects
7
+ assert_equal(nil, DefaultSerializer.new(nil).serializable_object)
8
+ assert_equal(1, DefaultSerializer.new(1).serializable_object)
9
+ assert_equal('hi', DefaultSerializer.new('hi').serializable_object)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Association
6
+ class BuildSerializerTest < Minitest::Test
7
+ def setup
8
+ @association = Association::HasOne.new('post', serializer: PostSerializer)
9
+ @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
10
+ @user = User.new
11
+ end
12
+
13
+ def test_build_serializer_for_array_called_twice
14
+ 2.times do
15
+ serializer = @association.build_serializer(@post)
16
+ assert_instance_of(PostSerializer, serializer)
17
+ end
18
+ end
19
+
20
+ def test_build_serializer_from_in_a_namespace
21
+ assoc = Association::HasOne.new('profile')
22
+ serializer = TestNamespace::UserSerializer.new(@user).build_serializer(assoc)
23
+
24
+ assert_instance_of(TestNamespace::ProfileSerializer, serializer)
25
+ end
26
+
27
+ def test_build_serializer_with_prefix
28
+ assoc = Association::HasOne.new('profile', prefix: :short)
29
+ serializer = UserSerializer.new(@user).build_serializer(assoc)
30
+
31
+ assert_instance_of(ShortProfileSerializer, serializer)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AssociationsTest < Minitest::Test
6
+ def test_associations_inheritance
7
+ inherited_serializer_klass = Class.new(PostSerializer) do
8
+ has_many :users
9
+ end
10
+ another_inherited_serializer_klass = Class.new(PostSerializer)
11
+
12
+ assert_equal([:comments, :users],
13
+ inherited_serializer_klass._associations.keys)
14
+ assert_equal([:comments],
15
+ another_inherited_serializer_klass._associations.keys)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AttributesTest < Minitest::Test
6
+ def setup
7
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8
+ @profile_serializer = ProfileSerializer.new(@profile)
9
+ end
10
+
11
+ def test_attributes_definition
12
+ assert_equal([:name, :description],
13
+ @profile_serializer.class._attributes)
14
+ end
15
+
16
+ def test_attributes_serialization_using_serializable_hash
17
+ assert_equal({
18
+ name: 'Name 1', description: 'Description 1'
19
+ }, @profile_serializer.serializable_hash)
20
+ end
21
+
22
+ def test_attributes_serialization_using_as_json
23
+ assert_equal({
24
+ 'profile' => { name: 'Name 1', description: 'Description 1' }
25
+ }, @profile_serializer.as_json)
26
+ end
27
+
28
+ def test_attributes_inheritance
29
+ inherited_serializer_klass = Class.new(ProfileSerializer) do
30
+ attributes :comments
31
+ end
32
+ another_inherited_serializer_klass = Class.new(ProfileSerializer)
33
+
34
+ assert_equal([:name, :description, :comments],
35
+ inherited_serializer_klass._attributes)
36
+ assert_equal([:name, :description],
37
+ another_inherited_serializer_klass._attributes)
38
+ end
39
+
40
+ def tests_query_attributes_strip_question_mark
41
+ model = Class.new(::Model) do
42
+ def strip?
43
+ true
44
+ end
45
+ end
46
+
47
+ serializer = Class.new(ActiveModel::Serializer) do
48
+ attributes :strip?
49
+ end
50
+
51
+ actual = serializer.new(model.new).as_json
52
+
53
+ assert_equal({ strip: true }, actual)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,91 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Config
6
+ class Test < Minitest::Test
7
+ def test_config_const_is_an_instance_of_config
8
+ assert_kind_of Config, CONFIG
9
+ end
10
+
11
+ def test_config_instance
12
+ config = Config.new
13
+ config.setting1 = 'value1'
14
+
15
+ assert_equal 'value1', config.setting1
16
+ end
17
+
18
+ def test_each_config
19
+ config = Config.new
20
+ config.setting1 = 'value1'
21
+ config.setting2 = 'value2'
22
+
23
+ actual = {}
24
+
25
+ config.each do |k, v|
26
+ actual[k] = v
27
+ end
28
+
29
+ assert_equal({ 'setting1' => 'value1', 'setting2' => 'value2' }, actual)
30
+ end
31
+ end
32
+
33
+ class ConfigTest < Minitest::Test
34
+ def test_setup
35
+ Serializer.setup do |config|
36
+ config.a = 'v1'
37
+ config.b = 'v2'
38
+ end
39
+
40
+ assert_equal 'v1', CONFIG.a
41
+ assert_equal 'v2', CONFIG.b
42
+ ensure
43
+ CONFIG.clear
44
+ end
45
+
46
+ def test_config_accessors
47
+ Serializer.setup do |config|
48
+ config.foo = 'v1'
49
+ config.bar = 'v2'
50
+ end
51
+
52
+ assert_equal 'v1', CONFIG.foo
53
+ assert_equal 'v2', CONFIG.bar
54
+ ensure
55
+ CONFIG.clear
56
+ end
57
+
58
+ def test_acessor_when_nil
59
+ assert_nil CONFIG.foo
60
+ CONFIG.foo = 1
61
+ assert_equal 1, CONFIG.foo
62
+ assert_nil CONFIG.bar
63
+ end
64
+ end
65
+
66
+ class ApplyConfigTest < Minitest::Test
67
+ def test_apply_config_to_associations
68
+ CONFIG.embed = :ids
69
+ CONFIG.embed_in_root = true
70
+ CONFIG.key_format = :lower_camel
71
+
72
+ association = PostSerializer._associations[:comments]
73
+ old_association = association.dup
74
+
75
+ association.send :initialize, association.name, association.options
76
+
77
+ assert association.embed_ids?
78
+ assert !association.embed_objects?
79
+ assert association.embed_in_root
80
+ assert_equal :lower_camel, association.key_format
81
+ assert_equal 'post', PostSerializer.root_name
82
+ CONFIG.plural_default_root = true
83
+ assert_equal 'posts', PostSerializer.root_name
84
+ ensure
85
+ PostSerializer._associations[:comments] = old_association
86
+ CONFIG.clear
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class FilterOptionsTest < Minitest::Test
6
+ def setup
7
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8
+ end
9
+
10
+ def test_only_option
11
+ @profile_serializer = ProfileSerializer.new(@profile, only: :name)
12
+ assert_equal({
13
+ 'profile' => { name: 'Name 1' }
14
+ }, @profile_serializer.as_json)
15
+ end
16
+
17
+ def test_except_option
18
+ @profile_serializer = ProfileSerializer.new(@profile, except: :comments)
19
+ assert_equal({
20
+ 'profile' => { name: 'Name 1', description: 'Description 1' }
21
+ }, @profile_serializer.as_json)
22
+ end
23
+ end
24
+
25
+ class FilterAttributesTest < Minitest::Test
26
+ def setup
27
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
28
+ @profile_serializer = ProfileSerializer.new(@profile)
29
+ @profile_serializer.instance_eval do
30
+ def filter(keys)
31
+ keys - [:description]
32
+ end
33
+ end
34
+ end
35
+
36
+ def test_filtered_attributes_serialization
37
+ assert_equal({
38
+ 'profile' => { name: 'Name 1' }
39
+ }, @profile_serializer.as_json)
40
+ end
41
+ end
42
+
43
+ class FilterAssociationsTest < Minitest::Test
44
+ def setup
45
+ @association = PostSerializer._associations[:comments]
46
+ @old_association = @association.dup
47
+ @association.embed = :ids
48
+ @association.embed_in_root = true
49
+ @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
50
+ @post_serializer = PostSerializer.new(@post)
51
+ @post_serializer.instance_eval do
52
+ def filter(keys)
53
+ keys - [:body, :comments]
54
+ end
55
+ end
56
+ end
57
+
58
+ def teardown
59
+ PostSerializer._associations[:comments] = @old_association
60
+ end
61
+
62
+ def test_filtered_associations_serialization
63
+ assert_equal({
64
+ 'post' => { title: 'Title 1' }
65
+ }, @post_serializer.as_json)
66
+ end
67
+ end
68
+ end
69
+ end