active_model_serializers 0.8.3 → 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 +5 -0
- data/.rubocop.yml +82 -0
- data/.rubocop_todo.yml +315 -0
- data/.simplecov +99 -0
- data/.travis.yml +26 -20
- data/CHANGELOG.md +14 -67
- data/CONTRIBUTING.md +31 -0
- data/Gemfile +45 -1
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
- data/README.md +186 -488
- data/Rakefile +33 -12
- data/active_model_serializers.gemspec +49 -23
- 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 +31 -31
- data/lib/active_model/serializable_resource.rb +70 -0
- data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +75 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +5 -0
- data/lib/active_model/serializer/adapter/json.rb +47 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +13 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +50 -0
- data/lib/active_model/serializer/adapter/json_api.rb +158 -0
- data/lib/active_model/serializer/adapter/null.rb +5 -0
- data/lib/active_model/serializer/adapter.rb +159 -0
- data/lib/active_model/serializer/array_serializer.rb +40 -0
- data/lib/active_model/serializer/association.rb +20 -0
- data/lib/active_model/serializer/associations.rb +83 -219
- 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 +14 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- 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 +15 -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/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +121 -465
- data/lib/active_model_serializers.rb +26 -88
- data/lib/generators/serializer/USAGE +0 -3
- data/lib/generators/serializer/resource_override.rb +12 -0
- data/lib/generators/serializer/serializer_generator.rb +8 -13
- data/lib/generators/serializer/templates/serializer.rb.erb +8 -0
- data/lib/tasks/rubocop.rake +0 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json_api/linked_test.rb +179 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +15 -15
- data/test/action_controller/serialization_test.rb +420 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/fragment_cache_test.rb +37 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +82 -0
- data/test/adapter/json/has_many_test.rb +47 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +95 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +145 -0
- data/test/adapter/json_api/has_one_test.rb +81 -0
- data/test/adapter/json_api/json_api_test.rb +37 -0
- data/test/adapter/json_api/linked_test.rb +283 -0
- 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 +47 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +42 -0
- data/test/array_serializer_test.rb +99 -73
- data/test/capture_warnings.rb +65 -0
- data/test/fixtures/active_record.rb +56 -0
- data/test/fixtures/poro.rb +261 -0
- data/test/generators/scaffold_controller_generator_test.rb +23 -0
- data/test/generators/serializer_generator_test.rb +56 -0
- 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 +170 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +150 -0
- data/test/serializers/attribute_test.rb +62 -0
- data/test/serializers/attributes_test.rb +57 -0
- data/test/serializers/cache_test.rb +165 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +25 -0
- data/test/serializers/meta_test.rb +121 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +65 -0
- 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 +48 -24
- data/test/utils/include_args_to_hash_test.rb +79 -0
- metadata +219 -47
- data/DESIGN.textile +0 -586
- data/Gemfile.edge +0 -9
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
data/test/poro_test.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class SerializableResourceTest < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
7
|
+
@serializer = ProfileSerializer.new(@resource)
|
8
|
+
@adapter = ActiveModel::Serializer::Adapter.create(@serializer)
|
9
|
+
@serializable_resource = ActiveModel::SerializableResource.new(@resource)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_serializable_resource_delegates_serializable_hash_to_the_adapter
|
13
|
+
options = nil
|
14
|
+
assert_equal @adapter.serializable_hash(options), @serializable_resource.serializable_hash(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_serializable_resource_delegates_to_json_to_the_adapter
|
18
|
+
options = nil
|
19
|
+
assert_equal @adapter.to_json(options), @serializable_resource.to_json(options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_serializable_resource_delegates_as_json_to_the_adapter
|
23
|
+
options = nil
|
24
|
+
assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Serializer
|
3
|
+
class AdapterForTest < Minitest::Test
|
4
|
+
UnknownAdapterError = ::ActiveModel::Serializer::Adapter::UnknownAdapterError
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@previous_adapter = ActiveModel::Serializer.config.adapter
|
8
|
+
# Eager load adapters
|
9
|
+
ActiveModel::Serializer::Adapter.eager_load!
|
10
|
+
[:json_api, :flatten_json, :null, :json].each do |adapter_name|
|
11
|
+
ActiveModel::Serializer::Adapter.lookup(adapter_name)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
ActiveModel::Serializer.config.adapter = @previous_adapter
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_returns_default_adapter
|
20
|
+
adapter = ActiveModel::Serializer.adapter
|
21
|
+
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_overwrite_adapter_with_symbol
|
25
|
+
ActiveModel::Serializer.config.adapter = :null
|
26
|
+
|
27
|
+
adapter = ActiveModel::Serializer.adapter
|
28
|
+
assert_equal ActiveModel::Serializer::Adapter::Null, adapter
|
29
|
+
ensure
|
30
|
+
ActiveModel::Serializer.config.adapter = @previous_adapter
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_overwrite_adapter_with_class
|
34
|
+
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::Null
|
35
|
+
|
36
|
+
adapter = ActiveModel::Serializer.adapter
|
37
|
+
assert_equal ActiveModel::Serializer::Adapter::Null, adapter
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_raises_exception_if_invalid_symbol_given
|
41
|
+
ActiveModel::Serializer.config.adapter = :unknown
|
42
|
+
|
43
|
+
assert_raises UnknownAdapterError do
|
44
|
+
ActiveModel::Serializer.adapter
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_raises_exception_if_it_does_not_know_hot_to_infer_adapter
|
49
|
+
ActiveModel::Serializer.config.adapter = 42
|
50
|
+
|
51
|
+
assert_raises UnknownAdapterError do
|
52
|
+
ActiveModel::Serializer.adapter
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_adapter_class_for_known_adapter
|
57
|
+
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api)
|
58
|
+
assert_equal ActiveModel::Serializer::Adapter::JsonApi, klass
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_adapter_class_for_unknown_adapter
|
62
|
+
assert_raises UnknownAdapterError do
|
63
|
+
ActiveModel::Serializer::Adapter.adapter_class(:json_simple)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_adapter_map
|
68
|
+
expected_adapter_map = {
|
69
|
+
'json'.freeze => ActiveModel::Serializer::Adapter::Json,
|
70
|
+
'json_api'.freeze => ActiveModel::Serializer::Adapter::JsonApi,
|
71
|
+
'flatten_json'.freeze => ActiveModel::Serializer::Adapter::FlattenJson,
|
72
|
+
'null'.freeze => ActiveModel::Serializer::Adapter::Null
|
73
|
+
}
|
74
|
+
assert_equal ActiveModel::Serializer::Adapter.adapter_map, expected_adapter_map
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_adapters
|
78
|
+
assert_equal ActiveModel::Serializer::Adapter.adapters.sort, [
|
79
|
+
'flatten_json'.freeze,
|
80
|
+
'json'.freeze,
|
81
|
+
'json_api'.freeze,
|
82
|
+
'null'.freeze
|
83
|
+
]
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_lookup_adapter_by_string_name
|
87
|
+
assert_equal ActiveModel::Serializer::Adapter.lookup('json'.freeze), ActiveModel::Serializer::Adapter::Json
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_lookup_adapter_by_symbol_name
|
91
|
+
assert_equal ActiveModel::Serializer::Adapter.lookup(:json), ActiveModel::Serializer::Adapter::Json
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_lookup_adapter_by_class
|
95
|
+
klass = ActiveModel::Serializer::Adapter::Json
|
96
|
+
assert_equal ActiveModel::Serializer::Adapter.lookup(klass), klass
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_lookup_adapter_from_environment_registers_adapter
|
100
|
+
ActiveModel::Serializer::Adapter.const_set(:AdapterFromEnvironment, Class.new)
|
101
|
+
klass = ::ActiveModel::Serializer::Adapter::AdapterFromEnvironment
|
102
|
+
name = 'adapter_from_environment'.freeze
|
103
|
+
assert_equal ActiveModel::Serializer::Adapter.lookup(name), klass
|
104
|
+
assert ActiveModel::Serializer::Adapter.adapters.include?(name)
|
105
|
+
ensure
|
106
|
+
ActiveModel::Serializer::Adapter.adapter_map.delete(name)
|
107
|
+
ActiveModel::Serializer::Adapter.send(:remove_const, :AdapterFromEnvironment)
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_lookup_adapter_for_unknown_name
|
111
|
+
assert_raises UnknownAdapterError do
|
112
|
+
ActiveModel::Serializer::Adapter.lookup(:json_simple)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_adapter
|
117
|
+
assert_equal ActiveModel::Serializer.config.adapter, :flatten_json
|
118
|
+
assert_equal ActiveModel::Serializer.adapter, ActiveModel::Serializer::Adapter::FlattenJson
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_register_adapter
|
122
|
+
new_adapter_name = :foo
|
123
|
+
new_adapter_klass = Class.new
|
124
|
+
ActiveModel::Serializer::Adapter.register(new_adapter_name, new_adapter_klass)
|
125
|
+
assert ActiveModel::Serializer::Adapter.adapters.include?('foo'.freeze)
|
126
|
+
assert ActiveModel::Serializer::Adapter.lookup(:foo), new_adapter_klass
|
127
|
+
ensure
|
128
|
+
ActiveModel::Serializer::Adapter.adapter_map.delete(new_adapter_name.to_s)
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_inherited_adapter_hooks_register_adapter
|
132
|
+
Object.const_set(:MyAdapter, Class.new)
|
133
|
+
my_adapter = MyAdapter
|
134
|
+
ActiveModel::Serializer::Adapter.inherited(my_adapter)
|
135
|
+
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_adapter), my_adapter
|
136
|
+
ensure
|
137
|
+
ActiveModel::Serializer::Adapter.adapter_map.delete('my_adapter'.freeze)
|
138
|
+
Object.send(:remove_const, :MyAdapter)
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_inherited_adapter_hooks_register_demodulized_adapter
|
142
|
+
Object.const_set(:MyNamespace, Module.new)
|
143
|
+
MyNamespace.const_set(:MyAdapter, Class.new)
|
144
|
+
my_adapter = MyNamespace::MyAdapter
|
145
|
+
ActiveModel::Serializer::Adapter.inherited(my_adapter)
|
146
|
+
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_adapter), my_adapter
|
147
|
+
ensure
|
148
|
+
ActiveModel::Serializer::Adapter.adapter_map.delete('my_adapter'.freeze)
|
149
|
+
MyNamespace.send(:remove_const, :MyAdapter)
|
150
|
+
Object.send(:remove_const, :MyNamespace)
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_inherited_adapter_hooks_register_subclass_of_registered_adapter
|
154
|
+
Object.const_set(:MyAdapter, Class.new)
|
155
|
+
my_adapter = MyAdapter
|
156
|
+
Object.const_set(:MySubclassedAdapter, Class.new(MyAdapter))
|
157
|
+
my_subclassed_adapter = MySubclassedAdapter
|
158
|
+
ActiveModel::Serializer::Adapter.inherited(my_adapter)
|
159
|
+
ActiveModel::Serializer::Adapter.inherited(my_subclassed_adapter)
|
160
|
+
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_adapter), my_adapter
|
161
|
+
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_subclassed_adapter), my_subclassed_adapter
|
162
|
+
ensure
|
163
|
+
ActiveModel::Serializer::Adapter.adapter_map.delete('my_adapter'.freeze)
|
164
|
+
ActiveModel::Serializer::Adapter.adapter_map.delete('my_subclassed_adapter'.freeze)
|
165
|
+
Object.send(:remove_const, :MyAdapter)
|
166
|
+
Object.send(:remove_const, :MySubclassedAdapter)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -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
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class AssociationsTest < Minitest::Test
|
6
|
+
class Model
|
7
|
+
def initialize(hash = {})
|
8
|
+
@attributes = hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def read_attribute_for_serialization(name)
|
12
|
+
@attributes[name]
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(meth, *args)
|
16
|
+
if meth.to_s =~ /^(.*)=$/
|
17
|
+
@attributes[$1.to_sym] = args[0]
|
18
|
+
elsif @attributes.key?(meth)
|
19
|
+
@attributes[meth]
|
20
|
+
else
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def setup
|
27
|
+
@author = Author.new(name: 'Steve K.')
|
28
|
+
@author.bio = nil
|
29
|
+
@author.roles = []
|
30
|
+
@blog = Blog.new({ name: 'AMS Blog' })
|
31
|
+
@post = Post.new({ title: 'New Post', body: 'Body' })
|
32
|
+
@tag = Tag.new({ name: '#hashtagged' })
|
33
|
+
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
34
|
+
@post.comments = [@comment]
|
35
|
+
@post.tags = [@tag]
|
36
|
+
@post.blog = @blog
|
37
|
+
@comment.post = @post
|
38
|
+
@comment.author = nil
|
39
|
+
@post.author = @author
|
40
|
+
@author.posts = [@post]
|
41
|
+
|
42
|
+
@post_serializer = PostSerializer.new(@post, { custom_options: true })
|
43
|
+
@author_serializer = AuthorSerializer.new(@author)
|
44
|
+
@comment_serializer = CommentSerializer.new(@comment)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_has_many_and_has_one
|
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)
|
56
|
+
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
57
|
+
when :bio
|
58
|
+
assert_equal({}, options)
|
59
|
+
assert_nil serializer
|
60
|
+
when :roles
|
61
|
+
assert_equal({}, options)
|
62
|
+
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
63
|
+
else
|
64
|
+
flunk "Unknown association: #{key}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
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
|
78
|
+
end
|
79
|
+
end
|
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
|
+
|
89
|
+
def test_belongs_to
|
90
|
+
@comment_serializer.associations.each do |association|
|
91
|
+
key = association.key
|
92
|
+
serializer = association.serializer
|
93
|
+
|
94
|
+
case key
|
95
|
+
when :post
|
96
|
+
assert_kind_of(PostSerializer, serializer)
|
97
|
+
when :author
|
98
|
+
assert_nil serializer
|
99
|
+
else
|
100
|
+
flunk "Unknown association: #{key}"
|
101
|
+
end
|
102
|
+
|
103
|
+
assert_equal({}, association.options)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_belongs_to_with_custom_method
|
108
|
+
assert(
|
109
|
+
@post_serializer.associations.any? do |association|
|
110
|
+
association.key == :blog
|
111
|
+
end
|
112
|
+
)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_associations_inheritance
|
116
|
+
inherited_klass = Class.new(PostSerializer)
|
117
|
+
|
118
|
+
assert_equal(PostSerializer._reflections, inherited_klass._reflections)
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_associations_inheritance_with_new_association
|
122
|
+
inherited_klass = Class.new(PostSerializer) do
|
123
|
+
has_many :top_comments, serializer: CommentSerializer
|
124
|
+
end
|
125
|
+
|
126
|
+
assert(
|
127
|
+
PostSerializer._reflections.all? do |reflection|
|
128
|
+
inherited_klass._reflections.include?(reflection)
|
129
|
+
end
|
130
|
+
)
|
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
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class AttributeTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@blog = Blog.new({ id: 1, name: 'AMS Hints', type: 'stuff' })
|
8
|
+
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_attributes_definition
|
12
|
+
assert_equal([:id, :title],
|
13
|
+
@blog_serializer.class._attributes)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_json_serializable_hash
|
17
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
18
|
+
assert_equal({ 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)
|
25
|
+
assert_equal({ :id => 1, :title => 'AMS Hints' }, adapter.serializable_hash)
|
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
|
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
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class AttributesTest < 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
|
+
@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
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_attributes_definition
|
17
|
+
assert_equal([:name, :description],
|
18
|
+
@profile_serializer.class._attributes)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_attributes_with_fields_option
|
22
|
+
assert_equal({ name: 'Name 1' },
|
23
|
+
@profile_serializer.attributes(fields: [:name]))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_attributes_inheritance_definition
|
27
|
+
assert_equal([:id, :body], @serializer_klass._attributes)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_attributes_inheritance
|
31
|
+
serializer = @serializer_klass.new(@comment)
|
32
|
+
assert_equal({ id: 1, body: 'ZOMG!!' },
|
33
|
+
serializer.attributes)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_attribute_inheritance_with_new_attribute_definition
|
37
|
+
assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes)
|
38
|
+
assert_equal([:id, :body], CommentSerializer._attributes)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_attribute_inheritance_with_new_attribute
|
42
|
+
serializer = @serializer_klass_with_new_attributes.new(@comment)
|
43
|
+
assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil },
|
44
|
+
serializer.attributes)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_multiple_calls_with_the_same_attribute
|
48
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
49
|
+
attributes :id, :title
|
50
|
+
attributes :id, :title, :title, :body
|
51
|
+
end
|
52
|
+
|
53
|
+
assert_equal([:id, :title, :body], serializer_class._attributes)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class CacheTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
ActionController::Base.cache_store.clear
|
8
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
9
|
+
@post = Post.new(title: 'New Post', body: 'Body')
|
10
|
+
@bio = Bio.new(id: 1, content: 'AMS Contributor')
|
11
|
+
@author = Author.new(name: 'Joao M. D. Moura')
|
12
|
+
@blog = Blog.new(id: 999, name: 'Custom blog', writer: @author, articles: [])
|
13
|
+
@role = Role.new(name: 'Great Author')
|
14
|
+
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
|
15
|
+
@place = Place.new(name: 'Amazing Place')
|
16
|
+
@author.posts = [@post]
|
17
|
+
@author.roles = [@role]
|
18
|
+
@role.author = @author
|
19
|
+
@author.bio = @bio
|
20
|
+
@bio.author = @author
|
21
|
+
@post.comments = [@comment]
|
22
|
+
@post.author = @author
|
23
|
+
@comment.post = @post
|
24
|
+
@comment.author = @author
|
25
|
+
@post.blog = @blog
|
26
|
+
@location.place = @place
|
27
|
+
|
28
|
+
@location_serializer = LocationSerializer.new(@location)
|
29
|
+
@bio_serializer = BioSerializer.new(@bio)
|
30
|
+
@role_serializer = RoleSerializer.new(@role)
|
31
|
+
@post_serializer = PostSerializer.new(@post)
|
32
|
+
@author_serializer = AuthorSerializer.new(@author)
|
33
|
+
@comment_serializer = CommentSerializer.new(@comment)
|
34
|
+
@blog_serializer = BlogSerializer.new(@blog)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_cache_definition
|
38
|
+
assert_equal(ActionController::Base.cache_store, @post_serializer.class._cache)
|
39
|
+
assert_equal(ActionController::Base.cache_store, @author_serializer.class._cache)
|
40
|
+
assert_equal(ActionController::Base.cache_store, @comment_serializer.class._cache)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_cache_key_definition
|
44
|
+
assert_equal('post', @post_serializer.class._cache_key)
|
45
|
+
assert_equal('writer', @author_serializer.class._cache_key)
|
46
|
+
assert_equal(nil, @comment_serializer.class._cache_key)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_cache_key_interpolation_with_updated_at
|
50
|
+
render_object_with_cache(@author)
|
51
|
+
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
|
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)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_default_cache_key_fallback
|
56
|
+
render_object_with_cache(@comment)
|
57
|
+
assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_cache_options_definition
|
61
|
+
assert_equal({ expires_in: 0.1, skip_digest: true }, @post_serializer.class._cache_options)
|
62
|
+
assert_equal(nil, @blog_serializer.class._cache_options)
|
63
|
+
assert_equal({ expires_in: 1.day, skip_digest: true }, @comment_serializer.class._cache_options)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_fragment_cache_definition
|
67
|
+
assert_equal([:name], @role_serializer.class._cache_only)
|
68
|
+
assert_equal([:content], @bio_serializer.class._cache_except)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_associations_separately_cache
|
72
|
+
ActionController::Base.cache_store.clear
|
73
|
+
assert_equal(nil, ActionController::Base.cache_store.fetch(@post.cache_key))
|
74
|
+
assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
75
|
+
|
76
|
+
Timecop.freeze(Time.now) do
|
77
|
+
render_object_with_cache(@post)
|
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
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_associations_cache_when_updated
|
85
|
+
# Clean the Cache
|
86
|
+
ActionController::Base.cache_store.clear
|
87
|
+
|
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)
|
91
|
+
|
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))
|
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]
|
100
|
+
|
101
|
+
# Ask for the serialized object
|
102
|
+
render_object_with_cache(@post)
|
103
|
+
|
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
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_fragment_fetch_with_virtual_associations
|
111
|
+
expected_result = {
|
112
|
+
id: @location.id,
|
113
|
+
lat: @location.lat,
|
114
|
+
lng: @location.lng,
|
115
|
+
place: 'Nowhere'
|
116
|
+
}
|
117
|
+
|
118
|
+
hash = render_object_with_cache(@location)
|
119
|
+
|
120
|
+
assert_equal(hash, expected_result)
|
121
|
+
assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key))
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_uses_file_digest_in_cache_key
|
125
|
+
render_object_with_cache(@blog)
|
126
|
+
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_cache_digest_definition
|
130
|
+
assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
|
131
|
+
end
|
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
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def render_object_with_cache(obj)
|
160
|
+
ActiveModel::SerializableResource.new(obj).serializable_hash
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|