cheap_ams 0.10.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +26 -0
- data/CHANGELOG.md +13 -0
- data/CONTRIBUTING.md +31 -0
- data/Gemfile +35 -0
- data/LICENSE.txt +22 -0
- data/README.md +348 -0
- data/Rakefile +12 -0
- data/appveyor.yml +25 -0
- data/cheap_ams.gemspec +49 -0
- data/docs/README.md +27 -0
- data/docs/general/adapters.md +51 -0
- data/docs/general/getting_started.md +73 -0
- data/docs/howto/add_pagination_links.md +112 -0
- data/docs/howto/add_root_key.md +51 -0
- data/lib/action_controller/serialization.rb +62 -0
- data/lib/active_model/serializable_resource.rb +84 -0
- data/lib/active_model/serializer/adapter/flatten_json.rb +19 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +82 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +16 -0
- data/lib/active_model/serializer/adapter/json.rb +53 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +24 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
- data/lib/active_model/serializer/adapter/json_api.rb +183 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +98 -0
- data/lib/active_model/serializer/array_serializer.rb +35 -0
- data/lib/active_model/serializer/association.rb +21 -0
- data/lib/active_model/serializer/associations.rb +97 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/configuration.rb +14 -0
- data/lib/active_model/serializer/fieldset.rb +42 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +10 -0
- data/lib/active_model/serializer/lint.rb +131 -0
- data/lib/active_model/serializer/railtie.rb +9 -0
- data/lib/active_model/serializer/reflection.rb +74 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/version.rb +5 -0
- data/lib/active_model/serializer.rb +205 -0
- data/lib/active_model_serializers.rb +29 -0
- data/lib/generators/serializer/USAGE +6 -0
- data/lib/generators/serializer/resource_override.rb +12 -0
- data/lib/generators/serializer/serializer_generator.rb +37 -0
- data/lib/generators/serializer/templates/serializer.rb +8 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json_api/linked_test.rb +180 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/action_controller/serialization_scope_name_test.rb +67 -0
- data/test/action_controller/serialization_test.rb +426 -0
- data/test/adapter/fragment_cache_test.rb +37 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +82 -0
- data/test/adapter/json/has_many_test.rb +47 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +96 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +145 -0
- data/test/adapter/json_api/has_one_test.rb +81 -0
- data/test/adapter/json_api/json_api_test.rb +38 -0
- data/test/adapter/json_api/linked_test.rb +283 -0
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/resource_type_config_test.rb +59 -0
- data/test/adapter/json_test.rb +47 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +52 -0
- data/test/array_serializer_test.rb +97 -0
- data/test/capture_warnings.rb +57 -0
- data/test/fixtures/active_record.rb +57 -0
- data/test/fixtures/poro.rb +266 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +56 -0
- data/test/lint_test.rb +44 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +27 -0
- data/test/serializers/adapter_for_test.rb +50 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +150 -0
- data/test/serializers/attribute_test.rb +62 -0
- data/test/serializers/attributes_test.rb +63 -0
- data/test/serializers/cache_test.rb +164 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +26 -0
- data/test/serializers/meta_test.rb +121 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/root_test.rb +23 -0
- data/test/serializers/serializer_for_test.rb +65 -0
- data/test/serializers/urls_test.rb +26 -0
- data/test/support/rails_app.rb +21 -0
- data/test/support/stream_capture.rb +49 -0
- data/test/support/test_case.rb +5 -0
- data/test/test_helper.rb +41 -0
- metadata +287 -0
@@ -0,0 +1,57 @@
|
|
1
|
+
# https://raw.githubusercontent.com/metric_fu/metric_fu/master/spec/capture_warnings.rb
|
2
|
+
require "tempfile"
|
3
|
+
require "fileutils"
|
4
|
+
|
5
|
+
class CaptureWarnings
|
6
|
+
def initialize(fail_on_warnings = true)
|
7
|
+
@fail_on_warnings = fail_on_warnings
|
8
|
+
@stderr_file = Tempfile.new("app.stderr")
|
9
|
+
@app_root ||= Dir.pwd
|
10
|
+
@output_dir = File.join(app_root, "tmp")
|
11
|
+
FileUtils.mkdir_p(output_dir)
|
12
|
+
@bundle_dir = File.join(app_root, "bundle")
|
13
|
+
end
|
14
|
+
|
15
|
+
def before_tests
|
16
|
+
$stderr.reopen(stderr_file.path)
|
17
|
+
$VERBOSE = true
|
18
|
+
at_exit { $stderr.reopen(STDERR) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def after_tests
|
22
|
+
stderr_file.rewind
|
23
|
+
lines = stderr_file.read.split("\n")
|
24
|
+
stderr_file.close!
|
25
|
+
|
26
|
+
$stderr.reopen(STDERR)
|
27
|
+
|
28
|
+
app_warnings, other_warnings = lines.partition { |line|
|
29
|
+
line.include?(app_root) && !line.include?(bundle_dir)
|
30
|
+
}
|
31
|
+
|
32
|
+
if app_warnings.any?
|
33
|
+
puts <<-WARNINGS
|
34
|
+
#{'-' * 30} app warnings: #{'-' * 30}
|
35
|
+
|
36
|
+
#{app_warnings.join("\n")}
|
37
|
+
|
38
|
+
#{'-' * 75}
|
39
|
+
WARNINGS
|
40
|
+
end
|
41
|
+
|
42
|
+
if other_warnings.any?
|
43
|
+
File.write(File.join(output_dir, "warnings.txt"), other_warnings.join("\n") << "\n")
|
44
|
+
puts
|
45
|
+
puts "Non-app warnings written to tmp/warnings.txt"
|
46
|
+
puts
|
47
|
+
end
|
48
|
+
|
49
|
+
# fail the build...
|
50
|
+
if fail_on_warnings && app_warnings.any?
|
51
|
+
abort "Failing build due to app warnings: #{app_warnings.inspect}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
attr_reader :stderr_file, :app_root, :output_dir, :bundle_dir, :fail_on_warnings
|
57
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
4
|
+
ActiveRecord::Schema.define do
|
5
|
+
create_table :posts, force: true do |t|
|
6
|
+
t.string :title
|
7
|
+
t.text :body
|
8
|
+
t.references :author
|
9
|
+
t.timestamps null: false
|
10
|
+
end
|
11
|
+
create_table :authors, force: true do |t|
|
12
|
+
t.string :name
|
13
|
+
t.timestamps null: false
|
14
|
+
end
|
15
|
+
create_table :comments, force: true do |t|
|
16
|
+
t.text :contents
|
17
|
+
t.references :author
|
18
|
+
t.references :post
|
19
|
+
t.timestamp null: false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ARModels
|
24
|
+
class Post < ActiveRecord::Base
|
25
|
+
has_many :comments
|
26
|
+
belongs_to :author
|
27
|
+
end
|
28
|
+
|
29
|
+
class Comment < ActiveRecord::Base
|
30
|
+
belongs_to :post
|
31
|
+
belongs_to :author
|
32
|
+
end
|
33
|
+
|
34
|
+
class Author < ActiveRecord::Base
|
35
|
+
has_many :posts
|
36
|
+
end
|
37
|
+
|
38
|
+
class PostSerializer < ActiveModel::Serializer
|
39
|
+
attributes :id, :title, :body
|
40
|
+
|
41
|
+
has_many :comments
|
42
|
+
belongs_to :author
|
43
|
+
url :comments
|
44
|
+
end
|
45
|
+
|
46
|
+
class CommentSerializer < ActiveModel::Serializer
|
47
|
+
attributes :id, :contents
|
48
|
+
|
49
|
+
belongs_to :author
|
50
|
+
end
|
51
|
+
|
52
|
+
class AuthorSerializer < ActiveModel::Serializer
|
53
|
+
attributes :id, :name
|
54
|
+
|
55
|
+
has_many :posts
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,266 @@
|
|
1
|
+
verbose = $VERBOSE
|
2
|
+
$VERBOSE = nil
|
3
|
+
class Model
|
4
|
+
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
|
5
|
+
|
6
|
+
def self.model_name
|
7
|
+
@_model_name ||= ActiveModel::Name.new(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(hash={})
|
11
|
+
@attributes = hash
|
12
|
+
end
|
13
|
+
|
14
|
+
def cache_key
|
15
|
+
"#{self.class.name.downcase}/#{self.id}-#{self.updated_at.strftime("%Y%m%d%H%M%S%9N")}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def serializable_hash(options = nil)
|
19
|
+
@attributes
|
20
|
+
end
|
21
|
+
|
22
|
+
def read_attribute_for_serialization(name)
|
23
|
+
if name == :id || name == 'id'
|
24
|
+
id
|
25
|
+
else
|
26
|
+
@attributes[name]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def id
|
31
|
+
@attributes[:id] || @attributes['id'] || object_id
|
32
|
+
end
|
33
|
+
|
34
|
+
### Helper methods, not required to be serializable
|
35
|
+
#
|
36
|
+
# Convenience for adding @attributes readers and writers
|
37
|
+
def method_missing(meth, *args)
|
38
|
+
if meth.to_s =~ /^(.*)=$/
|
39
|
+
@attributes[$1.to_sym] = args[0]
|
40
|
+
elsif @attributes.key?(meth)
|
41
|
+
@attributes[meth]
|
42
|
+
else
|
43
|
+
super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def cache_key_with_digest
|
48
|
+
"#{cache_key}/#{FILE_DIGEST}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def updated_at
|
52
|
+
@attributes[:updated_at] ||= DateTime.now.to_time
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Profile < Model
|
57
|
+
end
|
58
|
+
|
59
|
+
class ProfileSerializer < ActiveModel::Serializer
|
60
|
+
attributes :name, :description
|
61
|
+
|
62
|
+
urls :posts, :comments
|
63
|
+
|
64
|
+
def arguments_passed_in?
|
65
|
+
options[:my_options] == :accessible
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class ProfilePreviewSerializer < ActiveModel::Serializer
|
70
|
+
attributes :name
|
71
|
+
|
72
|
+
urls :posts, :comments
|
73
|
+
end
|
74
|
+
|
75
|
+
Post = Class.new(Model)
|
76
|
+
Like = Class.new(Model)
|
77
|
+
Author = Class.new(Model)
|
78
|
+
Bio = Class.new(Model)
|
79
|
+
Blog = Class.new(Model)
|
80
|
+
Role = Class.new(Model)
|
81
|
+
User = Class.new(Model)
|
82
|
+
Location = Class.new(Model)
|
83
|
+
Place = Class.new(Model)
|
84
|
+
Tag = Class.new(Model)
|
85
|
+
VirtualValue = Class.new(Model)
|
86
|
+
Comment = Class.new(Model) do
|
87
|
+
# Uses a custom non-time-based cache key
|
88
|
+
def cache_key
|
89
|
+
"#{self.class.name.downcase}/#{self.id}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
module Spam; end
|
94
|
+
Spam::UnrelatedLink = Class.new(Model)
|
95
|
+
|
96
|
+
PostSerializer = Class.new(ActiveModel::Serializer) do
|
97
|
+
cache key:'post', expires_in: 0.1, skip_digest: true
|
98
|
+
attributes :id, :title, :body
|
99
|
+
|
100
|
+
has_many :comments
|
101
|
+
belongs_to :blog
|
102
|
+
belongs_to :author
|
103
|
+
url :comments
|
104
|
+
|
105
|
+
def blog
|
106
|
+
Blog.new(id: 999, name: "Custom blog")
|
107
|
+
end
|
108
|
+
|
109
|
+
def custom_options
|
110
|
+
options
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
|
115
|
+
attributes :id
|
116
|
+
has_many :related
|
117
|
+
|
118
|
+
def self.root_name
|
119
|
+
'posts'
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
CommentSerializer = Class.new(ActiveModel::Serializer) do
|
124
|
+
cache expires_in: 1.day, skip_digest: true
|
125
|
+
attributes :id, :body
|
126
|
+
|
127
|
+
belongs_to :post
|
128
|
+
belongs_to :author
|
129
|
+
|
130
|
+
def custom_options
|
131
|
+
options
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
136
|
+
cache key:'writer', skip_digest: true
|
137
|
+
attribute :id
|
138
|
+
attribute :name
|
139
|
+
|
140
|
+
has_many :posts
|
141
|
+
has_many :roles
|
142
|
+
has_one :bio
|
143
|
+
end
|
144
|
+
|
145
|
+
RoleSerializer = Class.new(ActiveModel::Serializer) do
|
146
|
+
cache only: [:name], skip_digest: true
|
147
|
+
attributes :id, :name, :description, :slug
|
148
|
+
|
149
|
+
def slug
|
150
|
+
"#{name}-#{id}"
|
151
|
+
end
|
152
|
+
|
153
|
+
belongs_to :author
|
154
|
+
end
|
155
|
+
|
156
|
+
LikeSerializer = Class.new(ActiveModel::Serializer) do
|
157
|
+
attributes :id, :time
|
158
|
+
|
159
|
+
belongs_to :likeable
|
160
|
+
end
|
161
|
+
|
162
|
+
LocationSerializer = Class.new(ActiveModel::Serializer) do
|
163
|
+
cache only: [:place], skip_digest: true
|
164
|
+
attributes :id, :lat, :lng
|
165
|
+
|
166
|
+
belongs_to :place
|
167
|
+
|
168
|
+
def place
|
169
|
+
'Nowhere'
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
PlaceSerializer = Class.new(ActiveModel::Serializer) do
|
174
|
+
attributes :id, :name
|
175
|
+
|
176
|
+
has_many :locations
|
177
|
+
end
|
178
|
+
|
179
|
+
BioSerializer = Class.new(ActiveModel::Serializer) do
|
180
|
+
cache except: [:content], skip_digest: true
|
181
|
+
attributes :id, :content, :rating
|
182
|
+
|
183
|
+
belongs_to :author
|
184
|
+
end
|
185
|
+
|
186
|
+
BlogSerializer = Class.new(ActiveModel::Serializer) do
|
187
|
+
cache key: 'blog'
|
188
|
+
attributes :id, :name
|
189
|
+
|
190
|
+
belongs_to :writer
|
191
|
+
has_many :articles
|
192
|
+
end
|
193
|
+
|
194
|
+
PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do
|
195
|
+
def json_key
|
196
|
+
'paginated'
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
201
|
+
attribute :id
|
202
|
+
attribute :name, key: :title
|
203
|
+
end
|
204
|
+
|
205
|
+
CustomBlogSerializer = Class.new(ActiveModel::Serializer) do
|
206
|
+
attribute :id
|
207
|
+
attribute :special_attribute
|
208
|
+
|
209
|
+
has_many :articles
|
210
|
+
end
|
211
|
+
|
212
|
+
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
213
|
+
attributes :id
|
214
|
+
|
215
|
+
belongs_to :post
|
216
|
+
end
|
217
|
+
|
218
|
+
AuthorPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
219
|
+
attributes :id
|
220
|
+
|
221
|
+
has_many :posts
|
222
|
+
end
|
223
|
+
|
224
|
+
PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
225
|
+
def self.root_name
|
226
|
+
'posts'
|
227
|
+
end
|
228
|
+
|
229
|
+
attributes :title, :body, :id
|
230
|
+
|
231
|
+
has_many :comments, serializer: CommentPreviewSerializer
|
232
|
+
belongs_to :author, serializer: AuthorPreviewSerializer
|
233
|
+
end
|
234
|
+
|
235
|
+
PostWithTagsSerializer = Class.new(ActiveModel::Serializer) do
|
236
|
+
attributes :id
|
237
|
+
|
238
|
+
has_many :tags
|
239
|
+
end
|
240
|
+
|
241
|
+
PostWithCustomKeysSerializer = Class.new(ActiveModel::Serializer) do
|
242
|
+
attributes :id
|
243
|
+
|
244
|
+
has_many :comments, key: :reviews
|
245
|
+
belongs_to :author, key: :writer
|
246
|
+
has_one :blog, key: :site
|
247
|
+
end
|
248
|
+
|
249
|
+
VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
|
250
|
+
attributes :id
|
251
|
+
|
252
|
+
has_many :reviews, virtual_value: [{id: 1}, {id: 2}]
|
253
|
+
has_one :maker, virtual_value: {id: 1}
|
254
|
+
|
255
|
+
def reviews
|
256
|
+
end
|
257
|
+
|
258
|
+
def maker
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
263
|
+
cache only: [:id]
|
264
|
+
attributes :id
|
265
|
+
end
|
266
|
+
$VERBOSE = verbose
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
# require 'active_model/serializer/railtie'
|
3
|
+
|
4
|
+
class ResourceGeneratorTest < Rails::Generators::TestCase
|
5
|
+
destination File.expand_path('../../../tmp/generators', __FILE__)
|
6
|
+
setup :prepare_destination, :copy_routes
|
7
|
+
|
8
|
+
tests Rails::Generators::ResourceGenerator
|
9
|
+
arguments %w(account)
|
10
|
+
|
11
|
+
def test_serializer_file_is_generated
|
12
|
+
run_generator
|
13
|
+
|
14
|
+
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def copy_routes
|
20
|
+
config_dir = File.join(destination_root, 'config')
|
21
|
+
FileUtils.mkdir_p(config_dir)
|
22
|
+
File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw { }')
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'generators/serializer/serializer_generator'
|
3
|
+
|
4
|
+
class SerializerGeneratorTest < Rails::Generators::TestCase
|
5
|
+
destination File.expand_path("../../../tmp/generators", __FILE__)
|
6
|
+
setup :prepare_destination
|
7
|
+
|
8
|
+
tests Rails::Generators::SerializerGenerator
|
9
|
+
arguments %w(account name:string description:text business:references)
|
10
|
+
|
11
|
+
def test_generates_a_serializer
|
12
|
+
run_generator
|
13
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_generates_a_namespaced_serializer
|
17
|
+
run_generator ["admin/account"]
|
18
|
+
assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_uses_application_serializer_if_one_exists
|
22
|
+
Object.const_set(:ApplicationSerializer, Class.new)
|
23
|
+
run_generator
|
24
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/
|
25
|
+
ensure
|
26
|
+
Object.send :remove_const, :ApplicationSerializer
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_uses_given_parent
|
30
|
+
Object.const_set(:ApplicationSerializer, Class.new)
|
31
|
+
run_generator ["Account", "--parent=MySerializer"]
|
32
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < MySerializer/
|
33
|
+
ensure
|
34
|
+
Object.send :remove_const, :ApplicationSerializer
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_generates_attributes_and_associations
|
38
|
+
run_generator
|
39
|
+
assert_file "app/serializers/account_serializer.rb" do |serializer|
|
40
|
+
assert_match(/^ attributes :id, :name, :description$/, serializer)
|
41
|
+
assert_match(/^ has_one :business$/, serializer)
|
42
|
+
assert_match(/^end\n*\z/, serializer)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_with_no_attributes_does_not_add_extra_space
|
47
|
+
run_generator ["account"]
|
48
|
+
assert_file "app/serializers/account_serializer.rb" do |content|
|
49
|
+
if RUBY_PLATFORM =~ /mingw/
|
50
|
+
assert_no_match(/\r\n\r\nend/, content)
|
51
|
+
else
|
52
|
+
assert_no_match(/\n\nend/, content)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/test/lint_test.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class LintTest < Minitest::Test
|
6
|
+
include ActiveModel::Serializer::Lint::Tests
|
7
|
+
|
8
|
+
class CompliantResource
|
9
|
+
def serializable_hash(options = nil)
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def read_attribute_for_serialization(name)
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
def as_json(options = nil)
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_json(options = nil)
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def cache_key
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
def id
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.model_name
|
34
|
+
@_model_name ||= ActiveModel::Name.new(self)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def setup
|
39
|
+
@resource = CompliantResource.new
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/test/poro_test.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class SerializableResourceTest < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
7
|
+
@serializer = ProfileSerializer.new(@resource)
|
8
|
+
@adapter = ActiveModel::Serializer::Adapter.create(@serializer)
|
9
|
+
@serializable_resource = ActiveModel::SerializableResource.new(@resource)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_serializable_resource_delegates_serializable_hash_to_the_adapter
|
13
|
+
options = nil
|
14
|
+
assert_equal @adapter.serializable_hash(options), @serializable_resource.serializable_hash(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_serializable_resource_delegates_to_json_to_the_adapter
|
18
|
+
options = nil
|
19
|
+
assert_equal @adapter.to_json(options), @serializable_resource.to_json(options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_serializable_resource_delegates_as_json_to_the_adapter
|
23
|
+
options = nil
|
24
|
+
assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,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::FlattenJson, 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,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class AssociationMacrosTest < Minitest::Test
|
6
|
+
AuthorSummarySerializer = Class.new
|
7
|
+
class AssociationsTestSerializer < Serializer
|
8
|
+
belongs_to :author, serializer: AuthorSummarySerializer
|
9
|
+
has_many :comments
|
10
|
+
has_one :category
|
11
|
+
end
|
12
|
+
|
13
|
+
def before_setup
|
14
|
+
@reflections = AssociationsTestSerializer._reflections
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_has_one_defines_reflection
|
18
|
+
has_one_reflection = HasOneReflection.new(:category, {})
|
19
|
+
|
20
|
+
assert_includes(@reflections, has_one_reflection)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_has_many_defines_reflection
|
24
|
+
has_many_reflection = HasManyReflection.new(:comments, {})
|
25
|
+
|
26
|
+
assert_includes(@reflections, has_many_reflection)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_belongs_to_defines_reflection
|
30
|
+
belongs_to_reflection = BelongsToReflection.new(:author, serializer: AuthorSummarySerializer)
|
31
|
+
|
32
|
+
assert_includes(@reflections, belongs_to_reflection)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|