active_model_serializers 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/README.md +109 -17
  4. data/lib/action_controller/serialization.rb +27 -8
  5. data/lib/action_controller/serialization_test_case.rb +4 -4
  6. data/lib/active_model/array_serializer.rb +12 -5
  7. data/lib/active_model/serializable.rb +24 -2
  8. data/lib/active_model/serializable/utils.rb +16 -0
  9. data/lib/active_model/serializer.rb +70 -36
  10. data/lib/active_model/serializer/{associations.rb → association.rb} +8 -52
  11. data/lib/active_model/serializer/association/has_many.rb +39 -0
  12. data/lib/active_model/serializer/association/has_one.rb +25 -0
  13. data/lib/active_model/serializer/railtie.rb +8 -0
  14. data/lib/active_model/serializer/version.rb +1 -1
  15. data/lib/active_model_serializers.rb +1 -1
  16. data/test/fixtures/poro.rb +106 -1
  17. data/test/fixtures/template.html.erb +1 -0
  18. data/test/integration/action_controller/namespaced_serialization_test.rb +96 -0
  19. data/test/integration/action_controller/serialization_test_case_test.rb +10 -0
  20. data/test/integration/active_record/active_record_test.rb +2 -2
  21. data/test/serializers/tmp/app/serializers/account_serializer.rb +3 -0
  22. data/test/test_app.rb +3 -0
  23. data/test/unit/active_model/array_serializer/serialization_test.rb +1 -1
  24. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +15 -0
  25. data/test/unit/active_model/serializer/attributes_test.rb +16 -0
  26. data/test/unit/active_model/serializer/config_test.rb +3 -0
  27. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +189 -0
  28. data/test/unit/active_model/serializer/has_many_test.rb +51 -16
  29. data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +27 -0
  30. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +196 -0
  31. data/test/unit/active_model/serializer/has_one_test.rb +32 -0
  32. data/test/unit/active_model/serializer/options_test.rb +19 -0
  33. data/test/unit/active_model/serializer/url_helpers_test.rb +35 -0
  34. metadata +38 -23
  35. data/test/tmp/app/serializers/account_serializer.rb +0 -3
@@ -0,0 +1,196 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class HasOnePolymorphicTest < ActiveModel::TestCase
6
+ def setup
7
+ @association = InterviewSerializer._associations[:attachment]
8
+ @old_association = @association.dup
9
+
10
+ @interview = Interview.new({ text: 'Text 1' })
11
+ @interview_serializer = InterviewSerializer.new(@interview)
12
+ end
13
+
14
+ def teardown
15
+ InterviewSerializer._associations[:attachment] = @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, InterviewSerializer._associations.length
24
+ assert_kind_of Association::HasOne, @association
25
+ assert_equal true, @association.polymorphic
26
+ assert_equal 'attachment', @association.name
27
+ end
28
+
29
+ def test_associations_embedding_ids_serialization_using_serializable_hash
30
+ @association.embed = :ids
31
+
32
+ assert_equal({
33
+ text: 'Text 1',
34
+ 'attachment_id' => {
35
+ type: model_name(@interview.attachment),
36
+ id: @interview.attachment.object_id
37
+ }
38
+ }, @interview_serializer.serializable_hash)
39
+ end
40
+
41
+ def test_associations_embedding_ids_serialization_using_as_json
42
+ @association.embed = :ids
43
+
44
+ assert_equal({
45
+ 'interview' => {
46
+ text: 'Text 1',
47
+ 'attachment_id' => {
48
+ type: model_name(@interview.attachment),
49
+ id: @interview.attachment.object_id
50
+ }
51
+ }
52
+ }, @interview_serializer.as_json)
53
+ end
54
+
55
+ def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
56
+ @association.embed = :ids
57
+ @association.key = 'key'
58
+
59
+ assert_equal({
60
+ text: 'Text 1',
61
+ 'key' => {
62
+ type: model_name(@interview.attachment),
63
+ id: @interview.attachment.object_id
64
+ }
65
+ }, @interview_serializer.serializable_hash)
66
+ end
67
+
68
+ def test_associations_embedding_objects_serialization_using_serializable_hash
69
+ @association.embed = :objects
70
+
71
+ assert_equal({
72
+ text: 'Text 1',
73
+ attachment: {
74
+ type: model_name(@interview.attachment),
75
+ model_name(@interview.attachment) => { url: 'U1'}
76
+ }
77
+ }, @interview_serializer.serializable_hash)
78
+ end
79
+
80
+ def test_associations_embedding_objects_serialization_using_as_json
81
+ @association.embed = :objects
82
+
83
+ assert_equal({
84
+ 'interview' => {
85
+ text: 'Text 1',
86
+ attachment: {
87
+ type: model_name(@interview.attachment),
88
+ model_name(@interview.attachment) => { url: 'U1'}
89
+ }
90
+ }
91
+ }, @interview_serializer.as_json)
92
+ end
93
+
94
+ def test_associations_embedding_nil_ids_serialization_using_as_json
95
+ @association.embed = :ids
96
+ @interview.instance_eval do
97
+ def attachment
98
+ nil
99
+ end
100
+ end
101
+
102
+ assert_equal({
103
+ 'interview' => { text: 'Text 1', 'attachment_id' => nil }
104
+ }, @interview_serializer.as_json)
105
+ end
106
+
107
+ def test_associations_embedding_nil_objects_serialization_using_as_json
108
+ @association.embed = :objects
109
+ @interview.instance_eval do
110
+ def attachment
111
+ nil
112
+ end
113
+ end
114
+
115
+ assert_equal({
116
+ 'interview' => { text: 'Text 1', attachment: nil }
117
+ }, @interview_serializer.as_json)
118
+ end
119
+
120
+ def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
121
+ @association.embed = :objects
122
+ @association.embedded_key = 'root'
123
+
124
+ assert_equal({
125
+ text: 'Text 1',
126
+ 'root' => {
127
+ type: model_name(@interview.attachment),
128
+ model_name(@interview.attachment) => { url: 'U1'}
129
+ }
130
+ }, @interview_serializer.serializable_hash)
131
+ end
132
+
133
+ def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
134
+ @association.embed = :ids
135
+ @association.embed_in_root = true
136
+
137
+ assert_equal({
138
+ text: 'Text 1',
139
+ 'attachment_id' => {
140
+ type: model_name(@interview.attachment),
141
+ id: @interview.attachment.object_id
142
+ }
143
+ }, @interview_serializer.serializable_hash)
144
+ end
145
+
146
+ def test_associations_embedding_ids_including_objects_serialization_using_as_json
147
+ @association.embed = :ids
148
+ @association.embed_in_root = true
149
+
150
+ assert_equal({
151
+ 'interview' => {
152
+ text: 'Text 1',
153
+ 'attachment_id' => {
154
+ type: model_name(@interview.attachment),
155
+ id: @interview.attachment.object_id
156
+ }
157
+ },
158
+ "attachments" => [{
159
+ type: model_name(@interview.attachment),
160
+ model_name(@interview.attachment) => {
161
+ url: 'U1'
162
+ }
163
+ }]
164
+ }, @interview_serializer.as_json)
165
+ end
166
+
167
+ def test_associations_using_a_given_serializer
168
+ @association.embed = :ids
169
+ @association.embed_in_root = true
170
+ @association.serializer_from_options = Class.new(ActiveModel::Serializer) do
171
+ def name
172
+ 'fake'
173
+ end
174
+
175
+ attributes :name
176
+ end
177
+
178
+ assert_equal({
179
+ 'interview' => {
180
+ text: 'Text 1',
181
+ 'attachment_id' => {
182
+ type: model_name(@interview.attachment),
183
+ id: @interview.attachment.object_id
184
+ }
185
+ },
186
+ "attachments" => [{
187
+ type: model_name(@interview.attachment),
188
+ model_name(@interview.attachment) => {
189
+ name: 'fake'
190
+ }
191
+ }]
192
+ }, @interview_serializer.as_json)
193
+ end
194
+ end
195
+ end
196
+ end
@@ -131,6 +131,20 @@ 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
+
134
148
  def test_associations_embedding_ids_using_a_given_serializer
135
149
  @association.embed = :ids
136
150
  @association.embed_in_root = true
@@ -202,6 +216,24 @@ module ActiveModel
202
216
  }
203
217
  }, @user_serializer.as_json)
204
218
  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
205
237
  end
206
238
  end
207
239
  end
@@ -11,5 +11,24 @@ 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
14
33
  end
15
34
  end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class UrlHelpersTest < Minitest::Test
6
+ include Rails.application.routes.url_helpers
7
+
8
+ def setup
9
+ Object.const_set 'UserController', Class.new(ActionController::Base) do
10
+ def show
11
+ render text: 'profile'
12
+ end
13
+ end
14
+
15
+ Rails.application.routes.draw do
16
+ get '/profile/:id', as: :profile, controller: :user, action: :show
17
+ end
18
+ end
19
+
20
+ def test_url_helpers_are_available
21
+ serializer = Class.new(ActiveModel::Serializer) do
22
+ attributes :url
23
+
24
+ def url
25
+ profile_url(id: object.object_id)
26
+ end
27
+ end
28
+ profile = Profile.new
29
+
30
+ assert_equal({ url: profile_url(id: profile.object_id) },
31
+ serializer.new(profile).as_json)
32
+ end
33
+ end
34
+ end
35
+ 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.2
4
+ version: 0.9.3
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-12 00:00:00.000000000 Z
13
+ date: 2015-01-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
@@ -59,8 +59,11 @@ 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
62
63
  - lib/active_model/serializer.rb
63
- - lib/active_model/serializer/associations.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
64
67
  - lib/active_model/serializer/config.rb
65
68
  - lib/active_model/serializer/generators/resource_override.rb
66
69
  - lib/active_model/serializer/generators/serializer/USAGE
@@ -74,15 +77,17 @@ files:
74
77
  - lib/active_model_serializers.rb
75
78
  - test/fixtures/active_record.rb
76
79
  - test/fixtures/poro.rb
80
+ - test/fixtures/template.html.erb
81
+ - test/integration/action_controller/namespaced_serialization_test.rb
77
82
  - test/integration/action_controller/serialization_test.rb
78
83
  - test/integration/action_controller/serialization_test_case_test.rb
79
84
  - test/integration/active_record/active_record_test.rb
80
85
  - test/integration/generators/resource_generator_test.rb
81
86
  - test/integration/generators/scaffold_controller_generator_test.rb
82
87
  - test/integration/generators/serializer_generator_test.rb
88
+ - test/serializers/tmp/app/serializers/account_serializer.rb
83
89
  - test/test_app.rb
84
90
  - test/test_helper.rb
85
- - test/tmp/app/serializers/account_serializer.rb
86
91
  - test/unit/active_model/array_serializer/except_test.rb
87
92
  - test/unit/active_model/array_serializer/key_format_test.rb
88
93
  - test/unit/active_model/array_serializer/meta_test.rb
@@ -96,13 +101,17 @@ files:
96
101
  - test/unit/active_model/serializer/attributes_test.rb
97
102
  - test/unit/active_model/serializer/config_test.rb
98
103
  - test/unit/active_model/serializer/filter_test.rb
104
+ - test/unit/active_model/serializer/has_many_polymorphic_test.rb
99
105
  - 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
100
108
  - test/unit/active_model/serializer/has_one_test.rb
101
109
  - test/unit/active_model/serializer/key_format_test.rb
102
110
  - test/unit/active_model/serializer/meta_test.rb
103
111
  - test/unit/active_model/serializer/options_test.rb
104
112
  - test/unit/active_model/serializer/root_test.rb
105
113
  - test/unit/active_model/serializer/scope_test.rb
114
+ - test/unit/active_model/serializer/url_helpers_test.rb
106
115
  homepage: https://github.com/rails-api/active_model_serializers
107
116
  licenses:
108
117
  - MIT
@@ -129,34 +138,40 @@ specification_version: 4
129
138
  summary: Bringing consistency and object orientation to model serialization. Works
130
139
  great for client-side MVC frameworks!
131
140
  test_files:
132
- - test/test_app.rb
133
- - test/tmp/app/serializers/account_serializer.rb
134
- - test/integration/active_record/active_record_test.rb
141
+ - test/fixtures/active_record.rb
142
+ - test/fixtures/poro.rb
143
+ - test/fixtures/template.html.erb
144
+ - test/integration/action_controller/namespaced_serialization_test.rb
135
145
  - test/integration/action_controller/serialization_test.rb
136
146
  - test/integration/action_controller/serialization_test_case_test.rb
147
+ - test/integration/active_record/active_record_test.rb
137
148
  - test/integration/generators/resource_generator_test.rb
138
- - test/integration/generators/serializer_generator_test.rb
139
149
  - test/integration/generators/scaffold_controller_generator_test.rb
150
+ - test/integration/generators/serializer_generator_test.rb
151
+ - test/serializers/tmp/app/serializers/account_serializer.rb
152
+ - test/test_app.rb
140
153
  - test/test_helper.rb
141
154
  - test/unit/active_model/array_serializer/except_test.rb
142
- - test/unit/active_model/array_serializer/scope_test.rb
143
- - test/unit/active_model/array_serializer/only_test.rb
144
- - test/unit/active_model/array_serializer/serialization_test.rb
155
+ - test/unit/active_model/array_serializer/key_format_test.rb
145
156
  - test/unit/active_model/array_serializer/meta_test.rb
157
+ - test/unit/active_model/array_serializer/only_test.rb
146
158
  - test/unit/active_model/array_serializer/root_test.rb
147
- - test/unit/active_model/array_serializer/key_format_test.rb
159
+ - test/unit/active_model/array_serializer/scope_test.rb
160
+ - test/unit/active_model/array_serializer/serialization_test.rb
161
+ - test/unit/active_model/default_serializer_test.rb
162
+ - test/unit/active_model/serializer/associations/build_serializer_test.rb
163
+ - test/unit/active_model/serializer/associations_test.rb
164
+ - test/unit/active_model/serializer/attributes_test.rb
148
165
  - test/unit/active_model/serializer/config_test.rb
149
- - test/unit/active_model/serializer/has_one_test.rb
150
- - test/unit/active_model/serializer/has_many_test.rb
151
166
  - test/unit/active_model/serializer/filter_test.rb
152
- - test/unit/active_model/serializer/scope_test.rb
167
+ - test/unit/active_model/serializer/has_many_polymorphic_test.rb
168
+ - test/unit/active_model/serializer/has_many_test.rb
169
+ - test/unit/active_model/serializer/has_one_and_has_many_test.rb
170
+ - test/unit/active_model/serializer/has_one_polymorphic_test.rb
171
+ - test/unit/active_model/serializer/has_one_test.rb
172
+ - test/unit/active_model/serializer/key_format_test.rb
153
173
  - test/unit/active_model/serializer/meta_test.rb
154
- - test/unit/active_model/serializer/root_test.rb
155
- - test/unit/active_model/serializer/associations/build_serializer_test.rb
156
- - test/unit/active_model/serializer/attributes_test.rb
157
174
  - test/unit/active_model/serializer/options_test.rb
158
- - test/unit/active_model/serializer/key_format_test.rb
159
- - test/unit/active_model/serializer/associations_test.rb
160
- - test/unit/active_model/default_serializer_test.rb
161
- - test/fixtures/poro.rb
162
- - test/fixtures/active_record.rb
175
+ - test/unit/active_model/serializer/root_test.rb
176
+ - test/unit/active_model/serializer/scope_test.rb
177
+ - test/unit/active_model/serializer/url_helpers_test.rb
@@ -1,3 +0,0 @@
1
- class AccountSerializer < MySerializer
2
- attributes :id
3
- end