active_model_serializers 0.8.4 → 0.9.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -45
  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 -113
  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.rb +126 -447
  12. data/lib/active_model/serializer/associations.rb +53 -211
  13. data/lib/active_model/serializer/config.rb +31 -0
  14. data/lib/active_model/serializer/generators/resource_override.rb +13 -0
  15. data/lib/{generators → active_model/serializer/generators}/serializer/USAGE +0 -0
  16. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
  17. data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +37 -0
  18. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
  19. data/lib/active_model/serializer/generators/serializer/templates/serializer.rb +8 -0
  20. data/lib/active_model/serializer/railtie.rb +10 -0
  21. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  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 +7 -41
  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 +78 -74
  51. data/.gitignore +0 -18
  52. data/.travis.yml +0 -34
  53. data/Gemfile +0 -38
  54. data/Rakefile +0 -22
  55. data/active_model_serializers.gemspec +0 -24
  56. data/appveyor.yml +0 -27
  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 -177
  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 -396
  70. data/test/serializer_support_test.rb +0 -51
  71. data/test/serializer_test.rb +0 -1466
  72. data/test/test_fakes.rb +0 -218
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class ArraySerializer
5
+ class ScopeTest < ActiveModel::TestCase
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,83 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class ArraySerializer
5
+ class BasicObjectsSerializationTest < ActiveModel::TestCase
6
+ def setup
7
+ array = [1, 2, 3]
8
+ @serializer = ActiveModel::Serializer.serializer_for(array).new(array)
9
+ end
10
+
11
+ def test_serializer_for_array_returns_appropriate_type
12
+ assert_kind_of 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 ModelSerializationTest < ActiveModel::TestCase
22
+ def test_array_serializer_serializes_models
23
+ array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
24
+ Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
25
+ serializer = ArraySerializer.new(array)
26
+
27
+ expected = [{ name: 'Name 1', description: 'Description 1' },
28
+ { name: 'Name 2', description: 'Description 2' }]
29
+
30
+ assert_equal expected, serializer.serializable_array
31
+ assert_equal expected, serializer.as_json
32
+ end
33
+
34
+ def test_array_serializers_each_serializer
35
+ array = [::Model.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
36
+ ::Model.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
37
+ serializer = ArraySerializer.new(array, each_serializer: ProfileSerializer)
38
+
39
+ expected = [{ name: 'Name 1', description: 'Description 1' },
40
+ { name: 'Name 2', description: 'Description 2' }]
41
+
42
+ assert_equal expected, serializer.serializable_array
43
+ assert_equal expected, serializer.as_json
44
+ end
45
+
46
+ def test_associated_objects_of_multiple_instances_embedded_in_root
47
+ @association = PostSerializer._associations[:comments]
48
+ @old_association = @association.dup
49
+
50
+ @association.embed = :ids
51
+ @association.embed_in_root = true
52
+
53
+ @post1 = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
54
+ @post2 = Post.new({ title: 'Title 2', body: 'Body 2', date: '1/1/2000' })
55
+
56
+ class << @post2
57
+ attr_writer :comments
58
+ end
59
+
60
+ @post2.comments = [
61
+ Comment.new(content: 'C3'),
62
+ Comment.new(content: 'C4')
63
+ ]
64
+
65
+ @serializer = ArraySerializer.new([@post1, @post2], root: :posts)
66
+ assert_equal({
67
+ posts: [
68
+ {title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
69
+ {title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
70
+ ],
71
+ comments: [
72
+ {content: "C1"},
73
+ {content: "C2"},
74
+ {content: "C3"},
75
+ {content: "C4"}
76
+ ]
77
+ }, @serializer.as_json)
78
+ ensure
79
+ PostSerializer._associations[:comments] = @old_association
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class DefaultSerializer
5
+ class Test < ActiveModel::TestCase
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,21 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Association
6
+ class BuildSerializerTest < ActiveModel::TestCase
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
+ end
11
+
12
+ def test_build_serializer_for_array_called_twice
13
+ 2.times do
14
+ serializer = @association.build_serializer(@post)
15
+ assert_instance_of(PostSerializer, serializer)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AssociationsTest < ActiveModel::TestCase
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,41 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AttributesTest < ActiveModel::TestCase
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
+ end
40
+ end
41
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Config
6
+ class Test < ActiveModel::TestCase
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 < ActiveModel::TestCase
34
+ def test_setup
35
+ ActiveModel::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
+ ActiveModel::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 < ActiveModel::TestCase
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 < ActiveModel::TestCase
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 < ActiveModel::TestCase
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,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