active_model_serializers 0.8.3 → 0.9.0.alpha1

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -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 +187 -96
  7. data/lib/action_controller/serialization.rb +30 -16
  8. data/lib/active_model/array_serializer.rb +36 -82
  9. data/lib/active_model/default_serializer.rb +22 -0
  10. data/lib/active_model/serializable.rb +25 -0
  11. data/lib/active_model/serializer/associations.rb +53 -211
  12. data/lib/active_model/serializer/config.rb +31 -0
  13. data/lib/active_model/serializer/generators/resource_override.rb +13 -0
  14. data/lib/{generators → active_model/serializer/generators}/serializer/USAGE +0 -0
  15. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
  16. data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +37 -0
  17. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
  18. data/lib/active_model/serializer/generators/serializer/templates/serializer.rb +8 -0
  19. data/lib/active_model/serializer/railtie.rb +10 -0
  20. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  21. data/lib/active_model/serializer.rb +126 -448
  22. data/lib/active_model/serializer_support.rb +5 -0
  23. data/lib/active_model_serializers.rb +7 -86
  24. data/test/coverage_setup.rb +15 -0
  25. data/test/fixtures/active_record.rb +92 -0
  26. data/test/fixtures/poro.rb +64 -0
  27. data/test/integration/action_controller/serialization_test.rb +234 -0
  28. data/test/integration/active_record/active_record_test.rb +77 -0
  29. data/test/integration/generators/resource_generator_test.rb +26 -0
  30. data/test/integration/generators/scaffold_controller_generator_test.rb +67 -0
  31. data/test/integration/generators/serializer_generator_test.rb +41 -0
  32. data/test/test_app.rb +11 -0
  33. data/test/test_helper.rb +8 -19
  34. data/test/tmp/app/serializers/account_serializer.rb +3 -0
  35. data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
  36. data/test/unit/active_model/array_serializer/root_test.rb +102 -0
  37. data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
  38. data/test/unit/active_model/array_serializer/serialization_test.rb +83 -0
  39. data/test/unit/active_model/default_serializer_test.rb +13 -0
  40. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +21 -0
  41. data/test/unit/active_model/serializer/associations_test.rb +19 -0
  42. data/test/unit/active_model/serializer/attributes_test.rb +41 -0
  43. data/test/unit/active_model/serializer/config_test.rb +86 -0
  44. data/test/unit/active_model/serializer/filter_test.rb +49 -0
  45. data/test/unit/active_model/serializer/has_many_test.rb +173 -0
  46. data/test/unit/active_model/serializer/has_one_test.rb +151 -0
  47. data/test/unit/active_model/serializer/meta_test.rb +39 -0
  48. data/test/unit/active_model/serializer/root_test.rb +117 -0
  49. data/test/unit/active_model/serializer/scope_test.rb +49 -0
  50. metadata +80 -65
  51. data/.gitignore +0 -18
  52. data/.travis.yml +0 -28
  53. data/Gemfile +0 -4
  54. data/Gemfile.edge +0 -9
  55. data/Rakefile +0 -18
  56. data/active_model_serializers.gemspec +0 -24
  57. data/bench/perf.rb +0 -43
  58. data/cruft.md +0 -19
  59. data/lib/active_record/serializer_override.rb +0 -16
  60. data/lib/generators/resource_override.rb +0 -13
  61. data/lib/generators/serializer/serializer_generator.rb +0 -42
  62. data/lib/generators/serializer/templates/serializer.rb +0 -19
  63. data/test/array_serializer_test.rb +0 -75
  64. data/test/association_test.rb +0 -592
  65. data/test/caching_test.rb +0 -96
  66. data/test/generators_test.rb +0 -85
  67. data/test/no_serialization_scope_test.rb +0 -34
  68. data/test/serialization_scope_name_test.rb +0 -67
  69. data/test/serialization_test.rb +0 -392
  70. data/test/serializer_support_test.rb +0 -51
  71. data/test/serializer_test.rb +0 -1465
  72. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,173 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class HasManyTest < ActiveModel::TestCase
6
+ def setup
7
+ @association = PostSerializer._associations[:comments]
8
+ @old_association = @association.dup
9
+
10
+ @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
11
+ @post_serializer = PostSerializer.new(@post)
12
+ end
13
+
14
+ def teardown
15
+ PostSerializer._associations[:comments] = @old_association
16
+ end
17
+
18
+ def test_associations_definition
19
+ assert_equal 1, PostSerializer._associations.length
20
+ assert_kind_of Association::HasMany, @association
21
+ assert_equal 'comments', @association.name
22
+ end
23
+
24
+ def test_associations_inheritance
25
+ inherited_serializer_klass = Class.new(PostSerializer) do
26
+ has_many :some_associations
27
+ end
28
+ another_inherited_serializer_klass = Class.new(PostSerializer)
29
+
30
+ assert_equal(PostSerializer._associations.length + 1,
31
+ inherited_serializer_klass._associations.length)
32
+ assert_equal(PostSerializer._associations.length,
33
+ another_inherited_serializer_klass._associations.length)
34
+ end
35
+
36
+ def test_associations_embedding_ids_serialization_using_serializable_hash
37
+ @association.embed = :ids
38
+
39
+ assert_equal({
40
+ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
41
+ }, @post_serializer.serializable_hash)
42
+ end
43
+
44
+ def test_associations_embedding_ids_serialization_using_as_json
45
+ @association.embed = :ids
46
+
47
+ assert_equal({
48
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }
49
+ }, @post_serializer.as_json)
50
+ end
51
+
52
+ def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
53
+ @association.embed = :ids
54
+ @association.key = 'key'
55
+
56
+ assert_equal({
57
+ title: 'Title 1', body: 'Body 1', 'key' => @post.comments.map { |c| c.object_id }
58
+ }, @post_serializer.serializable_hash)
59
+ end
60
+
61
+ def test_associations_embedding_objects_serialization_using_serializable_hash
62
+ @association.embed = :objects
63
+
64
+ assert_equal({
65
+ title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }]
66
+ }, @post_serializer.serializable_hash)
67
+ end
68
+
69
+ def test_associations_embedding_objects_serialization_using_as_json
70
+ @association.embed = :objects
71
+
72
+ assert_equal({
73
+ 'post' => { title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }] }
74
+ }, @post_serializer.as_json)
75
+ end
76
+
77
+ def test_associations_embedding_nil_objects_serialization_using_as_json
78
+ @association.embed = :objects
79
+ @post.instance_eval do
80
+ def comments
81
+ [nil]
82
+ end
83
+ end
84
+
85
+ assert_equal({
86
+ 'post' => { title: 'Title 1', body: 'Body 1', comments: [nil] }
87
+ }, @post_serializer.as_json)
88
+ end
89
+
90
+ def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
91
+ @association.embed = :objects
92
+ @association.embedded_key = 'root'
93
+
94
+ assert_equal({
95
+ title: 'Title 1', body: 'Body 1', 'root' => [{ content: 'C1' }, { content: 'C2' }]
96
+ }, @post_serializer.serializable_hash)
97
+ end
98
+
99
+ def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
100
+ @association.embed = :ids
101
+ @association.embed_in_root = true
102
+
103
+ assert_equal({
104
+ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
105
+ }, @post_serializer.serializable_hash)
106
+ end
107
+
108
+ def test_associations_embedding_ids_including_objects_serialization_using_as_json
109
+ @association.embed = :ids
110
+ @association.embed_in_root = true
111
+
112
+ assert_equal({
113
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
114
+ comments: [{ content: 'C1' }, { content: 'C2' }]
115
+ }, @post_serializer.as_json)
116
+ end
117
+
118
+ def test_associations_embedding_nothing_including_objects_serialization_using_as_json
119
+ @association.embed = nil
120
+ @association.embed_in_root = true
121
+
122
+ assert_equal({
123
+ 'post' => { title: 'Title 1', body: 'Body 1' },
124
+ comments: [{ content: 'C1' }, { content: 'C2' }]
125
+ }, @post_serializer.as_json)
126
+ end
127
+
128
+ def test_associations_using_a_given_serializer
129
+ @association.embed = :ids
130
+ @association.embed_in_root = true
131
+ @association.serializer_class = Class.new(ActiveModel::Serializer) do
132
+ def content
133
+ object.read_attribute_for_serialization(:content) + '!'
134
+ end
135
+
136
+ attributes :content
137
+ end
138
+
139
+ assert_equal({
140
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
141
+ comments: [{ content: 'C1!' }, { content: 'C2!' }]
142
+ }, @post_serializer.as_json)
143
+ end
144
+
145
+ def test_associations_embedding_ids_using_a_given_array_serializer
146
+ @association.embed = :ids
147
+ @association.embed_in_root = true
148
+ @association.serializer_class = Class.new(ActiveModel::ArraySerializer) do
149
+ def serializable_object
150
+ { my_content: ['fake'] }
151
+ end
152
+ end
153
+
154
+ assert_equal({
155
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
156
+ comments: { my_content: ['fake'] }
157
+ }, @post_serializer.as_json)
158
+ end
159
+
160
+ def test_associations_embedding_objects_using_a_given_array_serializer
161
+ @association.serializer_class = Class.new(ActiveModel::ArraySerializer) do
162
+ def serializable_object
163
+ { my_content: ['fake'] }
164
+ end
165
+ end
166
+
167
+ assert_equal({
168
+ 'post' => { title: 'Title 1', body: 'Body 1', comments: { my_content: ['fake'] } }
169
+ }, @post_serializer.as_json)
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,151 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class HasOneTest < ActiveModel::TestCase
6
+ def setup
7
+ @association = UserSerializer._associations[:profile]
8
+ @old_association = @association.dup
9
+
10
+ @user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
11
+ @user_serializer = UserSerializer.new(@user)
12
+ end
13
+
14
+ def teardown
15
+ UserSerializer._associations[:profile] = @old_association
16
+ end
17
+
18
+ def test_associations_definition
19
+ assert_equal 1, UserSerializer._associations.length
20
+ assert_kind_of Association::HasOne, @association
21
+ assert_equal 'profile', @association.name
22
+ end
23
+
24
+ def test_associations_embedding_ids_serialization_using_serializable_hash
25
+ @association.embed = :ids
26
+
27
+ assert_equal({
28
+ name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id
29
+ }, @user_serializer.serializable_hash)
30
+ end
31
+
32
+ def test_associations_embedding_ids_serialization_using_as_json
33
+ @association.embed = :ids
34
+
35
+ assert_equal({
36
+ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id }
37
+ }, @user_serializer.as_json)
38
+ end
39
+
40
+ def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
41
+ @association.embed = :ids
42
+ @association.key = 'key'
43
+
44
+ assert_equal({
45
+ name: 'Name 1', email: 'mail@server.com', 'key' => @user.profile.object_id
46
+ }, @user_serializer.serializable_hash)
47
+ end
48
+
49
+ def test_associations_embedding_objects_serialization_using_serializable_hash
50
+ @association.embed = :objects
51
+
52
+ assert_equal({
53
+ name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' }
54
+ }, @user_serializer.serializable_hash)
55
+ end
56
+
57
+ def test_associations_embedding_objects_serialization_using_as_json
58
+ @association.embed = :objects
59
+
60
+ assert_equal({
61
+ 'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' } }
62
+ }, @user_serializer.as_json)
63
+ end
64
+
65
+ def test_associations_embedding_nil_ids_serialization_using_as_json
66
+ @association.embed = :ids
67
+ @user.instance_eval do
68
+ def profile
69
+ nil
70
+ end
71
+ end
72
+
73
+ assert_equal({
74
+ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => nil }
75
+ }, @user_serializer.as_json)
76
+ end
77
+
78
+ def test_associations_embedding_nil_objects_serialization_using_as_json
79
+ @association.embed = :objects
80
+ @user.instance_eval do
81
+ def profile
82
+ nil
83
+ end
84
+ end
85
+
86
+ assert_equal({
87
+ 'user' => { name: 'Name 1', email: 'mail@server.com', profile: nil }
88
+ }, @user_serializer.as_json)
89
+ end
90
+
91
+ def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
92
+ @association.embed = :objects
93
+ @association.embedded_key = 'root'
94
+
95
+ assert_equal({
96
+ name: 'Name 1', email: 'mail@server.com', 'root' => { name: 'N1', description: 'D1' }
97
+ }, @user_serializer.serializable_hash)
98
+ end
99
+
100
+ def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
101
+ @association.embed = :ids
102
+ @association.embed_in_root = true
103
+
104
+ assert_equal({
105
+ name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id
106
+ }, @user_serializer.serializable_hash)
107
+ end
108
+
109
+ def test_associations_embedding_ids_including_objects_serialization_using_as_json
110
+ @association.embed = :ids
111
+ @association.embed_in_root = true
112
+
113
+ assert_equal({
114
+ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
115
+ 'profiles' => [{ name: 'N1', description: 'D1' }]
116
+ }, @user_serializer.as_json)
117
+ end
118
+
119
+ def test_associations_embedding_ids_using_a_given_serializer
120
+ @association.embed = :ids
121
+ @association.embed_in_root = true
122
+ @association.serializer_class = Class.new(ActiveModel::Serializer) do
123
+ def name
124
+ 'fake'
125
+ end
126
+
127
+ attributes :name
128
+ end
129
+
130
+ assert_equal({
131
+ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
132
+ 'profiles' => [{ name: 'fake' }]
133
+ }, @user_serializer.as_json)
134
+ end
135
+
136
+ def test_associations_embedding_objects_using_a_given_serializer
137
+ @association.serializer_class = Class.new(ActiveModel::Serializer) do
138
+ def name
139
+ 'fake'
140
+ end
141
+
142
+ attributes :name
143
+ end
144
+
145
+ assert_equal({
146
+ 'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'fake' } }
147
+ }, @user_serializer.as_json)
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class MetaTest < ActiveModel::TestCase
6
+ def setup
7
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8
+ end
9
+
10
+ def test_meta
11
+ profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta: { total: 10 })
12
+
13
+ assert_equal({
14
+ 'profile' => {
15
+ name: 'Name 1',
16
+ description: 'Description 1'
17
+ },
18
+ meta: {
19
+ total: 10
20
+ }
21
+ }, profile_serializer.as_json)
22
+ end
23
+
24
+ def test_meta_using_meta_key
25
+ profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta_key: :my_meta, my_meta: { total: 10 })
26
+
27
+ assert_equal({
28
+ 'profile' => {
29
+ name: 'Name 1',
30
+ description: 'Description 1'
31
+ },
32
+ my_meta: {
33
+ total: 10
34
+ }
35
+ }, profile_serializer.as_json)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,117 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class RootAsOptionTest < ActiveModel::TestCase
6
+ def setup
7
+ @old_root = ProfileSerializer._root
8
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
+ @serializer = ProfileSerializer.new(@profile, root: :initialize)
10
+ ProfileSerializer._root = true
11
+ end
12
+
13
+ def teardown
14
+ ProfileSerializer._root = @old_root
15
+ end
16
+
17
+ def test_root_is_not_displayed_using_serializable_hash
18
+ assert_equal({
19
+ name: 'Name 1', description: 'Description 1'
20
+ }, @serializer.serializable_hash)
21
+ end
22
+
23
+ def test_root_using_as_json
24
+ assert_equal({
25
+ initialize: {
26
+ name: 'Name 1', description: 'Description 1'
27
+ }
28
+ }, @serializer.as_json)
29
+ end
30
+
31
+ def test_root_from_serializer_name
32
+ @serializer = ProfileSerializer.new(@profile)
33
+
34
+ assert_equal({
35
+ 'profile' => {
36
+ name: 'Name 1', description: 'Description 1'
37
+ }
38
+ }, @serializer.as_json)
39
+ end
40
+
41
+ def test_root_as_argument_takes_precedence
42
+ assert_equal({
43
+ argument: {
44
+ name: 'Name 1', description: 'Description 1'
45
+ }
46
+ }, @serializer.as_json(root: :argument))
47
+ end
48
+
49
+ def test_using_false_root_in_initializer_takes_precedence
50
+ ProfileSerializer._root = 'root'
51
+ @serializer = ProfileSerializer.new(@profile, root: false)
52
+
53
+ assert_equal({
54
+ name: 'Name 1', description: 'Description 1'
55
+ }, @serializer.as_json)
56
+ end
57
+
58
+ def test_root_inheritance
59
+ ProfileSerializer._root = 'profile'
60
+
61
+ inherited_serializer_klass = Class.new(ProfileSerializer)
62
+ inherited_serializer_klass._root = 'inherited_profile'
63
+
64
+ another_inherited_serializer_klass = Class.new(ProfileSerializer)
65
+
66
+ assert_equal('inherited_profile',
67
+ inherited_serializer_klass._root)
68
+ assert_equal('profile',
69
+ another_inherited_serializer_klass._root)
70
+ end
71
+ end
72
+
73
+ class RootInSerializerTest < ActiveModel::TestCase
74
+ def setup
75
+ @old_root = ProfileSerializer._root
76
+ ProfileSerializer._root = :in_serializer
77
+ profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
78
+ @serializer = ProfileSerializer.new(profile)
79
+ @rooted_serializer = ProfileSerializer.new(profile, root: :initialize)
80
+ end
81
+
82
+ def teardown
83
+ ProfileSerializer._root = @old_root
84
+ end
85
+
86
+ def test_root_is_not_displayed_using_serializable_hash
87
+ assert_equal({
88
+ name: 'Name 1', description: 'Description 1'
89
+ }, @serializer.serializable_hash)
90
+ end
91
+
92
+ def test_root_using_as_json
93
+ assert_equal({
94
+ in_serializer: {
95
+ name: 'Name 1', description: 'Description 1'
96
+ }
97
+ }, @serializer.as_json)
98
+ end
99
+
100
+ def test_root_in_initializer_takes_precedence
101
+ assert_equal({
102
+ initialize: {
103
+ name: 'Name 1', description: 'Description 1'
104
+ }
105
+ }, @rooted_serializer.as_json)
106
+ end
107
+
108
+ def test_root_as_argument_takes_precedence
109
+ assert_equal({
110
+ argument: {
111
+ name: 'Name 1', description: 'Description 1'
112
+ }
113
+ }, @rooted_serializer.as_json(root: :argument))
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class ScopeTest < ActiveModel::TestCase
6
+ def setup
7
+ @serializer = ProfileSerializer.new(nil, scope: current_user)
8
+ end
9
+
10
+ def test_scope
11
+ assert_equal('user', @serializer.scope)
12
+ end
13
+
14
+ private
15
+
16
+ def current_user
17
+ 'user'
18
+ end
19
+ end
20
+
21
+ class NestedScopeTest < ActiveModel::TestCase
22
+ def setup
23
+ @association = UserSerializer._associations[:profile]
24
+ @old_association = @association.dup
25
+ @user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
26
+ @user_serializer = UserSerializer.new(@user, scope: 'user')
27
+ end
28
+
29
+ def teardown
30
+ UserSerializer._associations[:profile] = @old_association
31
+ end
32
+
33
+ def test_scope_passed_through
34
+ @association.serializer_class = Class.new(ActiveModel::Serializer) do
35
+ def name
36
+ scope
37
+ end
38
+
39
+ attributes :name
40
+ end
41
+
42
+ assert_equal({
43
+ name: 'Name 1', email: 'mail@server.com', profile: { name: 'user' }
44
+ }, @user_serializer.serializable_hash)
45
+ end
46
+ end
47
+
48
+ end
49
+ end