active_model_serializers 0.10.0.rc1 → 0.10.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/Gemfile +1 -1
- data/README.md +6 -3
- data/lib/action_controller/serialization.rb +9 -1
- data/lib/active_model/serializer.rb +15 -21
- data/lib/active_model/serializer/adapter.rb +13 -4
- data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +5 -5
- data/lib/active_model/serializer/adapter/json.rb +8 -10
- data/lib/active_model/serializer/adapter/json_api.rb +34 -30
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +3 -2
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/configuration.rb +1 -1
- data/lib/active_model/serializer/fieldset.rb +2 -2
- data/lib/active_model/serializer/railtie.rb +8 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +1 -0
- data/lib/generators/serializer/resource_override.rb +12 -0
- data/test/action_controller/adapter_selector_test.rb +7 -5
- data/test/action_controller/explicit_serializer_test.rb +33 -9
- data/test/action_controller/json_api_linked_test.rb +25 -19
- data/test/action_controller/rescue_from_test.rb +32 -0
- data/test/action_controller/serialization_scope_name_test.rb +2 -2
- data/test/action_controller/serialization_test.rb +41 -23
- data/test/adapter/fragment_cache_test.rb +1 -1
- data/test/adapter/json/belongs_to_test.rb +9 -2
- data/test/adapter/json/collection_test.rb +16 -2
- data/test/adapter/json/has_many_test.rb +1 -1
- data/test/adapter/json_api/belongs_to_test.rb +45 -35
- data/test/adapter/json_api/collection_test.rb +30 -23
- data/test/adapter/json_api/has_many_embed_ids_test.rb +2 -2
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +14 -14
- data/test/adapter/json_api/has_many_test.rb +22 -18
- data/test/adapter/json_api/has_one_test.rb +8 -6
- data/test/adapter/json_api/linked_test.rb +97 -71
- data/test/adapter/json_test.rb +1 -1
- data/test/adapter_test.rb +1 -1
- data/test/array_serializer_test.rb +14 -0
- data/test/fixtures/poro.rb +31 -7
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/{serializers/generators_test.rb → generators/serializer_generator_test.rb} +2 -10
- data/test/serializers/adapter_for_test.rb +1 -1
- data/test/serializers/associations_test.rb +21 -0
- data/test/serializers/attribute_test.rb +16 -1
- data/test/serializers/attributes_test.rb +35 -0
- data/test/serializers/cache_test.rb +14 -4
- data/test/serializers/configuration_test.rb +1 -1
- data/test/serializers/meta_test.rb +38 -9
- data/test/serializers/serializer_for_test.rb +9 -0
- data/test/test_helper.rb +10 -7
- metadata +12 -5
@@ -1,16 +1,8 @@
|
|
1
|
-
|
2
|
-
if Rails.version.to_s.start_with? '4'
|
3
|
-
config.eager_load = false
|
4
|
-
config.secret_key_base = 'abc123'
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
Rails.application.load_generators
|
9
|
-
|
1
|
+
require 'test_helper'
|
10
2
|
require 'generators/serializer/serializer_generator'
|
11
3
|
|
12
4
|
class SerializerGeneratorTest < Rails::Generators::TestCase
|
13
|
-
destination File.expand_path("
|
5
|
+
destination File.expand_path("../../../tmp/generators", __FILE__)
|
14
6
|
setup :prepare_destination
|
15
7
|
|
16
8
|
tests Rails::Generators::SerializerGenerator
|
@@ -11,7 +11,7 @@ module ActiveModel
|
|
11
11
|
|
12
12
|
def test_returns_default_adapter
|
13
13
|
adapter = ActiveModel::Serializer.adapter
|
14
|
-
assert_equal ActiveModel::Serializer::Adapter::
|
14
|
+
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_overwrite_adapter_with_symbol
|
@@ -101,6 +101,27 @@ module ActiveModel
|
|
101
101
|
|
102
102
|
assert blog_is_present
|
103
103
|
end
|
104
|
+
|
105
|
+
def test_associations_inheritance
|
106
|
+
inherited_klass = Class.new(PostSerializer)
|
107
|
+
|
108
|
+
assert_equal(PostSerializer._associations, inherited_klass._associations)
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_associations_inheritance_with_new_association
|
112
|
+
inherited_klass = Class.new(PostSerializer) do
|
113
|
+
has_many :top_comments, serializer: CommentSerializer
|
114
|
+
end
|
115
|
+
expected_associations = PostSerializer._associations.merge(
|
116
|
+
top_comments: {
|
117
|
+
type: :has_many,
|
118
|
+
association_options: {
|
119
|
+
serializer: CommentSerializer
|
120
|
+
}
|
121
|
+
}
|
122
|
+
)
|
123
|
+
assert_equal(inherited_klass._associations, expected_associations)
|
124
|
+
end
|
104
125
|
end
|
105
126
|
end
|
106
127
|
end
|
@@ -15,9 +15,24 @@ module ActiveModel
|
|
15
15
|
|
16
16
|
def test_json_serializable_hash
|
17
17
|
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
18
|
+
assert_equal({alternate_blog: { id:1, title:"AMS Hints"}}, adapter.serializable_hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_attribute_inheritance_with_key
|
22
|
+
inherited_klass = Class.new(AlternateBlogSerializer)
|
23
|
+
blog_serializer = inherited_klass.new(@blog)
|
24
|
+
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(blog_serializer)
|
18
25
|
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
|
19
26
|
end
|
27
|
+
|
28
|
+
def test_multiple_calls_with_the_same_attribute
|
29
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
30
|
+
attribute :title
|
31
|
+
attribute :title
|
32
|
+
end
|
33
|
+
|
34
|
+
assert_equal([:title], serializer_class._attributes)
|
35
|
+
end
|
20
36
|
end
|
21
37
|
end
|
22
38
|
end
|
23
|
-
|
@@ -6,6 +6,11 @@ module ActiveModel
|
|
6
6
|
def setup
|
7
7
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
8
8
|
@profile_serializer = ProfileSerializer.new(@profile)
|
9
|
+
@comment = Comment.new(id: 1, body: "ZOMG!!", date: "2015")
|
10
|
+
@serializer_klass = Class.new(CommentSerializer)
|
11
|
+
@serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
|
12
|
+
attributes :date, :likes
|
13
|
+
end
|
9
14
|
end
|
10
15
|
|
11
16
|
def test_attributes_definition
|
@@ -23,6 +28,36 @@ module ActiveModel
|
|
23
28
|
@profile_serializer.attributes(fields: [:name, :description], required_fields: [:name]))
|
24
29
|
|
25
30
|
end
|
31
|
+
|
32
|
+
def test_attributes_inheritance_definition
|
33
|
+
assert_equal([:id, :body], @serializer_klass._attributes)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_attributes_inheritance
|
37
|
+
serializer = @serializer_klass.new(@comment)
|
38
|
+
assert_equal({id: 1, body: "ZOMG!!"},
|
39
|
+
serializer.attributes)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_attribute_inheritance_with_new_attribute_definition
|
43
|
+
assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes)
|
44
|
+
assert_equal([:id, :body], CommentSerializer._attributes)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_attribute_inheritance_with_new_attribute
|
48
|
+
serializer = @serializer_klass_with_new_attributes.new(@comment)
|
49
|
+
assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil},
|
50
|
+
serializer.attributes)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_multiple_calls_with_the_same_attribute
|
54
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
55
|
+
attributes :id, :title
|
56
|
+
attributes :id, :title, :title, :body
|
57
|
+
end
|
58
|
+
|
59
|
+
assert_equal([:id, :title, :body], serializer_class._attributes)
|
60
|
+
end
|
26
61
|
end
|
27
62
|
end
|
28
63
|
end
|
@@ -5,10 +5,10 @@ module ActiveModel
|
|
5
5
|
def setup
|
6
6
|
ActionController::Base.cache_store.clear
|
7
7
|
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
8
|
-
@blog = Blog.new(id: 999, name: "Custom blog")
|
9
8
|
@post = Post.new(title: 'New Post', body: 'Body')
|
10
9
|
@bio = Bio.new(id: 1, content: 'AMS Contributor')
|
11
10
|
@author = Author.new(name: 'Joao M. D. Moura')
|
11
|
+
@blog = Blog.new(id: 999, name: "Custom blog", writer: @author, articles: [])
|
12
12
|
@role = Role.new(name: 'Great Author')
|
13
13
|
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
|
14
14
|
@place = Place.new(name: 'Amazing Place')
|
@@ -30,6 +30,7 @@ module ActiveModel
|
|
30
30
|
@post_serializer = PostSerializer.new(@post)
|
31
31
|
@author_serializer = AuthorSerializer.new(@author)
|
32
32
|
@comment_serializer = CommentSerializer.new(@comment)
|
33
|
+
@blog_serializer = BlogSerializer.new(@blog)
|
33
34
|
end
|
34
35
|
|
35
36
|
def test_cache_definition
|
@@ -56,9 +57,9 @@ module ActiveModel
|
|
56
57
|
end
|
57
58
|
|
58
59
|
def test_cache_options_definition
|
59
|
-
assert_equal({expires_in: 0.1}, @post_serializer.class._cache_options)
|
60
|
-
assert_equal(nil, @
|
61
|
-
assert_equal({expires_in: 1.day}, @comment_serializer.class._cache_options)
|
60
|
+
assert_equal({expires_in: 0.1, skip_digest: true}, @post_serializer.class._cache_options)
|
61
|
+
assert_equal(nil, @blog_serializer.class._cache_options)
|
62
|
+
assert_equal({expires_in: 1.day, skip_digest: true}, @comment_serializer.class._cache_options)
|
62
63
|
end
|
63
64
|
|
64
65
|
def test_fragment_cache_definition
|
@@ -115,6 +116,15 @@ module ActiveModel
|
|
115
116
|
assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
|
116
117
|
end
|
117
118
|
|
119
|
+
def test_uses_file_digest_in_cahe_key
|
120
|
+
blog = render_object_with_cache(@blog)
|
121
|
+
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
|
122
|
+
end
|
123
|
+
|
124
|
+
def _cache_digest_definition
|
125
|
+
assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
|
126
|
+
end
|
127
|
+
|
118
128
|
private
|
119
129
|
def render_object_with_cache(obj)
|
120
130
|
serializer_class = ActiveModel::Serializer.serializer_for(obj)
|
@@ -12,9 +12,10 @@ module ActiveModel
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_meta_is_present_with_root
|
15
|
-
|
15
|
+
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10})
|
16
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
|
16
17
|
expected = {
|
17
|
-
|
18
|
+
alternate_blog: {
|
18
19
|
id: 1,
|
19
20
|
title: "AMS Hints"
|
20
21
|
},
|
@@ -35,9 +36,10 @@ module ActiveModel
|
|
35
36
|
end
|
36
37
|
|
37
38
|
def test_meta_key_is_used
|
38
|
-
|
39
|
+
serializer = AlternateBlogSerializer.new(@blog, root: 'blog', meta: {total: 10}, meta_key: "haha_meta")
|
40
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
|
39
41
|
expected = {
|
40
|
-
|
42
|
+
alternate_blog: {
|
41
43
|
id: 1,
|
42
44
|
title: "AMS Hints"
|
43
45
|
},
|
@@ -48,9 +50,9 @@ module ActiveModel
|
|
48
50
|
assert_equal expected, adapter.as_json
|
49
51
|
end
|
50
52
|
|
51
|
-
def
|
52
|
-
serializer = ArraySerializer.new([@blog],
|
53
|
-
adapter = ActiveModel::Serializer::Adapter::
|
53
|
+
def test_meta_is_not_present_on_arrays_without_root
|
54
|
+
serializer = ArraySerializer.new([@blog], meta: {total: 10})
|
55
|
+
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
|
54
56
|
expected = [{
|
55
57
|
id: 1,
|
56
58
|
name: "AMS Hints",
|
@@ -67,11 +69,38 @@ module ActiveModel
|
|
67
69
|
assert_equal expected, adapter.as_json
|
68
70
|
end
|
69
71
|
|
72
|
+
def test_meta_is_present_on_arrays_with_root
|
73
|
+
serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
|
74
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
|
75
|
+
expected = {
|
76
|
+
blogs: [{
|
77
|
+
id: 1,
|
78
|
+
name: "AMS Hints",
|
79
|
+
writer: {
|
80
|
+
id: 2,
|
81
|
+
name: "Steve"
|
82
|
+
},
|
83
|
+
articles: [{
|
84
|
+
id: 3,
|
85
|
+
title: "AMS",
|
86
|
+
body: nil
|
87
|
+
}]
|
88
|
+
}],
|
89
|
+
'haha_meta' => {
|
90
|
+
total: 10
|
91
|
+
}
|
92
|
+
}
|
93
|
+
assert_equal expected, adapter.as_json
|
94
|
+
end
|
95
|
+
|
70
96
|
private
|
71
97
|
|
72
98
|
def load_adapter(options)
|
73
|
-
|
74
|
-
|
99
|
+
adapter_opts, serializer_opts =
|
100
|
+
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
101
|
+
|
102
|
+
serializer = AlternateBlogSerializer.new(@blog, serializer_opts)
|
103
|
+
ActiveModel::Serializer::Adapter::FlattenJson.new(serializer, adapter_opts)
|
75
104
|
end
|
76
105
|
end
|
77
106
|
end
|
@@ -29,10 +29,14 @@ module ActiveModel
|
|
29
29
|
class SerializerTest < Minitest::Test
|
30
30
|
class MyProfile < Profile
|
31
31
|
end
|
32
|
+
class CustomProfile
|
33
|
+
def serializer_class; ProfileSerializer; end
|
34
|
+
end
|
32
35
|
|
33
36
|
def setup
|
34
37
|
@profile = Profile.new
|
35
38
|
@my_profile = MyProfile.new
|
39
|
+
@custom_profile = CustomProfile.new
|
36
40
|
@model = ::Model.new
|
37
41
|
end
|
38
42
|
|
@@ -50,6 +54,11 @@ module ActiveModel
|
|
50
54
|
serializer = ActiveModel::Serializer.serializer_for(@my_profile)
|
51
55
|
assert_equal ProfileSerializer, serializer
|
52
56
|
end
|
57
|
+
|
58
|
+
def test_serializer_custom_serializer
|
59
|
+
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
|
60
|
+
assert_equal ProfileSerializer, serializer
|
61
|
+
end
|
53
62
|
end
|
54
63
|
end
|
55
64
|
end
|
data/test/test_helper.rb
CHANGED
@@ -6,17 +6,24 @@ require 'action_controller/test_case'
|
|
6
6
|
require 'action_controller/railtie'
|
7
7
|
require 'active_support/json'
|
8
8
|
require 'minitest/autorun'
|
9
|
+
require 'fileutils'
|
9
10
|
# Ensure backward compatibility with Minitest 4
|
10
11
|
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
11
12
|
|
13
|
+
require 'active_model_serializers'
|
14
|
+
|
12
15
|
class Foo < Rails::Application
|
13
|
-
if Rails
|
16
|
+
if Rails::VERSION::MAJOR >= 4
|
17
|
+
config.eager_load = false
|
18
|
+
config.secret_key_base = 'abc123'
|
14
19
|
config.action_controller.perform_caching = true
|
20
|
+
config.active_support.test_order = :random
|
21
|
+
config.logger = Logger.new(nil)
|
15
22
|
ActionController::Base.cache_store = :memory_store
|
16
23
|
end
|
17
24
|
end
|
18
|
-
|
19
|
-
|
25
|
+
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
26
|
+
Foo.initialize!
|
20
27
|
|
21
28
|
require 'fixtures/poro'
|
22
29
|
|
@@ -35,7 +42,3 @@ ActionController::TestCase.class_eval do
|
|
35
42
|
@routes = TestHelper::Routes
|
36
43
|
end
|
37
44
|
end
|
38
|
-
|
39
|
-
def def_serializer(&block)
|
40
|
-
Class.new(ActiveModel::Serializer, &block)
|
41
|
-
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.10.0.
|
4
|
+
version: 0.10.0.rc2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Klabnik
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- lib/action_controller/serialization.rb
|
87
87
|
- lib/active_model/serializer.rb
|
88
88
|
- lib/active_model/serializer/adapter.rb
|
89
|
+
- lib/active_model/serializer/adapter/flatten_json.rb
|
89
90
|
- lib/active_model/serializer/adapter/fragment_cache.rb
|
90
91
|
- lib/active_model/serializer/adapter/json.rb
|
91
92
|
- lib/active_model/serializer/adapter/json/fragment_cache.rb
|
@@ -95,14 +96,17 @@ files:
|
|
95
96
|
- lib/active_model/serializer/array_serializer.rb
|
96
97
|
- lib/active_model/serializer/configuration.rb
|
97
98
|
- lib/active_model/serializer/fieldset.rb
|
99
|
+
- lib/active_model/serializer/railtie.rb
|
98
100
|
- lib/active_model/serializer/version.rb
|
99
101
|
- lib/active_model_serializers.rb
|
100
102
|
- lib/generators/serializer/USAGE
|
103
|
+
- lib/generators/serializer/resource_override.rb
|
101
104
|
- lib/generators/serializer/serializer_generator.rb
|
102
105
|
- lib/generators/serializer/templates/serializer.rb
|
103
106
|
- test/action_controller/adapter_selector_test.rb
|
104
107
|
- test/action_controller/explicit_serializer_test.rb
|
105
108
|
- test/action_controller/json_api_linked_test.rb
|
109
|
+
- test/action_controller/rescue_from_test.rb
|
106
110
|
- test/action_controller/serialization_scope_name_test.rb
|
107
111
|
- test/action_controller/serialization_test.rb
|
108
112
|
- test/adapter/fragment_cache_test.rb
|
@@ -121,6 +125,8 @@ files:
|
|
121
125
|
- test/adapter_test.rb
|
122
126
|
- test/array_serializer_test.rb
|
123
127
|
- test/fixtures/poro.rb
|
128
|
+
- test/generators/scaffold_controller_generator_test.rb
|
129
|
+
- test/generators/serializer_generator_test.rb
|
124
130
|
- test/serializers/adapter_for_test.rb
|
125
131
|
- test/serializers/associations_test.rb
|
126
132
|
- test/serializers/attribute_test.rb
|
@@ -128,7 +134,6 @@ files:
|
|
128
134
|
- test/serializers/cache_test.rb
|
129
135
|
- test/serializers/configuration_test.rb
|
130
136
|
- test/serializers/fieldset_test.rb
|
131
|
-
- test/serializers/generators_test.rb
|
132
137
|
- test/serializers/meta_test.rb
|
133
138
|
- test/serializers/options_test.rb
|
134
139
|
- test/serializers/serializer_for_test.rb
|
@@ -154,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
159
|
version: 1.3.1
|
155
160
|
requirements: []
|
156
161
|
rubyforge_project:
|
157
|
-
rubygems_version: 2.
|
162
|
+
rubygems_version: 2.4.5
|
158
163
|
signing_key:
|
159
164
|
specification_version: 4
|
160
165
|
summary: Conventions-based JSON generation for Rails.
|
@@ -162,6 +167,7 @@ test_files:
|
|
162
167
|
- test/action_controller/adapter_selector_test.rb
|
163
168
|
- test/action_controller/explicit_serializer_test.rb
|
164
169
|
- test/action_controller/json_api_linked_test.rb
|
170
|
+
- test/action_controller/rescue_from_test.rb
|
165
171
|
- test/action_controller/serialization_scope_name_test.rb
|
166
172
|
- test/action_controller/serialization_test.rb
|
167
173
|
- test/adapter/fragment_cache_test.rb
|
@@ -180,6 +186,8 @@ test_files:
|
|
180
186
|
- test/adapter_test.rb
|
181
187
|
- test/array_serializer_test.rb
|
182
188
|
- test/fixtures/poro.rb
|
189
|
+
- test/generators/scaffold_controller_generator_test.rb
|
190
|
+
- test/generators/serializer_generator_test.rb
|
183
191
|
- test/serializers/adapter_for_test.rb
|
184
192
|
- test/serializers/associations_test.rb
|
185
193
|
- test/serializers/attribute_test.rb
|
@@ -187,7 +195,6 @@ test_files:
|
|
187
195
|
- test/serializers/cache_test.rb
|
188
196
|
- test/serializers/configuration_test.rb
|
189
197
|
- test/serializers/fieldset_test.rb
|
190
|
-
- test/serializers/generators_test.rb
|
191
198
|
- test/serializers/meta_test.rb
|
192
199
|
- test/serializers/options_test.rb
|
193
200
|
- test/serializers/serializer_for_test.rb
|