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,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class NullTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
serializer = ProfileSerializer.new(profile)
|
10
|
+
|
11
|
+
@adapter = Null.new(serializer)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_serializable_hash
|
15
|
+
assert_equal({}, @adapter.serializable_hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_it_returns_empty_json
|
19
|
+
assert_equal('{}', @adapter.to_json)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class AdapterTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
profile = Profile.new
|
8
|
+
@serializer = ProfileSerializer.new(profile)
|
9
|
+
@adapter = ActiveModel::Serializer::Adapter.new(@serializer)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_serializable_hash_is_abstract_method
|
13
|
+
assert_raises(NotImplementedError) do
|
14
|
+
@adapter.serializable_hash(only: [:name])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_serializer
|
19
|
+
assert_equal @serializer, @adapter.serializer
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_adapter_class_for_known_adapter
|
23
|
+
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api)
|
24
|
+
assert_equal ActiveModel::Serializer::Adapter::JsonApi, klass
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_adapter_class_for_unknown_adapter
|
28
|
+
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_simple)
|
29
|
+
assert_nil klass
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_create_adapter
|
33
|
+
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
|
34
|
+
assert_equal ActiveModel::Serializer::Adapter::Json, adapter.class
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_create_adapter_with_override
|
38
|
+
adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api})
|
39
|
+
assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class ArraySerializerTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@comment = Comment.new
|
8
|
+
@post = Post.new
|
9
|
+
@serializer = ArraySerializer.new([@comment, @post], {some: :options})
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_respond_to_each
|
13
|
+
assert_respond_to @serializer, :each
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_each_object_should_be_serialized_with_appropriate_serializer
|
17
|
+
serializers = @serializer.to_a
|
18
|
+
|
19
|
+
assert_kind_of CommentSerializer, serializers.first
|
20
|
+
assert_kind_of Comment, serializers.first.object
|
21
|
+
|
22
|
+
assert_kind_of PostSerializer, serializers.last
|
23
|
+
assert_kind_of Post, serializers.last.object
|
24
|
+
|
25
|
+
assert_equal serializers.last.custom_options[:some], :options
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/test/fixtures/poro.rb
CHANGED
@@ -1,223 +1,206 @@
|
|
1
1
|
class Model
|
2
|
-
def initialize(hash
|
2
|
+
def initialize(hash={})
|
3
3
|
@attributes = hash
|
4
4
|
end
|
5
5
|
|
6
|
+
def cache_key
|
7
|
+
"#{self.class.name.downcase}/#{self.id}-#{self.updated_at}"
|
8
|
+
end
|
9
|
+
|
10
|
+
def updated_at
|
11
|
+
@attributes[:updated_at] ||= DateTime.now.to_time.to_i
|
12
|
+
end
|
13
|
+
|
6
14
|
def read_attribute_for_serialization(name)
|
7
15
|
if name == :id || name == 'id'
|
8
|
-
|
9
|
-
elsif respond_to?(name)
|
10
|
-
send name
|
16
|
+
id
|
11
17
|
else
|
12
18
|
@attributes[name]
|
13
19
|
end
|
14
20
|
end
|
15
|
-
end
|
16
21
|
|
22
|
+
def id
|
23
|
+
@attributes[:id] || @attributes['id'] || object_id
|
24
|
+
end
|
17
25
|
|
18
|
-
|
19
|
-
|
20
|
-
###
|
21
|
-
class User < Model
|
22
|
-
def profile
|
23
|
-
@profile ||= Profile.new(name: 'N1', description: 'D1')
|
26
|
+
def to_param
|
27
|
+
id
|
24
28
|
end
|
25
|
-
end
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
+
def method_missing(meth, *args)
|
31
|
+
if meth.to_s =~ /^(.*)=$/
|
32
|
+
@attributes[$1.to_sym] = args[0]
|
33
|
+
elsif @attributes.key?(meth)
|
34
|
+
@attributes[meth]
|
35
|
+
else
|
36
|
+
super
|
37
|
+
end
|
30
38
|
end
|
31
39
|
end
|
32
40
|
|
33
41
|
class Profile < Model
|
34
42
|
end
|
35
43
|
|
36
|
-
class
|
37
|
-
|
38
|
-
@posts ||= [Post.new(title: 'T1', body: 'B1'),
|
39
|
-
Post.new(title: 'T2', body: 'B2')]
|
40
|
-
end
|
41
|
-
end
|
44
|
+
class ProfileSerializer < ActiveModel::Serializer
|
45
|
+
attributes :name, :description
|
42
46
|
|
43
|
-
|
44
|
-
def comments
|
45
|
-
@comments ||= [Comment.new(content: 'C1'),
|
46
|
-
Comment.new(content: 'C2')]
|
47
|
-
end
|
48
|
-
end
|
47
|
+
urls :posts, :comments
|
49
48
|
|
50
|
-
|
51
|
-
|
52
|
-
@special_comment ||= Comment.new(content: 'special')
|
49
|
+
def arguments_passed_in?
|
50
|
+
options[:my_options] == :accessible
|
53
51
|
end
|
54
52
|
end
|
55
53
|
|
56
|
-
class
|
57
|
-
|
54
|
+
class ProfilePreviewSerializer < ActiveModel::Serializer
|
55
|
+
attributes :name
|
58
56
|
|
59
|
-
|
60
|
-
def type
|
61
|
-
@type ||= Type.new(name: 'N1')
|
62
|
-
end
|
63
|
-
def parent
|
64
|
-
@parent ||= SelfReferencingUserParent.new(name: 'N1')
|
65
|
-
end
|
57
|
+
urls :posts, :comments
|
66
58
|
end
|
67
59
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
60
|
+
Post = Class.new(Model)
|
61
|
+
Like = Class.new(Model)
|
62
|
+
Comment = Class.new(Model)
|
63
|
+
Author = Class.new(Model)
|
64
|
+
Bio = Class.new(Model)
|
65
|
+
Blog = Class.new(Model)
|
66
|
+
Role = Class.new(Model)
|
67
|
+
User = Class.new(Model)
|
68
|
+
Location = Class.new(Model)
|
69
|
+
Place = Class.new(Model)
|
75
70
|
|
76
|
-
|
77
|
-
|
71
|
+
module Spam; end
|
72
|
+
Spam::UnrelatedLink = Class.new(Model)
|
78
73
|
|
79
|
-
|
80
|
-
|
74
|
+
PostSerializer = Class.new(ActiveModel::Serializer) do
|
75
|
+
cache key:'post', expires_in: 0.1
|
76
|
+
attributes :id, :title, :body
|
77
|
+
|
78
|
+
has_many :comments
|
79
|
+
belongs_to :blog
|
80
|
+
belongs_to :author
|
81
|
+
url :comments
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
@attachment ||= Image.new(url: 'U1')
|
83
|
+
def blog
|
84
|
+
Blog.new(id: 999, name: "Custom blog")
|
85
85
|
end
|
86
|
-
end
|
87
86
|
|
88
|
-
|
89
|
-
|
90
|
-
@attachments ||= [Image.new(url: 'U1'),
|
91
|
-
Video.new(html: 'H1')]
|
87
|
+
def custom_options
|
88
|
+
options
|
92
89
|
end
|
93
90
|
end
|
94
91
|
|
95
|
-
|
96
|
-
|
92
|
+
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
|
93
|
+
attributes :id
|
94
|
+
has_many :related
|
97
95
|
|
98
|
-
|
96
|
+
def self.root_name
|
97
|
+
'posts'
|
98
|
+
end
|
99
99
|
end
|
100
100
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
class UserSerializer < ActiveModel::Serializer
|
105
|
-
attributes :name, :email
|
101
|
+
CommentSerializer = Class.new(ActiveModel::Serializer) do
|
102
|
+
cache expires_in: 1.day
|
103
|
+
attributes :id, :body
|
106
104
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
class TypeSerializer < ActiveModel::Serializer
|
111
|
-
attributes :name
|
112
|
-
end
|
105
|
+
belongs_to :post
|
106
|
+
belongs_to :author
|
113
107
|
|
114
|
-
|
115
|
-
|
116
|
-
|
108
|
+
def custom_options
|
109
|
+
options
|
110
|
+
end
|
117
111
|
end
|
118
112
|
|
119
|
-
|
120
|
-
|
113
|
+
AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
114
|
+
cache key:'writer'
|
115
|
+
attributes :id, :name
|
121
116
|
|
122
|
-
|
123
|
-
|
117
|
+
has_many :posts, embed: :ids
|
118
|
+
has_many :roles, embed: :ids
|
119
|
+
has_one :bio
|
124
120
|
end
|
125
121
|
|
126
|
-
|
127
|
-
|
128
|
-
|
122
|
+
RoleSerializer = Class.new(ActiveModel::Serializer) do
|
123
|
+
cache only: [:name]
|
124
|
+
attributes :id, :name, :description, :slug
|
129
125
|
|
130
|
-
|
131
|
-
|
132
|
-
description = object.read_attribute_for_serialization(:description)
|
133
|
-
scope ? "#{description} - #{scope}" : description
|
126
|
+
def slug
|
127
|
+
"#{name}-#{id}"
|
134
128
|
end
|
135
129
|
|
136
|
-
|
130
|
+
belongs_to :author
|
137
131
|
end
|
138
132
|
|
139
|
-
|
140
|
-
attributes :
|
133
|
+
LikeSerializer = Class.new(ActiveModel::Serializer) do
|
134
|
+
attributes :id, :time
|
141
135
|
|
142
|
-
|
136
|
+
belongs_to :post
|
143
137
|
end
|
144
138
|
|
145
|
-
|
146
|
-
|
139
|
+
LocationSerializer = Class.new(ActiveModel::Serializer) do
|
140
|
+
cache only: [:place]
|
141
|
+
attributes :id, :lat, :lng
|
147
142
|
|
148
|
-
|
149
|
-
keyword = serialization_options[:highlight_keyword]
|
150
|
-
title = object.read_attribute_for_serialization(:title)
|
151
|
-
title = title.gsub(keyword,"'#{keyword}'") if keyword
|
152
|
-
title
|
153
|
-
end
|
143
|
+
belongs_to :place
|
154
144
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
class SpecialPostSerializer < ActiveModel::Serializer
|
159
|
-
attributes :title, :body
|
160
|
-
has_many :comments, root: :comments, embed_in_root: true, embed: :ids
|
161
|
-
has_one :special_comment, root: :comments, embed_in_root: true, embed: :ids
|
145
|
+
def place
|
146
|
+
'Nowhere'
|
147
|
+
end
|
162
148
|
end
|
163
149
|
|
164
|
-
|
165
|
-
attributes :
|
166
|
-
end
|
150
|
+
PlaceSerializer = Class.new(ActiveModel::Serializer) do
|
151
|
+
attributes :id, :name
|
167
152
|
|
168
|
-
|
169
|
-
attributes :name, :display_name
|
153
|
+
has_many :locations
|
170
154
|
end
|
171
155
|
|
172
|
-
|
173
|
-
|
174
|
-
|
156
|
+
BioSerializer = Class.new(ActiveModel::Serializer) do
|
157
|
+
cache except: [:content]
|
158
|
+
attributes :id, :content, :rating
|
175
159
|
|
176
|
-
|
177
|
-
attributes :text
|
178
|
-
|
179
|
-
has_one :attachment, polymorphic: true
|
160
|
+
belongs_to :author
|
180
161
|
end
|
181
162
|
|
182
|
-
|
183
|
-
attributes :
|
163
|
+
BlogSerializer = Class.new(ActiveModel::Serializer) do
|
164
|
+
attributes :id, :name
|
184
165
|
|
185
|
-
|
166
|
+
belongs_to :writer
|
167
|
+
has_many :articles
|
186
168
|
end
|
187
169
|
|
188
|
-
|
189
|
-
|
170
|
+
PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do
|
171
|
+
def json_key
|
172
|
+
'paginated'
|
173
|
+
end
|
190
174
|
end
|
191
175
|
|
192
|
-
|
193
|
-
|
176
|
+
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
177
|
+
attribute :id
|
178
|
+
attribute :name, key: :title
|
194
179
|
end
|
195
180
|
|
196
|
-
|
197
|
-
|
198
|
-
module TestNamespace
|
199
|
-
class ProfileSerializer < ::ProfileSerializer; end
|
200
|
-
class UserSerializer < ::UserSerializer; end
|
201
|
-
end
|
181
|
+
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
182
|
+
attributes :id
|
202
183
|
|
203
|
-
|
204
|
-
config.default_key_type = :name
|
184
|
+
belongs_to :post
|
205
185
|
end
|
206
186
|
|
207
|
-
|
208
|
-
attributes :
|
187
|
+
AuthorPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
188
|
+
attributes :id
|
209
189
|
|
210
|
-
|
190
|
+
has_many :posts
|
211
191
|
end
|
212
192
|
|
213
|
-
|
214
|
-
|
193
|
+
PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
194
|
+
def self.root_name
|
195
|
+
'posts'
|
196
|
+
end
|
215
197
|
|
216
|
-
|
217
|
-
end
|
198
|
+
attributes :title, :body, :id
|
218
199
|
|
219
|
-
|
220
|
-
|
200
|
+
has_many :comments, serializer: CommentPreviewSerializer
|
201
|
+
belongs_to :author, serializer: AuthorPreviewSerializer
|
221
202
|
end
|
222
203
|
|
223
|
-
|
204
|
+
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
205
|
+
attributes :id
|
206
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Serializer
|
3
|
+
class AdapterForTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@previous_adapter = ActiveModel::Serializer.config.adapter
|
6
|
+
end
|
7
|
+
|
8
|
+
def teardown
|
9
|
+
ActiveModel::Serializer.config.adapter = @previous_adapter
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_returns_default_adapter
|
13
|
+
adapter = ActiveModel::Serializer.adapter
|
14
|
+
assert_equal ActiveModel::Serializer::Adapter::Json, adapter
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_overwrite_adapter_with_symbol
|
18
|
+
ActiveModel::Serializer.config.adapter = :null
|
19
|
+
|
20
|
+
adapter = ActiveModel::Serializer.adapter
|
21
|
+
assert_equal ActiveModel::Serializer::Adapter::Null, adapter
|
22
|
+
ensure
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_overwrite_adapter_with_class
|
27
|
+
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::Null
|
28
|
+
|
29
|
+
adapter = ActiveModel::Serializer.adapter
|
30
|
+
assert_equal ActiveModel::Serializer::Adapter::Null, adapter
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_raises_exception_if_invalid_symbol_given
|
34
|
+
ActiveModel::Serializer.config.adapter = :unknown
|
35
|
+
|
36
|
+
assert_raises ArgumentError do
|
37
|
+
ActiveModel::Serializer.adapter
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_raises_exception_if_it_does_not_know_hot_to_infer_adapter
|
42
|
+
ActiveModel::Serializer.config.adapter = 42
|
43
|
+
|
44
|
+
assert_raises ArgumentError do
|
45
|
+
ActiveModel::Serializer.adapter
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|