active_model_serializers 0.9.8 → 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 -200
- 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 -75
- data/lib/active_model/serializer.rb +192 -252
- data/lib/active_model/serializer/adapter.rb +87 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
- data/lib/active_model/serializer/adapter/json.rb +52 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
- data/lib/active_model/serializer/adapter/json_api.rb +152 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -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 -1
- data/lib/active_model_serializers.rb +5 -7
- data/lib/generators/serializer/USAGE +6 -0
- data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb +8 -8
- 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 +125 -142
- 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 +21 -4
- metadata +121 -159
- data/DESIGN.textile +0 -586
- data/lib/action_controller/serialization_test_case.rb +0 -79
- data/lib/active_model/array_serializer.rb +0 -68
- data/lib/active_model/default_serializer.rb +0 -28
- data/lib/active_model/serializable.rb +0 -59
- data/lib/active_model/serializable/utils.rb +0 -16
- data/lib/active_model/serializer/association.rb +0 -58
- data/lib/active_model/serializer/association/has_many.rb +0 -39
- data/lib/active_model/serializer/association/has_one.rb +0 -25
- data/lib/active_model/serializer/config.rb +0 -31
- data/lib/active_model/serializer/generators/resource_override.rb +0 -13
- data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -14
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
- data/lib/active_model/serializer/railtie.rb +0 -22
- data/lib/active_model/serializer_support.rb +0 -5
- data/lib/active_model_serializers/model/caching.rb +0 -25
- 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 -14
- data/test/tmp/app/assets/javascripts/accounts.js +0 -2
- data/test/tmp/app/assets/stylesheets/accounts.css +0 -4
- data/test/tmp/app/controllers/accounts_controller.rb +0 -2
- data/test/tmp/app/helpers/accounts_helper.rb +0 -2
- data/test/tmp/app/serializers/account_serializer.rb +0 -3
- data/test/tmp/config/routes.rb +0 -1
- 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 -216
- 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 -35
- data/test/unit/active_model/serilizable_test.rb +0 -50
@@ -0,0 +1,106 @@
|
|
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
|
+
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
33
|
+
@post.comments = [@comment]
|
34
|
+
@post.blog = @blog
|
35
|
+
@comment.post = @post
|
36
|
+
@comment.author = nil
|
37
|
+
@post.author = @author
|
38
|
+
@author.posts = [@post]
|
39
|
+
|
40
|
+
@post_serializer = PostSerializer.new(@post, {custom_options: true})
|
41
|
+
@author_serializer = AuthorSerializer.new(@author)
|
42
|
+
@comment_serializer = CommentSerializer.new(@comment)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_has_many_and_has_one
|
46
|
+
assert_equal(
|
47
|
+
{ posts: { type: :has_many, association_options: { embed: :ids } },
|
48
|
+
roles: { type: :has_many, association_options: { embed: :ids } },
|
49
|
+
bio: { type: :has_one, association_options: {} } },
|
50
|
+
@author_serializer.class._associations
|
51
|
+
)
|
52
|
+
@author_serializer.each_association do |name, serializer, options|
|
53
|
+
if name == :posts
|
54
|
+
assert_equal({embed: :ids}, options)
|
55
|
+
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
56
|
+
elsif name == :bio
|
57
|
+
assert_equal({}, options)
|
58
|
+
assert_nil serializer
|
59
|
+
elsif name == :roles
|
60
|
+
assert_equal({embed: :ids}, options)
|
61
|
+
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
62
|
+
else
|
63
|
+
flunk "Unknown association: #{name}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_serializer_options_are_passed_into_associations_serializers
|
69
|
+
@post_serializer.each_association do |name, association|
|
70
|
+
if name == :comments
|
71
|
+
assert association.first.custom_options[:custom_options]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_belongs_to
|
77
|
+
assert_equal(
|
78
|
+
{ post: { type: :belongs_to, association_options: {} },
|
79
|
+
author: { type: :belongs_to, association_options: {} } },
|
80
|
+
@comment_serializer.class._associations
|
81
|
+
)
|
82
|
+
@comment_serializer.each_association do |name, serializer, options|
|
83
|
+
if name == :post
|
84
|
+
assert_equal({}, options)
|
85
|
+
assert_kind_of(PostSerializer, serializer)
|
86
|
+
elsif name == :author
|
87
|
+
assert_equal({}, options)
|
88
|
+
assert_nil serializer
|
89
|
+
else
|
90
|
+
flunk "Unknown association: #{name}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_belongs_to_with_custom_method
|
96
|
+
blog_is_present = false
|
97
|
+
|
98
|
+
@post_serializer.each_association do |name, serializer, options|
|
99
|
+
blog_is_present = true if name == :blog
|
100
|
+
end
|
101
|
+
|
102
|
+
assert blog_is_present
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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' })
|
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({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,28 @@
|
|
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
|
+
end
|
10
|
+
|
11
|
+
def test_attributes_definition
|
12
|
+
assert_equal([:name, :description],
|
13
|
+
@profile_serializer.class._attributes)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_attributes_with_fields_option
|
17
|
+
assert_equal({name: 'Name 1'},
|
18
|
+
@profile_serializer.attributes(fields: [:name]))
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_required_fields
|
22
|
+
assert_equal({name: 'Name 1', description: 'Description 1'},
|
23
|
+
@profile_serializer.attributes(fields: [:name, :description], required_fields: [:name]))
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -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
|