active_model_serializers 0.10.0.rc2 → 0.10.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.rubocop.yml +82 -0
- data/.rubocop_todo.yml +315 -0
- data/.simplecov +99 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +9 -3
- data/Gemfile +39 -8
- data/README.md +55 -31
- data/Rakefile +29 -2
- data/active_model_serializers.gemspec +37 -13
- data/appveyor.yml +25 -0
- data/docs/README.md +29 -0
- data/docs/general/adapters.md +110 -0
- data/docs/general/configuration_options.md +11 -0
- data/docs/general/getting_started.md +73 -0
- data/docs/howto/add_pagination_links.md +112 -0
- data/docs/howto/add_root_key.md +51 -0
- data/docs/howto/outside_controller_use.md +42 -0
- data/lib/action_controller/serialization.rb +24 -33
- data/lib/active_model/serializable_resource.rb +70 -0
- data/lib/active_model/serializer.rb +50 -131
- data/lib/active_model/serializer/adapter.rb +84 -21
- data/lib/active_model/serializer/adapter/flatten_json.rb +9 -9
- data/lib/active_model/serializer/adapter/fragment_cache.rb +10 -13
- data/lib/active_model/serializer/adapter/json.rb +25 -28
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +2 -12
- data/lib/active_model/serializer/adapter/json_api.rb +100 -98
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +4 -14
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +50 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -8
- data/lib/active_model/serializer/array_serializer.rb +22 -17
- data/lib/active_model/serializer/association.rb +20 -0
- data/lib/active_model/serializer/associations.rb +97 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/configuration.rb +1 -0
- data/lib/active_model/serializer/fieldset.rb +7 -7
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +10 -0
- data/lib/active_model/serializer/lint.rb +129 -0
- data/lib/active_model/serializer/railtie.rb +7 -0
- data/lib/active_model/serializer/reflection.rb +74 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/utils.rb +35 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +28 -14
- data/lib/generators/serializer/serializer_generator.rb +7 -7
- data/lib/generators/serializer/templates/{serializer.rb → serializer.rb.erb} +2 -2
- data/lib/tasks/rubocop.rake +0 -0
- data/test/action_controller/adapter_selector_test.rb +3 -3
- data/test/action_controller/explicit_serializer_test.rb +9 -9
- data/test/action_controller/json_api/linked_test.rb +179 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/action_controller/serialization_scope_name_test.rb +10 -6
- data/test/action_controller/serialization_test.rb +149 -112
- data/test/active_record_test.rb +9 -0
- data/test/adapter/fragment_cache_test.rb +11 -1
- data/test/adapter/json/belongs_to_test.rb +4 -5
- data/test/adapter/json/collection_test.rb +30 -21
- data/test/adapter/json/has_many_test.rb +20 -9
- data/test/adapter/json_api/belongs_to_test.rb +38 -38
- data/test/adapter/json_api/collection_test.rb +22 -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 +4 -4
- data/test/adapter/json_api/has_many_test.rb +54 -19
- data/test/adapter/json_api/has_one_test.rb +28 -8
- data/test/adapter/json_api/json_api_test.rb +37 -0
- data/test/adapter/json_api/linked_test.rb +75 -75
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/resource_type_config_test.rb +59 -0
- data/test/adapter/json_test.rb +18 -5
- data/test/adapter_test.rb +10 -11
- data/test/array_serializer_test.rb +63 -5
- data/test/capture_warnings.rb +65 -0
- data/test/fixtures/active_record.rb +56 -0
- data/test/fixtures/poro.rb +60 -29
- data/test/generators/scaffold_controller_generator_test.rb +1 -2
- data/test/generators/serializer_generator_test.rb +17 -12
- data/test/lint_test.rb +37 -0
- data/test/logger_test.rb +18 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +27 -0
- data/test/serializers/adapter_for_test.rb +123 -3
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +70 -47
- data/test/serializers/attribute_test.rb +28 -4
- data/test/serializers/attributes_test.rb +8 -14
- data/test/serializers/cache_test.rb +58 -31
- data/test/serializers/fieldset_test.rb +3 -4
- data/test/serializers/meta_test.rb +42 -28
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +1 -1
- data/test/support/rails_app.rb +21 -0
- data/test/support/serialization_testing.rb +13 -0
- data/test/support/simplecov.rb +6 -0
- data/test/support/stream_capture.rb +50 -0
- data/test/support/test_case.rb +5 -0
- data/test/test_helper.rb +41 -29
- data/test/utils/include_args_to_hash_test.rb +79 -0
- metadata +123 -17
- data/test/action_controller/json_api_linked_test.rb +0 -179
- data/test/action_controller/rescue_from_test.rb +0 -32
- data/test/serializers/urls_test.rb +0 -26
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class AssociationMacrosTest < Minitest::Test
|
6
|
+
AuthorSummarySerializer = Class.new
|
7
|
+
class AssociationsTestSerializer < Serializer
|
8
|
+
belongs_to :author, serializer: AuthorSummarySerializer
|
9
|
+
has_many :comments
|
10
|
+
has_one :category
|
11
|
+
end
|
12
|
+
|
13
|
+
def before_setup
|
14
|
+
@reflections = AssociationsTestSerializer._reflections
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_has_one_defines_reflection
|
18
|
+
has_one_reflection = HasOneReflection.new(:category, {})
|
19
|
+
|
20
|
+
assert_includes(@reflections, has_one_reflection)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_has_many_defines_reflection
|
24
|
+
has_many_reflection = HasManyReflection.new(:comments, {})
|
25
|
+
|
26
|
+
assert_includes(@reflections, has_many_reflection)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_belongs_to_defines_reflection
|
30
|
+
belongs_to_reflection = BelongsToReflection.new(:author, serializer: AuthorSummarySerializer)
|
31
|
+
|
32
|
+
assert_includes(@reflections, belongs_to_reflection)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -4,7 +4,7 @@ module ActiveModel
|
|
4
4
|
class Serializer
|
5
5
|
class AssociationsTest < Minitest::Test
|
6
6
|
class Model
|
7
|
-
def initialize(hash={})
|
7
|
+
def initialize(hash = {})
|
8
8
|
@attributes = hash
|
9
9
|
end
|
10
10
|
|
@@ -29,98 +29,121 @@ module ActiveModel
|
|
29
29
|
@author.roles = []
|
30
30
|
@blog = Blog.new({ name: 'AMS Blog' })
|
31
31
|
@post = Post.new({ title: 'New Post', body: 'Body' })
|
32
|
+
@tag = Tag.new({ name: '#hashtagged' })
|
32
33
|
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
33
34
|
@post.comments = [@comment]
|
35
|
+
@post.tags = [@tag]
|
34
36
|
@post.blog = @blog
|
35
37
|
@comment.post = @post
|
36
38
|
@comment.author = nil
|
37
39
|
@post.author = @author
|
38
40
|
@author.posts = [@post]
|
39
41
|
|
40
|
-
@post_serializer = PostSerializer.new(@post, {custom_options: true})
|
42
|
+
@post_serializer = PostSerializer.new(@post, { custom_options: true })
|
41
43
|
@author_serializer = AuthorSerializer.new(@author)
|
42
44
|
@comment_serializer = CommentSerializer.new(@comment)
|
43
45
|
end
|
44
46
|
|
45
47
|
def test_has_many_and_has_one
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
assert_equal({embed: :ids}, options)
|
48
|
+
@author_serializer.associations.each do |association|
|
49
|
+
key = association.key
|
50
|
+
serializer = association.serializer
|
51
|
+
options = association.options
|
52
|
+
|
53
|
+
case key
|
54
|
+
when :posts
|
55
|
+
assert_equal({}, options)
|
55
56
|
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
56
|
-
|
57
|
+
when :bio
|
57
58
|
assert_equal({}, options)
|
58
59
|
assert_nil serializer
|
59
|
-
|
60
|
-
assert_equal({
|
60
|
+
when :roles
|
61
|
+
assert_equal({}, options)
|
61
62
|
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
62
63
|
else
|
63
|
-
flunk "Unknown association: #{
|
64
|
+
flunk "Unknown association: #{key}"
|
64
65
|
end
|
65
66
|
end
|
66
67
|
end
|
67
68
|
|
68
|
-
def
|
69
|
-
@
|
70
|
-
|
71
|
-
|
72
|
-
|
69
|
+
def test_has_many_with_no_serializer
|
70
|
+
PostWithTagsSerializer.new(@post).associations.each do |association|
|
71
|
+
key = association.key
|
72
|
+
serializer = association.serializer
|
73
|
+
options = association.options
|
74
|
+
|
75
|
+
assert_equal key, :tags
|
76
|
+
assert_equal serializer, nil
|
77
|
+
assert_equal [{ attributes: { name: '#hashtagged' } }].to_json, options[:virtual_value].to_json
|
73
78
|
end
|
74
79
|
end
|
75
80
|
|
81
|
+
def test_serializer_options_are_passed_into_associations_serializers
|
82
|
+
association = @post_serializer
|
83
|
+
.associations
|
84
|
+
.detect { |assoc| assoc.key == :comments }
|
85
|
+
|
86
|
+
assert association.serializer.first.custom_options[:custom_options]
|
87
|
+
end
|
88
|
+
|
76
89
|
def test_belongs_to
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
if name == :post
|
84
|
-
assert_equal({}, options)
|
90
|
+
@comment_serializer.associations.each do |association|
|
91
|
+
key = association.key
|
92
|
+
serializer = association.serializer
|
93
|
+
|
94
|
+
case key
|
95
|
+
when :post
|
85
96
|
assert_kind_of(PostSerializer, serializer)
|
86
|
-
|
87
|
-
assert_equal({}, options)
|
97
|
+
when :author
|
88
98
|
assert_nil serializer
|
89
99
|
else
|
90
|
-
flunk "Unknown association: #{
|
100
|
+
flunk "Unknown association: #{key}"
|
91
101
|
end
|
102
|
+
|
103
|
+
assert_equal({}, association.options)
|
92
104
|
end
|
93
105
|
end
|
94
106
|
|
95
107
|
def test_belongs_to_with_custom_method
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
assert blog_is_present
|
108
|
+
assert(
|
109
|
+
@post_serializer.associations.any? do |association|
|
110
|
+
association.key == :blog
|
111
|
+
end
|
112
|
+
)
|
103
113
|
end
|
104
114
|
|
105
115
|
def test_associations_inheritance
|
106
116
|
inherited_klass = Class.new(PostSerializer)
|
107
117
|
|
108
|
-
assert_equal(PostSerializer.
|
118
|
+
assert_equal(PostSerializer._reflections, inherited_klass._reflections)
|
109
119
|
end
|
110
120
|
|
111
121
|
def test_associations_inheritance_with_new_association
|
112
122
|
inherited_klass = Class.new(PostSerializer) do
|
113
123
|
has_many :top_comments, serializer: CommentSerializer
|
114
124
|
end
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
}
|
121
|
-
}
|
125
|
+
|
126
|
+
assert(
|
127
|
+
PostSerializer._reflections.all? do |reflection|
|
128
|
+
inherited_klass._reflections.include?(reflection)
|
129
|
+
end
|
122
130
|
)
|
123
|
-
|
131
|
+
|
132
|
+
assert(
|
133
|
+
inherited_klass._reflections.any? do |reflection|
|
134
|
+
reflection.name == :top_comments
|
135
|
+
end
|
136
|
+
)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_associations_custom_keys
|
140
|
+
serializer = PostWithCustomKeysSerializer.new(@post)
|
141
|
+
|
142
|
+
expected_association_keys = serializer.associations.map(&:key)
|
143
|
+
|
144
|
+
assert expected_association_keys.include? :reviews
|
145
|
+
assert expected_association_keys.include? :writer
|
146
|
+
assert expected_association_keys.include? :site
|
124
147
|
end
|
125
148
|
end
|
126
149
|
end
|
@@ -4,25 +4,25 @@ module ActiveModel
|
|
4
4
|
class Serializer
|
5
5
|
class AttributeTest < Minitest::Test
|
6
6
|
def setup
|
7
|
-
@blog = Blog.new({ id: 1, name: 'AMS Hints' })
|
7
|
+
@blog = Blog.new({ id: 1, name: 'AMS Hints', type: 'stuff' })
|
8
8
|
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_attributes_definition
|
12
12
|
assert_equal([:id, :title],
|
13
|
-
|
13
|
+
@blog_serializer.class._attributes)
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_json_serializable_hash
|
17
17
|
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
18
|
-
assert_equal({
|
18
|
+
assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash)
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_attribute_inheritance_with_key
|
22
22
|
inherited_klass = Class.new(AlternateBlogSerializer)
|
23
23
|
blog_serializer = inherited_klass.new(@blog)
|
24
24
|
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(blog_serializer)
|
25
|
-
assert_equal({:id=>1, :title=>
|
25
|
+
assert_equal({ :id => 1, :title => 'AMS Hints' }, adapter.serializable_hash)
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_multiple_calls_with_the_same_attribute
|
@@ -33,6 +33,30 @@ module ActiveModel
|
|
33
33
|
|
34
34
|
assert_equal([:title], serializer_class._attributes)
|
35
35
|
end
|
36
|
+
|
37
|
+
def test_id_attribute_override
|
38
|
+
serializer = Class.new(ActiveModel::Serializer) do
|
39
|
+
attribute :name, key: :id
|
40
|
+
end
|
41
|
+
|
42
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
|
43
|
+
assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_type_attribute
|
47
|
+
attribute_serializer = Class.new(ActiveModel::Serializer) do
|
48
|
+
attribute :id, key: :type
|
49
|
+
end
|
50
|
+
attributes_serializer = Class.new(ActiveModel::Serializer) do
|
51
|
+
attributes :type
|
52
|
+
end
|
53
|
+
|
54
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(attribute_serializer.new(@blog))
|
55
|
+
assert_equal({ blog: { type: 1 } }, adapter.serializable_hash)
|
56
|
+
|
57
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(attributes_serializer.new(@blog))
|
58
|
+
assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
|
59
|
+
end
|
36
60
|
end
|
37
61
|
end
|
38
62
|
end
|
@@ -6,7 +6,7 @@ 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:
|
9
|
+
@comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
|
10
10
|
@serializer_klass = Class.new(CommentSerializer)
|
11
11
|
@serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
|
12
12
|
attributes :date, :likes
|
@@ -15,18 +15,12 @@ module ActiveModel
|
|
15
15
|
|
16
16
|
def test_attributes_definition
|
17
17
|
assert_equal([:name, :description],
|
18
|
-
|
18
|
+
@profile_serializer.class._attributes)
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_attributes_with_fields_option
|
22
|
-
assert_equal({name: 'Name 1'},
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_required_fields
|
27
|
-
assert_equal({name: 'Name 1', description: 'Description 1'},
|
28
|
-
@profile_serializer.attributes(fields: [:name, :description], required_fields: [:name]))
|
29
|
-
|
22
|
+
assert_equal({ name: 'Name 1' },
|
23
|
+
@profile_serializer.attributes(fields: [:name]))
|
30
24
|
end
|
31
25
|
|
32
26
|
def test_attributes_inheritance_definition
|
@@ -35,8 +29,8 @@ module ActiveModel
|
|
35
29
|
|
36
30
|
def test_attributes_inheritance
|
37
31
|
serializer = @serializer_klass.new(@comment)
|
38
|
-
assert_equal({id: 1, body:
|
39
|
-
|
32
|
+
assert_equal({ id: 1, body: 'ZOMG!!' },
|
33
|
+
serializer.attributes)
|
40
34
|
end
|
41
35
|
|
42
36
|
def test_attribute_inheritance_with_new_attribute_definition
|
@@ -46,8 +40,8 @@ module ActiveModel
|
|
46
40
|
|
47
41
|
def test_attribute_inheritance_with_new_attribute
|
48
42
|
serializer = @serializer_klass_with_new_attributes.new(@comment)
|
49
|
-
assert_equal({id: 1, body:
|
50
|
-
|
43
|
+
assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil },
|
44
|
+
serializer.attributes)
|
51
45
|
end
|
52
46
|
|
53
47
|
def test_multiple_calls_with_the_same_attribute
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'tempfile'
|
2
3
|
module ActiveModel
|
3
4
|
class Serializer
|
4
5
|
class CacheTest < Minitest::Test
|
@@ -8,7 +9,7 @@ module ActiveModel
|
|
8
9
|
@post = Post.new(title: 'New Post', body: 'Body')
|
9
10
|
@bio = Bio.new(id: 1, content: 'AMS Contributor')
|
10
11
|
@author = Author.new(name: 'Joao M. D. Moura')
|
11
|
-
@blog = Blog.new(id: 999, name:
|
12
|
+
@blog = Blog.new(id: 999, name: 'Custom blog', writer: @author, articles: [])
|
12
13
|
@role = Role.new(name: 'Great Author')
|
13
14
|
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
|
14
15
|
@place = Place.new(name: 'Amazing Place')
|
@@ -46,20 +47,20 @@ module ActiveModel
|
|
46
47
|
end
|
47
48
|
|
48
49
|
def test_cache_key_interpolation_with_updated_at
|
49
|
-
|
50
|
+
render_object_with_cache(@author)
|
50
51
|
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
|
51
|
-
assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at}").to_json)
|
52
|
+
assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json)
|
52
53
|
end
|
53
54
|
|
54
55
|
def test_default_cache_key_fallback
|
55
|
-
|
56
|
+
render_object_with_cache(@comment)
|
56
57
|
assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
|
57
58
|
end
|
58
59
|
|
59
60
|
def test_cache_options_definition
|
60
|
-
assert_equal({expires_in: 0.1, skip_digest: true}, @post_serializer.class._cache_options)
|
61
|
+
assert_equal({ expires_in: 0.1, skip_digest: true }, @post_serializer.class._cache_options)
|
61
62
|
assert_equal(nil, @blog_serializer.class._cache_options)
|
62
|
-
assert_equal({expires_in: 1.day, skip_digest: true}, @comment_serializer.class._cache_options)
|
63
|
+
assert_equal({ expires_in: 1.day, skip_digest: true }, @comment_serializer.class._cache_options)
|
63
64
|
end
|
64
65
|
|
65
66
|
def test_fragment_cache_definition
|
@@ -72,34 +73,38 @@ module ActiveModel
|
|
72
73
|
assert_equal(nil, ActionController::Base.cache_store.fetch(@post.cache_key))
|
73
74
|
assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
74
75
|
|
75
|
-
|
76
|
+
Timecop.freeze(Time.now) do
|
77
|
+
render_object_with_cache(@post)
|
76
78
|
|
77
|
-
|
78
|
-
|
79
|
+
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
80
|
+
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
81
|
+
end
|
79
82
|
end
|
80
83
|
|
81
84
|
def test_associations_cache_when_updated
|
82
85
|
# Clean the Cache
|
83
86
|
ActionController::Base.cache_store.clear
|
84
87
|
|
85
|
-
|
86
|
-
|
88
|
+
Timecop.freeze(Time.now) do
|
89
|
+
# Generate a new Cache of Post object and each objects related to it.
|
90
|
+
render_object_with_cache(@post)
|
87
91
|
|
88
|
-
|
89
|
-
|
90
|
-
|
92
|
+
# Check if it cached the objects separately
|
93
|
+
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
94
|
+
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
91
95
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
+
# Simulating update on comments relationship with Post
|
97
|
+
new_comment = Comment.new(id: 2, body: 'ZOMG A NEW COMMENT')
|
98
|
+
new_comment_serializer = CommentSerializer.new(new_comment)
|
99
|
+
@post.comments = [new_comment]
|
96
100
|
|
97
|
-
|
98
|
-
|
101
|
+
# Ask for the serialized object
|
102
|
+
render_object_with_cache(@post)
|
99
103
|
|
100
|
-
|
101
|
-
|
102
|
-
|
104
|
+
# Check if the the new comment was cached
|
105
|
+
assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key))
|
106
|
+
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
107
|
+
end
|
103
108
|
end
|
104
109
|
|
105
110
|
def test_fragment_fetch_with_virtual_associations
|
@@ -113,24 +118,46 @@ module ActiveModel
|
|
113
118
|
hash = render_object_with_cache(@location)
|
114
119
|
|
115
120
|
assert_equal(hash, expected_result)
|
116
|
-
assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
|
121
|
+
assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key))
|
117
122
|
end
|
118
123
|
|
119
|
-
def
|
120
|
-
|
124
|
+
def test_uses_file_digest_in_cache_key
|
125
|
+
render_object_with_cache(@blog)
|
121
126
|
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
|
122
127
|
end
|
123
128
|
|
124
|
-
def
|
129
|
+
def test_cache_digest_definition
|
125
130
|
assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
|
126
131
|
end
|
127
132
|
|
133
|
+
def test_serializer_file_path_on_nix
|
134
|
+
path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
|
135
|
+
caller_line = "#{path}:1:in `<top (required)>'"
|
136
|
+
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_serializer_file_path_on_windows
|
140
|
+
path = 'c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
|
141
|
+
caller_line = "#{path}:1:in `<top (required)>'"
|
142
|
+
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_digest_caller_file
|
146
|
+
contents = "puts 'AMS rocks'!"
|
147
|
+
file = Tempfile.new('some_ruby.rb')
|
148
|
+
file.write(contents)
|
149
|
+
path = file.path
|
150
|
+
caller_line = "#{path}:1:in `<top (required)>'"
|
151
|
+
file.close
|
152
|
+
assert_equal ActiveModel::Serializer.digest_caller_file(caller_line), Digest::MD5.hexdigest(contents)
|
153
|
+
ensure
|
154
|
+
file.unlink
|
155
|
+
end
|
156
|
+
|
128
157
|
private
|
158
|
+
|
129
159
|
def render_object_with_cache(obj)
|
130
|
-
|
131
|
-
serializer = serializer_class.new(obj)
|
132
|
-
adapter = ActiveModel::Serializer.adapter.new(serializer)
|
133
|
-
adapter.serializable_hash
|
160
|
+
ActiveModel::SerializableResource.new(obj).serializable_hash
|
134
161
|
end
|
135
162
|
end
|
136
163
|
end
|