active_model_serializers 0.9.13 → 0.10.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +21 -0
- data/.travis.yml +27 -0
- data/CHANGELOG.md +8 -236
- data/CONTRIBUTING.md +23 -12
- data/Gemfile +17 -0
- data/{MIT-LICENSE → LICENSE.txt} +3 -2
- data/README.md +151 -781
- data/Rakefile +12 -0
- data/active_model_serializers.gemspec +26 -0
- data/lib/action_controller/serialization.rb +30 -84
- data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
- data/lib/active_model/serializer/adapter/json.rb +52 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
- data/lib/active_model/serializer/adapter/json_api.rb +152 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +87 -0
- data/lib/active_model/serializer/array_serializer.rb +32 -0
- data/lib/active_model/serializer/configuration.rb +13 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- data/lib/active_model/serializer/version.rb +1 -3
- data/lib/active_model/serializer.rb +193 -276
- data/lib/active_model_serializers.rb +5 -19
- data/lib/generators/serializer/USAGE +6 -0
- data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb +8 -10
- data/lib/{active_model/serializer/generators → generators}/serializer/templates/serializer.rb +2 -2
- data/test/action_controller/adapter_selector_test.rb +51 -0
- data/test/action_controller/explicit_serializer_test.rb +110 -0
- data/test/action_controller/json_api_linked_test.rb +173 -0
- data/test/action_controller/serialization_scope_name_test.rb +63 -0
- data/test/action_controller/serialization_test.rb +365 -0
- data/test/adapter/fragment_cache_test.rb +27 -0
- data/test/adapter/json/belongs_to_test.rb +41 -0
- data/test/adapter/json/collection_test.rb +59 -0
- data/test/adapter/json/has_many_test.rb +36 -0
- data/test/adapter/json_api/belongs_to_test.rb +147 -0
- data/test/adapter/json_api/collection_test.rb +89 -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 +106 -0
- data/test/adapter/json_api/has_one_test.rb +59 -0
- data/test/adapter/json_api/linked_test.rb +257 -0
- data/test/adapter/json_test.rb +34 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +43 -0
- data/test/array_serializer_test.rb +29 -0
- data/test/fixtures/poro.rb +123 -172
- data/test/serializers/adapter_for_test.rb +50 -0
- data/test/serializers/associations_test.rb +106 -0
- data/test/serializers/attribute_test.rb +23 -0
- data/test/serializers/attributes_test.rb +28 -0
- data/test/serializers/cache_test.rb +128 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +26 -0
- data/test/serializers/generators_test.rb +59 -0
- data/test/serializers/meta_test.rb +78 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +56 -0
- data/test/serializers/urls_test.rb +26 -0
- data/test/test_helper.rb +20 -10
- metadata +121 -148
- data/DESIGN.textile +0 -586
- data/lib/action_controller/serialization_test_case.rb +0 -82
- data/lib/active_model/array_serializer.rb +0 -70
- data/lib/active_model/default_serializer.rb +0 -30
- data/lib/active_model/serializable/utils.rb +0 -18
- data/lib/active_model/serializable.rb +0 -61
- data/lib/active_model/serializer/association/has_many.rb +0 -41
- data/lib/active_model/serializer/association/has_one.rb +0 -27
- data/lib/active_model/serializer/association.rb +0 -58
- data/lib/active_model/serializer/config.rb +0 -33
- data/lib/active_model/serializer/generators/resource_override.rb +0 -15
- data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -16
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
- data/lib/active_model/serializer/railtie.rb +0 -24
- data/lib/active_model/serializer_support.rb +0 -7
- data/test/benchmark/app.rb +0 -60
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -41
- data/test/benchmark/setup.rb +0 -75
- data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
- data/test/fixtures/active_record.rb +0 -96
- data/test/fixtures/template.html.erb +0 -1
- data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
- data/test/integration/action_controller/serialization_test.rb +0 -287
- data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
- data/test/integration/active_record/active_record_test.rb +0 -94
- data/test/integration/generators/resource_generator_test.rb +0 -26
- data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
- data/test/integration/generators/serializer_generator_test.rb +0 -41
- data/test/test_app.rb +0 -18
- data/test/tmp/app/serializers/account_serializer.rb +0 -3
- data/test/unit/active_model/array_serializer/except_test.rb +0 -18
- data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
- data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
- data/test/unit/active_model/array_serializer/only_test.rb +0 -18
- data/test/unit/active_model/array_serializer/options_test.rb +0 -16
- data/test/unit/active_model/array_serializer/root_test.rb +0 -102
- data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
- data/test/unit/active_model/array_serializer/serialization_test.rb +0 -239
- data/test/unit/active_model/default_serializer_test.rb +0 -13
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
- data/test/unit/active_model/serializer/associations_test.rb +0 -49
- data/test/unit/active_model/serializer/attributes_test.rb +0 -57
- data/test/unit/active_model/serializer/config_test.rb +0 -91
- data/test/unit/active_model/serializer/filter_test.rb +0 -69
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
- data/test/unit/active_model/serializer/has_many_test.rb +0 -265
- data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
- data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
- data/test/unit/active_model/serializer/has_one_test.rb +0 -253
- data/test/unit/active_model/serializer/key_format_test.rb +0 -25
- data/test/unit/active_model/serializer/meta_test.rb +0 -39
- data/test/unit/active_model/serializer/options_test.rb +0 -42
- data/test/unit/active_model/serializer/root_test.rb +0 -117
- data/test/unit/active_model/serializer/scope_test.rb +0 -49
- data/test/unit/active_model/serializer/url_helpers_test.rb +0 -36
- data/test/unit/active_model/serilizable_test.rb +0 -50
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
module ActiveModel
|
3
|
+
class Serializer
|
4
|
+
class CacheTest < Minitest::Test
|
5
|
+
def setup
|
6
|
+
ActionController::Base.cache_store.clear
|
7
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
8
|
+
@blog = Blog.new(id: 999, name: "Custom blog")
|
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
|
+
@role = Role.new(name: 'Great Author')
|
13
|
+
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
|
14
|
+
@place = Place.new(name: 'Amazing Place')
|
15
|
+
@author.posts = [@post]
|
16
|
+
@author.roles = [@role]
|
17
|
+
@role.author = @author
|
18
|
+
@author.bio = @bio
|
19
|
+
@bio.author = @author
|
20
|
+
@post.comments = [@comment]
|
21
|
+
@post.author = @author
|
22
|
+
@comment.post = @post
|
23
|
+
@comment.author = @author
|
24
|
+
@post.blog = @blog
|
25
|
+
@location.place = @place
|
26
|
+
|
27
|
+
@location_serializer = LocationSerializer.new(@location)
|
28
|
+
@bio_serializer = BioSerializer.new(@bio)
|
29
|
+
@role_serializer = RoleSerializer.new(@role)
|
30
|
+
@post_serializer = PostSerializer.new(@post)
|
31
|
+
@author_serializer = AuthorSerializer.new(@author)
|
32
|
+
@comment_serializer = CommentSerializer.new(@comment)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_cache_definition
|
36
|
+
assert_equal(ActionController::Base.cache_store, @post_serializer.class._cache)
|
37
|
+
assert_equal(ActionController::Base.cache_store, @author_serializer.class._cache)
|
38
|
+
assert_equal(ActionController::Base.cache_store, @comment_serializer.class._cache)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_cache_key_definition
|
42
|
+
assert_equal('post', @post_serializer.class._cache_key)
|
43
|
+
assert_equal('writer', @author_serializer.class._cache_key)
|
44
|
+
assert_equal(nil, @comment_serializer.class._cache_key)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_cache_key_interpolation_with_updated_at
|
48
|
+
author = render_object_with_cache(@author)
|
49
|
+
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
|
50
|
+
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)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_default_cache_key_fallback
|
54
|
+
comment = render_object_with_cache(@comment)
|
55
|
+
assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_cache_options_definition
|
59
|
+
assert_equal({expires_in: 0.1}, @post_serializer.class._cache_options)
|
60
|
+
assert_equal(nil, @author_serializer.class._cache_options)
|
61
|
+
assert_equal({expires_in: 1.day}, @comment_serializer.class._cache_options)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_fragment_cache_definition
|
65
|
+
assert_equal([:name], @role_serializer.class._cache_only)
|
66
|
+
assert_equal([:content], @bio_serializer.class._cache_except)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_associations_separately_cache
|
70
|
+
ActionController::Base.cache_store.clear
|
71
|
+
assert_equal(nil, ActionController::Base.cache_store.fetch(@post.cache_key))
|
72
|
+
assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
73
|
+
|
74
|
+
post = render_object_with_cache(@post)
|
75
|
+
|
76
|
+
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
77
|
+
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_associations_cache_when_updated
|
81
|
+
# Clean the Cache
|
82
|
+
ActionController::Base.cache_store.clear
|
83
|
+
|
84
|
+
# Generate a new Cache of Post object and each objects related to it.
|
85
|
+
render_object_with_cache(@post)
|
86
|
+
|
87
|
+
# Check if if cache the objects separately
|
88
|
+
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
89
|
+
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
90
|
+
|
91
|
+
# Simulating update on comments relationship with Post
|
92
|
+
new_comment = Comment.new(id: 2, body: 'ZOMG A NEW COMMENT')
|
93
|
+
new_comment_serializer = CommentSerializer.new(new_comment)
|
94
|
+
@post.comments = [new_comment]
|
95
|
+
|
96
|
+
# Ask for the serialized object
|
97
|
+
render_object_with_cache(@post)
|
98
|
+
|
99
|
+
# Check if the the new comment was cached
|
100
|
+
assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key))
|
101
|
+
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_fragment_fetch_with_virtual_associations
|
105
|
+
expected_result = {
|
106
|
+
id: @location.id,
|
107
|
+
lat: @location.lat,
|
108
|
+
lng: @location.lng,
|
109
|
+
place: 'Nowhere'
|
110
|
+
}
|
111
|
+
|
112
|
+
hash = render_object_with_cache(@location)
|
113
|
+
|
114
|
+
assert_equal(hash, expected_result)
|
115
|
+
assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
def render_object_with_cache(obj)
|
120
|
+
serializer_class = ActiveModel::Serializer.serializer_for(obj)
|
121
|
+
serializer = serializer_class.new(obj)
|
122
|
+
adapter = ActiveModel::Serializer.adapter.new(serializer)
|
123
|
+
adapter.serializable_hash
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class ConfigurationTest < Minitest::Test
|
6
|
+
def test_array_serializer
|
7
|
+
assert_equal ActiveModel::Serializer::ArraySerializer, ActiveModel::Serializer.config.array_serializer
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_default_adapter
|
11
|
+
assert_equal :json, ActiveModel::Serializer.config.adapter
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class FieldsetTest < Minitest::Test
|
6
|
+
|
7
|
+
def test_fieldset_with_hash
|
8
|
+
fieldset = ActiveModel::Serializer::Fieldset.new({'post' => ['id', 'title'], 'coment' => ['body']})
|
9
|
+
|
10
|
+
assert_equal(
|
11
|
+
{:post=>[:id, :title], :coment=>[:body]},
|
12
|
+
fieldset.fields
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_fieldset_with_array_of_fields_and_root_name
|
17
|
+
fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
|
18
|
+
|
19
|
+
assert_equal(
|
20
|
+
{:post => [:title]},
|
21
|
+
fieldset.fields
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class Foo < Rails::Application
|
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
|
+
|
10
|
+
require 'generators/serializer/serializer_generator'
|
11
|
+
|
12
|
+
class SerializerGeneratorTest < Rails::Generators::TestCase
|
13
|
+
destination File.expand_path("../tmp", __FILE__)
|
14
|
+
setup :prepare_destination
|
15
|
+
|
16
|
+
tests Rails::Generators::SerializerGenerator
|
17
|
+
arguments %w(account name:string description:text business:references)
|
18
|
+
|
19
|
+
def test_generates_a_serializer
|
20
|
+
run_generator
|
21
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_generates_a_namespaced_serializer
|
25
|
+
run_generator ["admin/account"]
|
26
|
+
assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_uses_application_serializer_if_one_exists
|
30
|
+
Object.const_set(:ApplicationSerializer, Class.new)
|
31
|
+
run_generator
|
32
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/
|
33
|
+
ensure
|
34
|
+
Object.send :remove_const, :ApplicationSerializer
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_uses_given_parent
|
38
|
+
Object.const_set(:ApplicationSerializer, Class.new)
|
39
|
+
run_generator ["Account", "--parent=MySerializer"]
|
40
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < MySerializer/
|
41
|
+
ensure
|
42
|
+
Object.send :remove_const, :ApplicationSerializer
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_generates_attributes_and_associations
|
46
|
+
run_generator
|
47
|
+
assert_file "app/serializers/account_serializer.rb" do |serializer|
|
48
|
+
assert_match(/^ attributes :id, :name, :description$/, serializer)
|
49
|
+
assert_match(/^ attribute :business$/, serializer)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_with_no_attributes_does_not_add_extra_space
|
54
|
+
run_generator ["account"]
|
55
|
+
assert_file "app/serializers/account_serializer.rb" do |content|
|
56
|
+
assert_no_match /\n\nend/, content
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class MetaTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
ActionController::Base.cache_store.clear
|
8
|
+
@blog = Blog.new(id: 1,
|
9
|
+
name: 'AMS Hints',
|
10
|
+
writer: Author.new(id: 2, name: "Steve"),
|
11
|
+
articles: [Post.new(id: 3, title: "AMS")])
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_meta_is_present_with_root
|
15
|
+
adapter = load_adapter(root: "blog", meta: {total: 10})
|
16
|
+
expected = {
|
17
|
+
"blog" => {
|
18
|
+
id: 1,
|
19
|
+
title: "AMS Hints"
|
20
|
+
},
|
21
|
+
"meta" => {
|
22
|
+
total: 10
|
23
|
+
}
|
24
|
+
}
|
25
|
+
assert_equal expected, adapter.as_json
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_meta_is_not_included_when_root_is_missing
|
29
|
+
adapter = load_adapter(meta: {total: 10})
|
30
|
+
expected = {
|
31
|
+
id: 1,
|
32
|
+
title: "AMS Hints"
|
33
|
+
}
|
34
|
+
assert_equal expected, adapter.as_json
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_meta_key_is_used
|
38
|
+
adapter = load_adapter(root: "blog", meta: {total: 10}, meta_key: "haha_meta")
|
39
|
+
expected = {
|
40
|
+
"blog" => {
|
41
|
+
id: 1,
|
42
|
+
title: "AMS Hints"
|
43
|
+
},
|
44
|
+
"haha_meta" => {
|
45
|
+
total: 10
|
46
|
+
}
|
47
|
+
}
|
48
|
+
assert_equal expected, adapter.as_json
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_meta_is_not_used_on_arrays
|
52
|
+
serializer = ArraySerializer.new([@blog], root: "blog", meta: {total: 10}, meta_key: "haha_meta")
|
53
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
54
|
+
expected = [{
|
55
|
+
id: 1,
|
56
|
+
name: "AMS Hints",
|
57
|
+
writer: {
|
58
|
+
id: 2,
|
59
|
+
name: "Steve"
|
60
|
+
},
|
61
|
+
articles: [{
|
62
|
+
id: 3,
|
63
|
+
title: "AMS",
|
64
|
+
body: nil
|
65
|
+
}]
|
66
|
+
}]
|
67
|
+
assert_equal expected, adapter.as_json
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def load_adapter(options)
|
73
|
+
serializer = AlternateBlogSerializer.new(@blog, options)
|
74
|
+
ActiveModel::Serializer::Adapter::Json.new(serializer)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class OptionsTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_options_are_accessible
|
11
|
+
@profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
|
12
|
+
assert @profile_serializer.arguments_passed_in?
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_no_option_is_passed_in
|
16
|
+
@profile_serializer = ProfileSerializer.new(@profile)
|
17
|
+
refute @profile_serializer.arguments_passed_in?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class SerializerForTest < Minitest::Test
|
6
|
+
class ArraySerializerTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@array = [1, 2, 3]
|
9
|
+
@previous_array_serializer = ActiveModel::Serializer.config.array_serializer
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
ActiveModel::Serializer.config.array_serializer = @previous_array_serializer
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_serializer_for_array
|
17
|
+
serializer = ActiveModel::Serializer.serializer_for(@array)
|
18
|
+
assert_equal ActiveModel::Serializer.config.array_serializer, serializer
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_overwritten_serializer_for_array
|
22
|
+
new_array_serializer = Class.new
|
23
|
+
ActiveModel::Serializer.config.array_serializer = new_array_serializer
|
24
|
+
serializer = ActiveModel::Serializer.serializer_for(@array)
|
25
|
+
assert_equal new_array_serializer, serializer
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class SerializerTest < Minitest::Test
|
30
|
+
class MyProfile < Profile
|
31
|
+
end
|
32
|
+
|
33
|
+
def setup
|
34
|
+
@profile = Profile.new
|
35
|
+
@my_profile = MyProfile.new
|
36
|
+
@model = ::Model.new
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_serializer_for_existing_serializer
|
40
|
+
serializer = ActiveModel::Serializer.serializer_for(@profile)
|
41
|
+
assert_equal ProfileSerializer, serializer
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_serializer_for_not_existing_serializer
|
45
|
+
serializer = ActiveModel::Serializer.serializer_for(@model)
|
46
|
+
assert_equal nil, serializer
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_serializer_inherited_serializer
|
50
|
+
serializer = ActiveModel::Serializer.serializer_for(@my_profile)
|
51
|
+
assert_equal ProfileSerializer, serializer
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class UrlsTest < Minitest::Test
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
@post = Post.new({ title: 'New Post', body: 'Body' })
|
10
|
+
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
11
|
+
@post.comments = [@comment]
|
12
|
+
|
13
|
+
@profile_serializer = ProfileSerializer.new(@profile)
|
14
|
+
@post_serializer = PostSerializer.new(@post)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_urls_definition
|
18
|
+
assert_equal([:posts, :comments], @profile_serializer.class._urls)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_url_definition
|
22
|
+
assert_equal([:comments], @post_serializer.class._urls)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,17 +1,24 @@
|
|
1
1
|
require 'bundler/setup'
|
2
|
+
|
3
|
+
require 'rails'
|
4
|
+
require 'action_controller'
|
5
|
+
require 'action_controller/test_case'
|
6
|
+
require 'action_controller/railtie'
|
7
|
+
require 'active_support/json'
|
2
8
|
require 'minitest/autorun'
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
Rails
|
8
|
-
|
9
|
-
|
9
|
+
# Ensure backward compatibility with Minitest 4
|
10
|
+
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
11
|
+
|
12
|
+
class Foo < Rails::Application
|
13
|
+
if Rails.version.to_s.start_with? '4'
|
14
|
+
config.action_controller.perform_caching = true
|
15
|
+
ActionController::Base.cache_store = :memory_store
|
16
|
+
end
|
10
17
|
end
|
11
18
|
|
19
|
+
require "active_model_serializers"
|
12
20
|
|
13
|
-
|
14
|
-
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
21
|
+
require 'fixtures/poro'
|
15
22
|
|
16
23
|
module TestHelper
|
17
24
|
Routes = ActionDispatch::Routing::RouteSet.new
|
@@ -21,7 +28,6 @@ module TestHelper
|
|
21
28
|
end
|
22
29
|
|
23
30
|
ActionController::Base.send :include, Routes.url_helpers
|
24
|
-
ActionController::Base.send :include, ActionController::Serialization
|
25
31
|
end
|
26
32
|
|
27
33
|
ActionController::TestCase.class_eval do
|
@@ -29,3 +35,7 @@ ActionController::TestCase.class_eval do
|
|
29
35
|
@routes = TestHelper::Routes
|
30
36
|
end
|
31
37
|
end
|
38
|
+
|
39
|
+
def def_serializer(&block)
|
40
|
+
Class.new(ActiveModel::Serializer, &block)
|
41
|
+
end
|