active_model_serializers 0.9.9 → 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 -204
- 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/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 -1
- data/lib/active_model/serializer.rb +192 -267
- 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 +123 -171
- 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 -6
- 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/utils.rb +0 -16
- data/lib/active_model/serializable.rb +0 -59
- 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/association.rb +0 -58
- 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 -26
- 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 -3
- data/test/tmp/app/helpers/accounts_helper.rb +0 -3
- data/test/tmp/app/serializers/account_serializer.rb +0 -4
- data/test/tmp/config/routes.rb +0 -2
- 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 -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,254 +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
|
|
17
|
-
|
18
|
-
|
19
|
-
## Models
|
20
|
-
###
|
21
|
-
|
22
|
-
module TestNamespace2
|
23
|
-
class Test < Model
|
24
|
-
attr_writer :sub_test
|
25
|
-
|
26
|
-
def sub_test
|
27
|
-
@sub_test ||= TestNamespace2::SubTest.new(name: 'N1', description: 'D1')
|
28
|
-
end
|
22
|
+
def id
|
23
|
+
@attributes[:id] || @attributes['id'] || object_id
|
29
24
|
end
|
30
25
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
class User < Model
|
35
|
-
def profile
|
36
|
-
@profile ||= Profile.new(name: 'N1', description: 'D1')
|
26
|
+
def to_param
|
27
|
+
id
|
37
28
|
end
|
38
|
-
end
|
39
29
|
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
43
38
|
end
|
44
39
|
end
|
45
40
|
|
46
41
|
class Profile < Model
|
47
42
|
end
|
48
43
|
|
49
|
-
class
|
50
|
-
|
51
|
-
@posts ||= [Post.new(title: 'T1', body: 'B1'),
|
52
|
-
Post.new(title: 'T2', body: 'B2')]
|
53
|
-
end
|
54
|
-
end
|
44
|
+
class ProfileSerializer < ActiveModel::Serializer
|
45
|
+
attributes :name, :description
|
55
46
|
|
56
|
-
|
57
|
-
def comments
|
58
|
-
@comments ||= [Comment.new(content: 'C1'),
|
59
|
-
Comment.new(content: 'C2')]
|
60
|
-
end
|
61
|
-
end
|
47
|
+
urls :posts, :comments
|
62
48
|
|
63
|
-
|
64
|
-
|
65
|
-
@special_comment ||= Comment.new(content: 'special')
|
49
|
+
def arguments_passed_in?
|
50
|
+
options[:my_options] == :accessible
|
66
51
|
end
|
67
52
|
end
|
68
53
|
|
69
|
-
class
|
70
|
-
|
54
|
+
class ProfilePreviewSerializer < ActiveModel::Serializer
|
55
|
+
attributes :name
|
71
56
|
|
72
|
-
|
73
|
-
def type
|
74
|
-
@type ||= Type.new(name: 'N1')
|
75
|
-
end
|
76
|
-
def parent
|
77
|
-
@parent ||= SelfReferencingUserParent.new(name: 'N1')
|
78
|
-
end
|
57
|
+
urls :posts, :comments
|
79
58
|
end
|
80
59
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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)
|
88
70
|
|
89
|
-
|
90
|
-
|
71
|
+
module Spam; end
|
72
|
+
Spam::UnrelatedLink = Class.new(Model)
|
91
73
|
|
92
|
-
|
93
|
-
|
74
|
+
PostSerializer = Class.new(ActiveModel::Serializer) do
|
75
|
+
cache key:'post', expires_in: 0.1
|
76
|
+
attributes :id, :title, :body
|
94
77
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
end
|
78
|
+
has_many :comments
|
79
|
+
belongs_to :blog
|
80
|
+
belongs_to :author
|
81
|
+
url :comments
|
100
82
|
|
101
|
-
|
102
|
-
|
103
|
-
@attachments ||= [Image.new(url: 'U1'),
|
104
|
-
Video.new(html: 'H1')]
|
83
|
+
def blog
|
84
|
+
Blog.new(id: 999, name: "Custom blog")
|
105
85
|
end
|
106
|
-
end
|
107
|
-
|
108
|
-
class Image < Model
|
109
|
-
end
|
110
86
|
|
111
|
-
|
87
|
+
def custom_options
|
88
|
+
options
|
89
|
+
end
|
112
90
|
end
|
113
91
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
module TestNamespace2
|
119
|
-
class TestSerializer < ActiveModel::Serializer
|
120
|
-
attributes :name, :email
|
92
|
+
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
|
93
|
+
attributes :id
|
94
|
+
has_many :related
|
121
95
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
class SubTestSerializer < ActiveModel::Serializer
|
126
|
-
def description
|
127
|
-
description = object.read_attribute_for_serialization(:description)
|
128
|
-
scope ? "#{description} - #{scope}" : description
|
129
|
-
end
|
130
|
-
|
131
|
-
attributes :name, :description
|
96
|
+
def self.root_name
|
97
|
+
'posts'
|
132
98
|
end
|
133
99
|
end
|
134
100
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
has_one :profile
|
139
|
-
end
|
101
|
+
CommentSerializer = Class.new(ActiveModel::Serializer) do
|
102
|
+
cache expires_in: 1.day
|
103
|
+
attributes :id, :body
|
140
104
|
|
141
|
-
|
142
|
-
|
143
|
-
end
|
105
|
+
belongs_to :post
|
106
|
+
belongs_to :author
|
144
107
|
|
145
|
-
|
146
|
-
|
147
|
-
|
108
|
+
def custom_options
|
109
|
+
options
|
110
|
+
end
|
148
111
|
end
|
149
112
|
|
150
|
-
|
151
|
-
|
113
|
+
AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
114
|
+
cache key:'writer'
|
115
|
+
attributes :id, :name
|
152
116
|
|
153
|
-
|
154
|
-
|
117
|
+
has_many :posts, embed: :ids
|
118
|
+
has_many :roles, embed: :ids
|
119
|
+
has_one :bio
|
155
120
|
end
|
156
121
|
|
157
|
-
|
158
|
-
|
159
|
-
|
122
|
+
RoleSerializer = Class.new(ActiveModel::Serializer) do
|
123
|
+
cache only: [:name]
|
124
|
+
attributes :id, :name, :description, :slug
|
160
125
|
|
161
|
-
|
162
|
-
|
163
|
-
description = object.read_attribute_for_serialization(:description)
|
164
|
-
scope ? "#{description} - #{scope}" : description
|
126
|
+
def slug
|
127
|
+
"#{name}-#{id}"
|
165
128
|
end
|
166
129
|
|
167
|
-
|
130
|
+
belongs_to :author
|
168
131
|
end
|
169
132
|
|
170
|
-
|
171
|
-
attributes :
|
133
|
+
LikeSerializer = Class.new(ActiveModel::Serializer) do
|
134
|
+
attributes :id, :time
|
172
135
|
|
173
|
-
|
136
|
+
belongs_to :post
|
174
137
|
end
|
175
138
|
|
176
|
-
|
177
|
-
|
139
|
+
LocationSerializer = Class.new(ActiveModel::Serializer) do
|
140
|
+
cache only: [:place]
|
141
|
+
attributes :id, :lat, :lng
|
178
142
|
|
179
|
-
|
180
|
-
keyword = serialization_options[:highlight_keyword]
|
181
|
-
title = object.read_attribute_for_serialization(:title)
|
182
|
-
title = title.gsub(keyword,"'#{keyword}'") if keyword
|
183
|
-
title
|
184
|
-
end
|
143
|
+
belongs_to :place
|
185
144
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
class SpecialPostSerializer < ActiveModel::Serializer
|
190
|
-
attributes :title, :body
|
191
|
-
has_many :comments, root: :comments, embed_in_root: true, embed: :ids
|
192
|
-
has_one :special_comment, root: :comments, embed_in_root: true, embed: :ids
|
193
|
-
end
|
194
|
-
|
195
|
-
class CommentSerializer < ActiveModel::Serializer
|
196
|
-
attributes :content
|
145
|
+
def place
|
146
|
+
'Nowhere'
|
147
|
+
end
|
197
148
|
end
|
198
149
|
|
199
|
-
|
200
|
-
attributes :
|
201
|
-
end
|
150
|
+
PlaceSerializer = Class.new(ActiveModel::Serializer) do
|
151
|
+
attributes :id, :name
|
202
152
|
|
203
|
-
|
204
|
-
format_keys :lower_camel
|
153
|
+
has_many :locations
|
205
154
|
end
|
206
155
|
|
207
|
-
|
208
|
-
|
156
|
+
BioSerializer = Class.new(ActiveModel::Serializer) do
|
157
|
+
cache except: [:content]
|
158
|
+
attributes :id, :content, :rating
|
209
159
|
|
210
|
-
|
160
|
+
belongs_to :author
|
211
161
|
end
|
212
162
|
|
213
|
-
|
214
|
-
attributes :
|
163
|
+
BlogSerializer = Class.new(ActiveModel::Serializer) do
|
164
|
+
attributes :id, :name
|
215
165
|
|
216
|
-
|
166
|
+
belongs_to :writer
|
167
|
+
has_many :articles
|
217
168
|
end
|
218
169
|
|
219
|
-
|
220
|
-
|
170
|
+
PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do
|
171
|
+
def json_key
|
172
|
+
'paginated'
|
173
|
+
end
|
221
174
|
end
|
222
175
|
|
223
|
-
|
224
|
-
|
176
|
+
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
177
|
+
attribute :id
|
178
|
+
attribute :name, key: :title
|
225
179
|
end
|
226
180
|
|
227
|
-
|
181
|
+
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
182
|
+
attributes :id
|
228
183
|
|
229
|
-
|
230
|
-
class ProfileSerializer < ::ProfileSerializer; end
|
231
|
-
class UserSerializer < ::UserSerializer; end
|
184
|
+
belongs_to :post
|
232
185
|
end
|
233
186
|
|
234
|
-
ActiveModel::Serializer
|
235
|
-
|
236
|
-
end
|
237
|
-
|
238
|
-
class NameKeyUserSerializer < ActiveModel::Serializer
|
239
|
-
attributes :name, :email
|
187
|
+
AuthorPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
188
|
+
attributes :id
|
240
189
|
|
241
|
-
|
190
|
+
has_many :posts
|
242
191
|
end
|
243
192
|
|
244
|
-
|
245
|
-
|
193
|
+
PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
194
|
+
def self.root_name
|
195
|
+
'posts'
|
196
|
+
end
|
197
|
+
|
198
|
+
attributes :title, :body, :id
|
246
199
|
|
247
|
-
has_many :comments
|
200
|
+
has_many :comments, serializer: CommentPreviewSerializer
|
201
|
+
belongs_to :author, serializer: AuthorPreviewSerializer
|
248
202
|
end
|
249
203
|
|
250
|
-
ActiveModel::Serializer
|
251
|
-
|
204
|
+
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
205
|
+
attributes :id
|
252
206
|
end
|
253
|
-
|
254
|
-
|
@@ -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
|
@@ -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
|