active_model_serializers_rails_2.3 0.9.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/CHANGELOG.md +87 -0
  2. data/CONTRIBUTING.md +20 -0
  3. data/DESIGN.textile +586 -0
  4. data/MIT-LICENSE +21 -0
  5. data/README.md +793 -0
  6. data/lib/active_model/array_serializer.rb +60 -0
  7. data/lib/active_model/default_serializer.rb +27 -0
  8. data/lib/active_model/serializable.rb +25 -0
  9. data/lib/active_model/serializer.rb +220 -0
  10. data/lib/active_model/serializer/associations.rb +98 -0
  11. data/lib/active_model/serializer/config.rb +31 -0
  12. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
  13. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
  14. data/lib/active_model/serializer/railtie.rb +10 -0
  15. data/lib/active_model/serializer/version.rb +5 -0
  16. data/lib/active_model/serializer_support.rb +5 -0
  17. data/lib/active_model_serializers.rb +33 -0
  18. data/test/coverage_setup.rb +15 -0
  19. data/test/fixtures/active_record.rb +92 -0
  20. data/test/fixtures/poro.rb +64 -0
  21. data/test/integration/active_record/active_record_test.rb +77 -0
  22. data/test/test_app.rb +11 -0
  23. data/test/test_helper.rb +13 -0
  24. data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
  25. data/test/unit/active_model/array_serializer/root_test.rb +102 -0
  26. data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
  27. data/test/unit/active_model/array_serializer/serialization_test.rb +182 -0
  28. data/test/unit/active_model/default_serializer_test.rb +13 -0
  29. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +21 -0
  30. data/test/unit/active_model/serializer/associations_test.rb +19 -0
  31. data/test/unit/active_model/serializer/attributes_test.rb +41 -0
  32. data/test/unit/active_model/serializer/config_test.rb +86 -0
  33. data/test/unit/active_model/serializer/filter_test.rb +49 -0
  34. data/test/unit/active_model/serializer/has_many_test.rb +174 -0
  35. data/test/unit/active_model/serializer/has_one_test.rb +151 -0
  36. data/test/unit/active_model/serializer/meta_test.rb +39 -0
  37. data/test/unit/active_model/serializer/root_test.rb +117 -0
  38. data/test/unit/active_model/serializer/scope_test.rb +49 -0
  39. metadata +127 -0
@@ -0,0 +1,86 @@
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
+
71
+ association = PostSerializer._associations[:comments]
72
+ old_association = association.dup
73
+
74
+ association.send :initialize, association.name, association.options
75
+
76
+ assert association.embed_ids?
77
+ assert !association.embed_objects?
78
+ assert association.embed_in_root
79
+ ensure
80
+ PostSerializer._associations[:comments] = old_association
81
+ CONFIG.clear
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class FilterAttributesTest < 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
+ @profile_serializer.instance_eval do
10
+ def filter(keys)
11
+ keys - [:description]
12
+ end
13
+ end
14
+ end
15
+
16
+ def test_filtered_attributes_serialization
17
+ assert_equal({
18
+ 'profile' => { name: 'Name 1' }
19
+ }, @profile_serializer.as_json)
20
+ end
21
+ end
22
+
23
+ class FilterAssociationsTest < Minitest::Test
24
+ def setup
25
+ @association = PostSerializer._associations[:comments]
26
+ @old_association = @association.dup
27
+ @association.embed = :ids
28
+ @association.embed_in_root = true
29
+ @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
30
+ @post_serializer = PostSerializer.new(@post)
31
+ @post_serializer.instance_eval do
32
+ def filter(keys)
33
+ keys - [:body, :comments]
34
+ end
35
+ end
36
+ end
37
+
38
+ def teardown
39
+ PostSerializer._associations[:comments] = @old_association
40
+ end
41
+
42
+ def test_filtered_associations_serialization
43
+ assert_equal({
44
+ 'post' => { title: 'Title 1' }
45
+ }, @post_serializer.as_json)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,174 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class HasManyTest < Minitest::Test
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 ({ :foo => nil }).as_json, { :foo => nil }
86
+ assert_equal({
87
+ 'post' => { title: 'Title 1', body: 'Body 1', comments: [nil] }
88
+ }, @post_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
+ title: 'Title 1', body: 'Body 1', 'root' => [{ content: 'C1' }, { content: 'C2' }]
97
+ }, @post_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
+ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
106
+ }, @post_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
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
115
+ comments: [{ content: 'C1' }, { content: 'C2' }]
116
+ }, @post_serializer.as_json)
117
+ end
118
+
119
+ def test_associations_embedding_nothing_including_objects_serialization_using_as_json
120
+ @association.embed = nil
121
+ @association.embed_in_root = true
122
+
123
+ assert_equal({
124
+ 'post' => { title: 'Title 1', body: 'Body 1' },
125
+ comments: [{ content: 'C1' }, { content: 'C2' }]
126
+ }, @post_serializer.as_json)
127
+ end
128
+
129
+ def test_associations_using_a_given_serializer
130
+ @association.embed = :ids
131
+ @association.embed_in_root = true
132
+ @association.serializer_from_options = Class.new(Serializer) do
133
+ def content
134
+ object.read_attribute_for_serialization(:content) + '!'
135
+ end
136
+
137
+ attributes :content
138
+ end
139
+
140
+ assert_equal({
141
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
142
+ comments: [{ content: 'C1!' }, { content: 'C2!' }]
143
+ }, @post_serializer.as_json)
144
+ end
145
+
146
+ def test_associations_embedding_ids_using_a_given_array_serializer
147
+ @association.embed = :ids
148
+ @association.embed_in_root = true
149
+ @association.serializer_from_options = Class.new(ArraySerializer) do
150
+ def serializable_object
151
+ { my_content: ['fake'] }
152
+ end
153
+ end
154
+
155
+ assert_equal({
156
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
157
+ comments: { my_content: ['fake'] }
158
+ }, @post_serializer.as_json)
159
+ end
160
+
161
+ def test_associations_embedding_objects_using_a_given_array_serializer
162
+ @association.serializer_from_options = Class.new(ArraySerializer) do
163
+ def serializable_object
164
+ { my_content: ['fake'] }
165
+ end
166
+ end
167
+
168
+ assert_equal({
169
+ 'post' => { title: 'Title 1', body: 'Body 1', comments: { my_content: ['fake'] } }
170
+ }, @post_serializer.as_json)
171
+ end
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,151 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class HasOneTest < Minitest::Test
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_from_options = Class.new(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_from_options = Class.new(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