active_model_serializers 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +0 -6
  3. data/README.md +17 -109
  4. data/lib/action_controller/serialization.rb +8 -27
  5. data/lib/action_controller/serialization_test_case.rb +4 -4
  6. data/lib/active_model/array_serializer.rb +5 -12
  7. data/lib/active_model/serializable.rb +2 -24
  8. data/lib/active_model/serializer.rb +36 -70
  9. data/lib/active_model/serializer/{association.rb → associations.rb} +52 -8
  10. data/lib/active_model/serializer/railtie.rb +0 -8
  11. data/lib/active_model/serializer/version.rb +1 -1
  12. data/lib/active_model_serializers.rb +1 -1
  13. data/test/fixtures/poro.rb +1 -110
  14. data/test/integration/action_controller/serialization_test.rb +0 -16
  15. data/test/integration/action_controller/serialization_test_case_test.rb +0 -10
  16. data/test/integration/active_record/active_record_test.rb +2 -2
  17. data/test/test_app.rb +0 -3
  18. data/test/unit/active_model/array_serializer/serialization_test.rb +1 -1
  19. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -15
  20. data/test/unit/active_model/serializer/attributes_test.rb +0 -16
  21. data/test/unit/active_model/serializer/config_test.rb +0 -3
  22. data/test/unit/active_model/serializer/has_many_test.rb +16 -51
  23. data/test/unit/active_model/serializer/has_one_test.rb +0 -32
  24. data/test/unit/active_model/serializer/options_test.rb +0 -19
  25. metadata +3 -18
  26. data/lib/active_model/serializable/utils.rb +0 -16
  27. data/lib/active_model/serializer/association/has_many.rb +0 -39
  28. data/lib/active_model/serializer/association/has_one.rb +0 -25
  29. data/test/fixtures/template.html.erb +0 -1
  30. data/test/integration/action_controller/namespaced_serialization_test.rb +0 -96
  31. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
  32. data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
  33. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
  34. data/test/unit/active_model/serializer/url_helpers_test.rb +0 -35
@@ -131,20 +131,6 @@ module ActiveModel
131
131
  }, @user_serializer.as_json)
132
132
  end
133
133
 
134
- def test_associations_embedding_ids_including_objects_serialization_when_invoked_from_parent_serializer
135
- @association.embed = :ids
136
- @association.embed_in_root = true
137
-
138
- user_info = UserInfo.new
139
- user_info.instance_variable_set(:@user, @user)
140
- user_info_serializer = UserInfoSerializer.new(user_info)
141
-
142
- assert_equal({
143
- 'user_info' => { user: { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id } },
144
- 'profiles' => [{ name: 'N1', description: 'D1' }]
145
- }, user_info_serializer.as_json)
146
- end
147
-
148
134
  def test_associations_embedding_ids_using_a_given_serializer
149
135
  @association.embed = :ids
150
136
  @association.embed_in_root = true
@@ -216,24 +202,6 @@ module ActiveModel
216
202
  }
217
203
  }, @user_serializer.as_json)
218
204
  end
219
-
220
- def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
221
- @association = NameKeyUserSerializer._associations[:profile]
222
- @association.embed = :ids
223
-
224
- assert_equal({
225
- name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id
226
- }, NameKeyUserSerializer.new(@user).serializable_hash)
227
- end
228
-
229
- def test_associations_name_key_embedding_ids_serialization_using_as_json
230
- @association = NameKeyUserSerializer._associations[:profile]
231
- @association.embed = :ids
232
-
233
- assert_equal({
234
- 'name_key_user' => { name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id }
235
- }, NameKeyUserSerializer.new(@user).as_json)
236
- end
237
205
  end
238
206
  end
239
207
  end
@@ -11,24 +11,5 @@ module ActiveModel
11
11
  assert_equal({foo: :bar}, @serializer.context)
12
12
  end
13
13
  end
14
-
15
- class SerializationOptionsTest < Minitest::Test
16
- def setup
17
- @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
18
- @profile_serializer = ProfileSerializer.new(@profile)
19
- @profile_serializer.instance_eval do
20
- def description
21
- serialization_options[:force_the_description]
22
- end
23
- end
24
- end
25
-
26
- def test_filtered_attributes_serialization
27
- forced_description = "This is a test"
28
- assert_equal({
29
- 'profile' => { name: 'Name 1', description: forced_description }
30
- }, @profile_serializer.as_json(force_the_description: forced_description))
31
- end
32
- end
33
14
  end
34
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Valim
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-12-04 00:00:00.000000000 Z
13
+ date: 2014-12-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
@@ -59,11 +59,8 @@ files:
59
59
  - lib/active_model/array_serializer.rb
60
60
  - lib/active_model/default_serializer.rb
61
61
  - lib/active_model/serializable.rb
62
- - lib/active_model/serializable/utils.rb
63
62
  - lib/active_model/serializer.rb
64
- - lib/active_model/serializer/association.rb
65
- - lib/active_model/serializer/association/has_many.rb
66
- - lib/active_model/serializer/association/has_one.rb
63
+ - lib/active_model/serializer/associations.rb
67
64
  - lib/active_model/serializer/config.rb
68
65
  - lib/active_model/serializer/generators/resource_override.rb
69
66
  - lib/active_model/serializer/generators/serializer/USAGE
@@ -77,8 +74,6 @@ files:
77
74
  - lib/active_model_serializers.rb
78
75
  - test/fixtures/active_record.rb
79
76
  - test/fixtures/poro.rb
80
- - test/fixtures/template.html.erb
81
- - test/integration/action_controller/namespaced_serialization_test.rb
82
77
  - test/integration/action_controller/serialization_test.rb
83
78
  - test/integration/action_controller/serialization_test_case_test.rb
84
79
  - test/integration/active_record/active_record_test.rb
@@ -101,17 +96,13 @@ files:
101
96
  - test/unit/active_model/serializer/attributes_test.rb
102
97
  - test/unit/active_model/serializer/config_test.rb
103
98
  - test/unit/active_model/serializer/filter_test.rb
104
- - test/unit/active_model/serializer/has_many_polymorphic_test.rb
105
99
  - test/unit/active_model/serializer/has_many_test.rb
106
- - test/unit/active_model/serializer/has_one_and_has_many_test.rb
107
- - test/unit/active_model/serializer/has_one_polymorphic_test.rb
108
100
  - test/unit/active_model/serializer/has_one_test.rb
109
101
  - test/unit/active_model/serializer/key_format_test.rb
110
102
  - test/unit/active_model/serializer/meta_test.rb
111
103
  - test/unit/active_model/serializer/options_test.rb
112
104
  - test/unit/active_model/serializer/root_test.rb
113
105
  - test/unit/active_model/serializer/scope_test.rb
114
- - test/unit/active_model/serializer/url_helpers_test.rb
115
106
  homepage: https://github.com/rails-api/active_model_serializers
116
107
  licenses:
117
108
  - MIT
@@ -141,7 +132,6 @@ test_files:
141
132
  - test/test_app.rb
142
133
  - test/tmp/app/serializers/account_serializer.rb
143
134
  - test/integration/active_record/active_record_test.rb
144
- - test/integration/action_controller/namespaced_serialization_test.rb
145
135
  - test/integration/action_controller/serialization_test.rb
146
136
  - test/integration/action_controller/serialization_test_case_test.rb
147
137
  - test/integration/generators/resource_generator_test.rb
@@ -157,21 +147,16 @@ test_files:
157
147
  - test/unit/active_model/array_serializer/key_format_test.rb
158
148
  - test/unit/active_model/serializer/config_test.rb
159
149
  - test/unit/active_model/serializer/has_one_test.rb
160
- - test/unit/active_model/serializer/url_helpers_test.rb
161
150
  - test/unit/active_model/serializer/has_many_test.rb
162
- - test/unit/active_model/serializer/has_one_and_has_many_test.rb
163
151
  - test/unit/active_model/serializer/filter_test.rb
164
152
  - test/unit/active_model/serializer/scope_test.rb
165
153
  - test/unit/active_model/serializer/meta_test.rb
166
154
  - test/unit/active_model/serializer/root_test.rb
167
- - test/unit/active_model/serializer/has_many_polymorphic_test.rb
168
155
  - test/unit/active_model/serializer/associations/build_serializer_test.rb
169
- - test/unit/active_model/serializer/has_one_polymorphic_test.rb
170
156
  - test/unit/active_model/serializer/attributes_test.rb
171
157
  - test/unit/active_model/serializer/options_test.rb
172
158
  - test/unit/active_model/serializer/key_format_test.rb
173
159
  - test/unit/active_model/serializer/associations_test.rb
174
160
  - test/unit/active_model/default_serializer_test.rb
175
161
  - test/fixtures/poro.rb
176
- - test/fixtures/template.html.erb
177
162
  - test/fixtures/active_record.rb
@@ -1,16 +0,0 @@
1
- module ActiveModel
2
- module Serializable
3
- module Utils
4
- extend self
5
-
6
- def _const_get(const)
7
- begin
8
- method = RUBY_VERSION >= '2.0' ? :const_get : :qualified_const_get
9
- Object.send method, const
10
- rescue NameError
11
- const.safe_constantize
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,39 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- class Association
4
- class HasMany < Association
5
- def initialize(name, *args)
6
- super
7
- @root_key = @embedded_key.to_s
8
- @key ||= case CONFIG.default_key_type
9
- when :name then name.to_s.pluralize
10
- else "#{name.to_s.singularize}_ids"
11
- end
12
- end
13
-
14
- def serializer_class(object, _)
15
- if use_array_serializer?
16
- ArraySerializer
17
- else
18
- serializer_from_options
19
- end
20
- end
21
-
22
- def options
23
- if use_array_serializer?
24
- { each_serializer: serializer_from_options }.merge! super
25
- else
26
- super
27
- end
28
- end
29
-
30
- private
31
-
32
- def use_array_serializer?
33
- !serializer_from_options ||
34
- serializer_from_options && !(serializer_from_options <= ArraySerializer)
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,25 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- class Association
4
- class HasOne < Association
5
- def initialize(name, *args)
6
- super
7
- @root_key = @embedded_key.to_s.pluralize
8
- @key ||= case CONFIG.default_key_type
9
- when :name then name.to_s.singularize
10
- else "#{name}_id"
11
- end
12
- end
13
-
14
- def serializer_class(object, options = {})
15
- serializer_from_options || serializer_from_object(object, options) || default_serializer
16
- end
17
-
18
- def build_serializer(object, options = {})
19
- options[:_wrap_in_array] = embed_in_root?
20
- super
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1 +0,0 @@
1
- <p>Hello.</p>
@@ -1,96 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActionController
4
- module Serialization
5
- class NamespacedSerializationTest < ActionController::TestCase
6
- class TestNamespace::MyController < ActionController::Base
7
- def render_profile_with_namespace
8
- render json: Profile.new({ name: 'Name 1', description: 'Description 1'})
9
- end
10
-
11
- def render_profiles_with_namespace
12
- render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})]
13
- end
14
-
15
- def render_comment
16
- render json: Comment.new(content: 'Comment 1')
17
- end
18
-
19
- def render_comments
20
- render json: [Comment.new(content: 'Comment 1')]
21
- end
22
- end
23
-
24
- tests TestNamespace::MyController
25
-
26
- def test_render_profile_with_namespace
27
- get :render_profile_with_namespace
28
- assert_serializer TestNamespace::ProfileSerializer
29
- end
30
-
31
- def test_render_profiles_with_namespace
32
- get :render_profiles_with_namespace
33
- assert_serializer TestNamespace::ProfileSerializer
34
- end
35
-
36
- def test_fallback_to_a_version_without_namespace
37
- get :render_comment
38
- assert_serializer CommentSerializer
39
- end
40
-
41
- def test_array_fallback_to_a_version_without_namespace
42
- get :render_comments
43
- assert_serializer CommentSerializer
44
- end
45
- end
46
-
47
- class OptionNamespacedSerializationTest < ActionController::TestCase
48
- class MyController < ActionController::Base
49
- def default_serializer_options
50
- {
51
- namespace: TestNamespace
52
- }
53
- end
54
-
55
- def render_profile_with_namespace_option
56
- render json: Profile.new({ name: 'Name 1', description: 'Description 1'})
57
- end
58
-
59
- def render_profiles_with_namespace_option
60
- render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})]
61
- end
62
-
63
- def render_comment
64
- render json: Comment.new(content: 'Comment 1')
65
- end
66
-
67
- def render_comments
68
- render json: [Comment.new(content: 'Comment 1')]
69
- end
70
- end
71
-
72
- tests MyController
73
-
74
- def test_render_profile_with_namespace_option
75
- get :render_profile_with_namespace_option
76
- assert_serializer TestNamespace::ProfileSerializer
77
- end
78
-
79
- def test_render_profiles_with_namespace_option
80
- get :render_profiles_with_namespace_option
81
- assert_serializer TestNamespace::ProfileSerializer
82
- end
83
-
84
- def test_fallback_to_a_version_without_namespace
85
- get :render_comment
86
- assert_serializer CommentSerializer
87
- end
88
-
89
- def test_array_fallback_to_a_version_without_namespace
90
- get :render_comments
91
- assert_serializer CommentSerializer
92
- end
93
- end
94
-
95
- end
96
- end
@@ -1,189 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class HasManyPolymorphicTest < ActiveModel::TestCase
6
- def setup
7
- @association = MailSerializer._associations[:attachments]
8
- @old_association = @association.dup
9
-
10
- @mail = Mail.new({ body: 'Body 1' })
11
- @mail_serializer = MailSerializer.new(@mail)
12
- end
13
-
14
- def teardown
15
- MailSerializer._associations[:attachments] = @old_association
16
- end
17
-
18
- def model_name(object)
19
- object.class.to_s.demodulize.underscore.to_sym
20
- end
21
-
22
- def test_associations_definition
23
- assert_equal 1, MailSerializer._associations.length
24
- assert_kind_of Association::HasMany, @association
25
- assert_equal true, @association.polymorphic
26
- assert_equal 'attachments', @association.name
27
- end
28
-
29
- def test_associations_embedding_ids_serialization_using_serializable_hash
30
- @association.embed = :ids
31
-
32
- assert_equal({
33
- body: 'Body 1',
34
- 'attachment_ids' => @mail.attachments.map do |c|
35
- { id: c.object_id, type: model_name(c) }
36
- end
37
- }, @mail_serializer.serializable_hash)
38
- end
39
-
40
- def test_associations_embedding_ids_serialization_using_as_json
41
- @association.embed = :ids
42
-
43
- assert_equal({
44
- 'mail' => {
45
- :body => 'Body 1',
46
- 'attachment_ids' => @mail.attachments.map do |c|
47
- { id: c.object_id, type: model_name(c) }
48
- end
49
- }
50
- }, @mail_serializer.as_json)
51
- end
52
-
53
- def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
54
- @association.embed = :ids
55
- @association.key = 'key'
56
-
57
- assert_equal({
58
- body: 'Body 1',
59
- 'key' => @mail.attachments.map do |c|
60
- { id: c.object_id, type: model_name(c) }
61
- end
62
- }, @mail_serializer.serializable_hash)
63
- end
64
-
65
- def test_associations_embedding_objects_serialization_using_serializable_hash
66
- @association.embed = :objects
67
-
68
- assert_equal({
69
- body: 'Body 1',
70
- :attachments => [
71
- { type: :image, image: { url: 'U1' }},
72
- { type: :video, video: { html: 'H1' }}
73
- ]
74
- }, @mail_serializer.serializable_hash)
75
- end
76
-
77
- def test_associations_embedding_objects_serialization_using_as_json
78
- @association.embed = :objects
79
-
80
- assert_equal({
81
- 'mail' => {
82
- body: 'Body 1',
83
- attachments: [
84
- { type: :image, image: { url: 'U1' }},
85
- { type: :video, video: { html: 'H1' }}
86
- ]
87
- }
88
- }, @mail_serializer.as_json)
89
- end
90
-
91
- def test_associations_embedding_nil_objects_serialization_using_as_json
92
- @association.embed = :objects
93
- @mail.instance_eval do
94
- def attachments
95
- [nil]
96
- end
97
- end
98
-
99
- assert_equal({
100
- 'mail' => {
101
- :body => 'Body 1',
102
- :attachments => [nil]
103
- }
104
- }, @mail_serializer.as_json)
105
- end
106
-
107
- def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
108
- @association.embed = :objects
109
- @association.embedded_key = 'root'
110
-
111
- assert_equal({
112
- body: 'Body 1',
113
- 'root' => [
114
- { type: :image, image: { url: 'U1' }},
115
- { type: :video, video: { html: 'H1' }}
116
- ]
117
- }, @mail_serializer.serializable_hash)
118
- end
119
-
120
- def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
121
- @association.embed = :ids
122
- @association.embed_in_root = true
123
-
124
- assert_equal({
125
- body: 'Body 1',
126
- 'attachment_ids' => @mail.attachments.map do |c|
127
- { id: c.object_id, type: model_name(c) }
128
- end
129
- }, @mail_serializer.serializable_hash)
130
- end
131
-
132
- def test_associations_embedding_ids_including_objects_serialization_using_as_json
133
- @association.embed = :ids
134
- @association.embed_in_root = true
135
-
136
- assert_equal({
137
- 'mail' => {
138
- body: 'Body 1',
139
- 'attachment_ids' => @mail.attachments.map do |c|
140
- { id: c.object_id, type: model_name(c) }
141
- end,
142
- },
143
- 'attachments' => [
144
- { type: :image, image: { url: 'U1' }},
145
- { type: :video, video: { html: 'H1' }}
146
- ]
147
- }, @mail_serializer.as_json)
148
- end
149
-
150
- def test_associations_embedding_nothing_including_objects_serialization_using_as_json
151
- @association.embed = nil
152
- @association.embed_in_root = true
153
-
154
- assert_equal({
155
- 'mail' => { body: 'Body 1' },
156
- 'attachments' => [
157
- { type: :image, image: { url: 'U1' }},
158
- { type: :video, video: { html: 'H1' }}
159
- ]
160
- }, @mail_serializer.as_json)
161
- end
162
-
163
- def test_associations_using_a_given_serializer
164
- @association.embed = :ids
165
- @association.embed_in_root = true
166
- @association.serializer_from_options = Class.new(ActiveModel::Serializer) do
167
- def fake
168
- 'fake'
169
- end
170
-
171
- attributes :fake
172
- end
173
-
174
- assert_equal({
175
- 'mail' => {
176
- body: 'Body 1',
177
- 'attachment_ids' => @mail.attachments.map do |c|
178
- { id: c.object_id, type: model_name(c) }
179
- end
180
- },
181
- 'attachments' => [
182
- { type: :image, image: { fake: 'fake' }},
183
- { type: :video, video: { fake: 'fake' }}
184
- ]
185
- }, @mail_serializer.as_json)
186
- end
187
- end
188
- end
189
- end