rbs_rails 0.3.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +18 -0
- data/.gitignore +3 -0
- data/.gitmodules +0 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +3 -2
- data/README.md +23 -43
- data/Rakefile +11 -2
- data/Steepfile +10 -1
- data/assets/sig/pg.rbs +5 -0
- data/assets/sig/que.rbs +4 -0
- data/assets/sig/queue_classic.rbs +4 -0
- data/assets/sig/rack.rbs +1 -0
- data/assets/sig/rails.rbs +1 -5
- data/assets/sig/sidekiq.rbs +4 -0
- data/assets/sig/sneakers.rbs +4 -0
- data/assets/sig/sucker_punch.rbs +4 -0
- data/bin/add-type-params.rb +7 -0
- data/bin/gem_rbs +94 -0
- data/bin/postprocess.rb +15 -6
- data/bin/rbs +29 -2
- data/bin/rbs-prototype-rb.rb +59 -6
- data/lib/rbs_rails.rb +4 -0
- data/lib/rbs_rails/active_record.rb +100 -50
- data/lib/rbs_rails/dependency_builder.rb +43 -0
- data/lib/rbs_rails/rake_task.rb +83 -0
- data/lib/rbs_rails/util.rb +25 -0
- data/lib/rbs_rails/version.rb +1 -1
- data/rbs_rails.gemspec +2 -1
- data/sig/fileutils.rbs +1 -0
- data/sig/rake.rbs +6 -0
- data/sig/rbs_rails/active_record.rbs +8 -2
- data/sig/rbs_rails/dependency_builder.rbs +9 -0
- data/sig/rbs_rails/rake_task.rbs +26 -0
- data/sig/rbs_rails/util.rbs +11 -0
- metadata +34 -13
- data/.travis.yml +0 -8
- data/assets/sig/action_controller.rbs +0 -49
- data/assets/sig/active_record.rbs +0 -137
- data/assets/sig/generated/actionpack.rbs +0 -11677
- data/assets/sig/generated/actionview.rbs +0 -10491
- data/assets/sig/generated/activemodel.rbs +0 -4139
- data/assets/sig/generated/activerecord-meta-programming.rbs +0 -98
- data/assets/sig/generated/activerecord.rbs +0 -24023
- data/assets/sig/generated/activesupport.rbs +0 -12207
- data/assets/sig/generated/railties.rbs +0 -4647
@@ -1,4139 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
module AttributeAssignment
|
3
|
-
include ActiveModel::ForbiddenAttributesProtection
|
4
|
-
|
5
|
-
# Allows you to set all the attributes by passing in a hash of attributes with
|
6
|
-
# keys matching the attribute names.
|
7
|
-
#
|
8
|
-
# If the passed hash responds to <tt>permitted?</tt> method and the return value
|
9
|
-
# of this method is +false+ an <tt>ActiveModel::ForbiddenAttributesError</tt>
|
10
|
-
# exception is raised.
|
11
|
-
#
|
12
|
-
# class Cat
|
13
|
-
# include ActiveModel::AttributeAssignment
|
14
|
-
# attr_accessor :name, :status
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# cat = Cat.new
|
18
|
-
# cat.assign_attributes(name: "Gorby", status: "yawning")
|
19
|
-
# cat.name # => 'Gorby'
|
20
|
-
# cat.status # => 'yawning'
|
21
|
-
# cat.assign_attributes(status: "sleeping")
|
22
|
-
# cat.name # => 'Gorby'
|
23
|
-
# cat.status # => 'sleeping'
|
24
|
-
def assign_attributes: (untyped new_attributes) -> (nil | untyped)
|
25
|
-
|
26
|
-
def _assign_attributes: (untyped attributes) -> untyped
|
27
|
-
|
28
|
-
def _assign_attribute: (untyped k, untyped v) -> untyped
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
module ActiveModel
|
33
|
-
# Raised when an attribute is not defined.
|
34
|
-
#
|
35
|
-
# class User < ActiveRecord::Base
|
36
|
-
# has_many :pets
|
37
|
-
# end
|
38
|
-
#
|
39
|
-
# user = User.first
|
40
|
-
# user.pets.select(:id).first.user_id
|
41
|
-
# # => ActiveModel::MissingAttributeError: missing attribute: user_id
|
42
|
-
class MissingAttributeError[T] < NoMethodError[T]
|
43
|
-
end
|
44
|
-
|
45
|
-
# == Active \Model \Attribute \Methods
|
46
|
-
#
|
47
|
-
# Provides a way to add prefixes and suffixes to your methods as
|
48
|
-
# well as handling the creation of <tt>ActiveRecord::Base</tt>-like
|
49
|
-
# class methods such as +table_name+.
|
50
|
-
#
|
51
|
-
# The requirements to implement <tt>ActiveModel::AttributeMethods</tt> are to:
|
52
|
-
#
|
53
|
-
# * <tt>include ActiveModel::AttributeMethods</tt> in your class.
|
54
|
-
# * Call each of its methods you want to add, such as +attribute_method_suffix+
|
55
|
-
# or +attribute_method_prefix+.
|
56
|
-
# * Call +define_attribute_methods+ after the other methods are called.
|
57
|
-
# * Define the various generic +_attribute+ methods that you have declared.
|
58
|
-
# * Define an +attributes+ method which returns a hash with each
|
59
|
-
# attribute name in your model as hash key and the attribute value as hash value.
|
60
|
-
# Hash keys must be strings.
|
61
|
-
#
|
62
|
-
# A minimal implementation could be:
|
63
|
-
#
|
64
|
-
# class Person
|
65
|
-
# include ActiveModel::AttributeMethods
|
66
|
-
#
|
67
|
-
# attribute_method_affix prefix: 'reset_', suffix: '_to_default!'
|
68
|
-
# attribute_method_suffix '_contrived?'
|
69
|
-
# attribute_method_prefix 'clear_'
|
70
|
-
# define_attribute_methods :name
|
71
|
-
#
|
72
|
-
# attr_accessor :name
|
73
|
-
#
|
74
|
-
# def attributes
|
75
|
-
# { 'name' => @name }
|
76
|
-
# end
|
77
|
-
#
|
78
|
-
# private
|
79
|
-
#
|
80
|
-
# def attribute_contrived?(attr)
|
81
|
-
# true
|
82
|
-
# end
|
83
|
-
#
|
84
|
-
# def clear_attribute(attr)
|
85
|
-
# send("#{attr}=", nil)
|
86
|
-
# end
|
87
|
-
#
|
88
|
-
# def reset_attribute_to_default!(attr)
|
89
|
-
# send("#{attr}=", 'Default Name')
|
90
|
-
# end
|
91
|
-
# end
|
92
|
-
module AttributeMethods
|
93
|
-
extend ActiveSupport::Concern
|
94
|
-
|
95
|
-
NAME_COMPILABLE_REGEXP: untyped
|
96
|
-
|
97
|
-
CALL_COMPILABLE_REGEXP: untyped
|
98
|
-
|
99
|
-
module ClassMethods
|
100
|
-
# Declares a method available for all attributes with the given prefix.
|
101
|
-
# Uses +method_missing+ and <tt>respond_to?</tt> to rewrite the method.
|
102
|
-
#
|
103
|
-
# #{prefix}#{attr}(*args, &block)
|
104
|
-
#
|
105
|
-
# to
|
106
|
-
#
|
107
|
-
# #{prefix}attribute(#{attr}, *args, &block)
|
108
|
-
#
|
109
|
-
# An instance method <tt>#{prefix}attribute</tt> must exist and accept
|
110
|
-
# at least the +attr+ argument.
|
111
|
-
#
|
112
|
-
# class Person
|
113
|
-
# include ActiveModel::AttributeMethods
|
114
|
-
#
|
115
|
-
# attr_accessor :name
|
116
|
-
# attribute_method_prefix 'clear_'
|
117
|
-
# define_attribute_methods :name
|
118
|
-
#
|
119
|
-
# private
|
120
|
-
#
|
121
|
-
# def clear_attribute(attr)
|
122
|
-
# send("#{attr}=", nil)
|
123
|
-
# end
|
124
|
-
# end
|
125
|
-
#
|
126
|
-
# person = Person.new
|
127
|
-
# person.name = 'Bob'
|
128
|
-
# person.name # => "Bob"
|
129
|
-
# person.clear_name
|
130
|
-
# person.name # => nil
|
131
|
-
def attribute_method_prefix: (*untyped prefixes) -> untyped
|
132
|
-
|
133
|
-
# Declares a method available for all attributes with the given suffix.
|
134
|
-
# Uses +method_missing+ and <tt>respond_to?</tt> to rewrite the method.
|
135
|
-
#
|
136
|
-
# #{attr}#{suffix}(*args, &block)
|
137
|
-
#
|
138
|
-
# to
|
139
|
-
#
|
140
|
-
# attribute#{suffix}(#{attr}, *args, &block)
|
141
|
-
#
|
142
|
-
# An <tt>attribute#{suffix}</tt> instance method must exist and accept at
|
143
|
-
# least the +attr+ argument.
|
144
|
-
#
|
145
|
-
# class Person
|
146
|
-
# include ActiveModel::AttributeMethods
|
147
|
-
#
|
148
|
-
# attr_accessor :name
|
149
|
-
# attribute_method_suffix '_short?'
|
150
|
-
# define_attribute_methods :name
|
151
|
-
#
|
152
|
-
# private
|
153
|
-
#
|
154
|
-
# def attribute_short?(attr)
|
155
|
-
# send(attr).length < 5
|
156
|
-
# end
|
157
|
-
# end
|
158
|
-
#
|
159
|
-
# person = Person.new
|
160
|
-
# person.name = 'Bob'
|
161
|
-
# person.name # => "Bob"
|
162
|
-
# person.name_short? # => true
|
163
|
-
def attribute_method_suffix: (*untyped suffixes) -> untyped
|
164
|
-
|
165
|
-
# Declares a method available for all attributes with the given prefix
|
166
|
-
# and suffix. Uses +method_missing+ and <tt>respond_to?</tt> to rewrite
|
167
|
-
# the method.
|
168
|
-
#
|
169
|
-
# #{prefix}#{attr}#{suffix}(*args, &block)
|
170
|
-
#
|
171
|
-
# to
|
172
|
-
#
|
173
|
-
# #{prefix}attribute#{suffix}(#{attr}, *args, &block)
|
174
|
-
#
|
175
|
-
# An <tt>#{prefix}attribute#{suffix}</tt> instance method must exist and
|
176
|
-
# accept at least the +attr+ argument.
|
177
|
-
#
|
178
|
-
# class Person
|
179
|
-
# include ActiveModel::AttributeMethods
|
180
|
-
#
|
181
|
-
# attr_accessor :name
|
182
|
-
# attribute_method_affix prefix: 'reset_', suffix: '_to_default!'
|
183
|
-
# define_attribute_methods :name
|
184
|
-
#
|
185
|
-
# private
|
186
|
-
#
|
187
|
-
# def reset_attribute_to_default!(attr)
|
188
|
-
# send("#{attr}=", 'Default Name')
|
189
|
-
# end
|
190
|
-
# end
|
191
|
-
#
|
192
|
-
# person = Person.new
|
193
|
-
# person.name # => 'Gem'
|
194
|
-
# person.reset_name_to_default!
|
195
|
-
# person.name # => 'Default Name'
|
196
|
-
def attribute_method_affix: (*untyped affixes) -> untyped
|
197
|
-
|
198
|
-
# Allows you to make aliases for attributes.
|
199
|
-
#
|
200
|
-
# class Person
|
201
|
-
# include ActiveModel::AttributeMethods
|
202
|
-
#
|
203
|
-
# attr_accessor :name
|
204
|
-
# attribute_method_suffix '_short?'
|
205
|
-
# define_attribute_methods :name
|
206
|
-
#
|
207
|
-
# alias_attribute :nickname, :name
|
208
|
-
#
|
209
|
-
# private
|
210
|
-
#
|
211
|
-
# def attribute_short?(attr)
|
212
|
-
# send(attr).length < 5
|
213
|
-
# end
|
214
|
-
# end
|
215
|
-
#
|
216
|
-
# person = Person.new
|
217
|
-
# person.name = 'Bob'
|
218
|
-
# person.name # => "Bob"
|
219
|
-
# person.nickname # => "Bob"
|
220
|
-
# person.name_short? # => true
|
221
|
-
# person.nickname_short? # => true
|
222
|
-
def alias_attribute: (untyped new_name, untyped old_name) -> untyped
|
223
|
-
|
224
|
-
# Is +new_name+ an alias?
|
225
|
-
def attribute_alias?: (untyped new_name) -> untyped
|
226
|
-
|
227
|
-
# Returns the original name for the alias +name+
|
228
|
-
def attribute_alias: (untyped name) -> untyped
|
229
|
-
|
230
|
-
# Declares the attributes that should be prefixed and suffixed by
|
231
|
-
# <tt>ActiveModel::AttributeMethods</tt>.
|
232
|
-
#
|
233
|
-
# To use, pass attribute names (as strings or symbols). Be sure to declare
|
234
|
-
# +define_attribute_methods+ after you define any prefix, suffix or affix
|
235
|
-
# methods, or they will not hook in.
|
236
|
-
#
|
237
|
-
# class Person
|
238
|
-
# include ActiveModel::AttributeMethods
|
239
|
-
#
|
240
|
-
# attr_accessor :name, :age, :address
|
241
|
-
# attribute_method_prefix 'clear_'
|
242
|
-
#
|
243
|
-
# # Call to define_attribute_methods must appear after the
|
244
|
-
# # attribute_method_prefix, attribute_method_suffix or
|
245
|
-
# # attribute_method_affix declarations.
|
246
|
-
# define_attribute_methods :name, :age, :address
|
247
|
-
#
|
248
|
-
# private
|
249
|
-
#
|
250
|
-
# def clear_attribute(attr)
|
251
|
-
# send("#{attr}=", nil)
|
252
|
-
# end
|
253
|
-
# end
|
254
|
-
def define_attribute_methods: (*untyped attr_names) -> untyped
|
255
|
-
|
256
|
-
# Declares an attribute that should be prefixed and suffixed by
|
257
|
-
# <tt>ActiveModel::AttributeMethods</tt>.
|
258
|
-
#
|
259
|
-
# To use, pass an attribute name (as string or symbol). Be sure to declare
|
260
|
-
# +define_attribute_method+ after you define any prefix, suffix or affix
|
261
|
-
# method, or they will not hook in.
|
262
|
-
#
|
263
|
-
# class Person
|
264
|
-
# include ActiveModel::AttributeMethods
|
265
|
-
#
|
266
|
-
# attr_accessor :name
|
267
|
-
# attribute_method_suffix '_short?'
|
268
|
-
#
|
269
|
-
# # Call to define_attribute_method must appear after the
|
270
|
-
# # attribute_method_prefix, attribute_method_suffix or
|
271
|
-
# # attribute_method_affix declarations.
|
272
|
-
# define_attribute_method :name
|
273
|
-
#
|
274
|
-
# private
|
275
|
-
#
|
276
|
-
# def attribute_short?(attr)
|
277
|
-
# send(attr).length < 5
|
278
|
-
# end
|
279
|
-
# end
|
280
|
-
#
|
281
|
-
# person = Person.new
|
282
|
-
# person.name = 'Bob'
|
283
|
-
# person.name # => "Bob"
|
284
|
-
# person.name_short? # => true
|
285
|
-
def define_attribute_method: (untyped attr_name) -> untyped
|
286
|
-
|
287
|
-
# Removes all the previously dynamically defined methods from the class.
|
288
|
-
#
|
289
|
-
# class Person
|
290
|
-
# include ActiveModel::AttributeMethods
|
291
|
-
#
|
292
|
-
# attr_accessor :name
|
293
|
-
# attribute_method_suffix '_short?'
|
294
|
-
# define_attribute_method :name
|
295
|
-
#
|
296
|
-
# private
|
297
|
-
#
|
298
|
-
# def attribute_short?(attr)
|
299
|
-
# send(attr).length < 5
|
300
|
-
# end
|
301
|
-
# end
|
302
|
-
#
|
303
|
-
# person = Person.new
|
304
|
-
# person.name = 'Bob'
|
305
|
-
# person.name_short? # => true
|
306
|
-
#
|
307
|
-
# Person.undefine_attribute_methods
|
308
|
-
#
|
309
|
-
# person.name_short? # => NoMethodError
|
310
|
-
def undefine_attribute_methods: () -> untyped
|
311
|
-
|
312
|
-
def generated_attribute_methods: () -> untyped
|
313
|
-
|
314
|
-
def instance_method_already_implemented?: (untyped method_name) -> untyped
|
315
|
-
|
316
|
-
# The methods +method_missing+ and +respond_to?+ of this module are
|
317
|
-
# invoked often in a typical rails, both of which invoke the method
|
318
|
-
# +matched_attribute_method+. The latter method iterates through an
|
319
|
-
# array doing regular expression matches, which results in a lot of
|
320
|
-
# object creations. Most of the time it returns a +nil+ match. As the
|
321
|
-
# match result is always the same given a +method_name+, this cache is
|
322
|
-
# used to alleviate the GC, which ultimately also speeds up the app
|
323
|
-
# significantly (in our case our test suite finishes 10% faster with
|
324
|
-
# this cache).
|
325
|
-
def attribute_method_matchers_cache: () -> untyped
|
326
|
-
|
327
|
-
def attribute_method_matchers_matching: (untyped method_name) -> untyped
|
328
|
-
|
329
|
-
# Define a method `name` in `mod` that dispatches to `send`
|
330
|
-
# using the given `extra` args. This falls back on `define_method`
|
331
|
-
# and `send` if the given names cannot be compiled.
|
332
|
-
def define_proxy_call: (untyped include_private, untyped mod, untyped name, untyped target, *untyped extra) -> untyped
|
333
|
-
|
334
|
-
class AttributeMethodMatcher
|
335
|
-
# nodoc:
|
336
|
-
attr_reader prefix: untyped
|
337
|
-
|
338
|
-
# nodoc:
|
339
|
-
attr_reader suffix: untyped
|
340
|
-
|
341
|
-
# nodoc:
|
342
|
-
attr_reader target: untyped
|
343
|
-
|
344
|
-
class AttributeMethodMatch < ::Struct[untyped]
|
345
|
-
attr_accessor target(): untyped
|
346
|
-
|
347
|
-
attr_accessor attr_name(): untyped
|
348
|
-
end
|
349
|
-
|
350
|
-
def initialize: (?::Hash[untyped, untyped] options) -> untyped
|
351
|
-
|
352
|
-
def match: (untyped method_name) -> untyped
|
353
|
-
|
354
|
-
def method_name: (untyped attr_name) -> untyped
|
355
|
-
|
356
|
-
def plain?: () -> untyped
|
357
|
-
end
|
358
|
-
end
|
359
|
-
|
360
|
-
# Allows access to the object attributes, which are held in the hash
|
361
|
-
# returned by <tt>attributes</tt>, as though they were first-class
|
362
|
-
# methods. So a +Person+ class with a +name+ attribute can for example use
|
363
|
-
# <tt>Person#name</tt> and <tt>Person#name=</tt> and never directly use
|
364
|
-
# the attributes hash -- except for multiple assignments with
|
365
|
-
# <tt>ActiveRecord::Base#attributes=</tt>.
|
366
|
-
#
|
367
|
-
# It's also possible to instantiate related objects, so a <tt>Client</tt>
|
368
|
-
# class belonging to the +clients+ table with a +master_id+ foreign key
|
369
|
-
# can instantiate master through <tt>Client#master</tt>.
|
370
|
-
def method_missing: (untyped method, *untyped args) { () -> untyped } -> untyped
|
371
|
-
|
372
|
-
# +attribute_missing+ is like +method_missing+, but for attributes. When
|
373
|
-
# +method_missing+ is called we check to see if there is a matching
|
374
|
-
# attribute method. If so, we tell +attribute_missing+ to dispatch the
|
375
|
-
# attribute. This method can be overloaded to customize the behavior.
|
376
|
-
def attribute_missing: (untyped match, *untyped args) { () -> untyped } -> untyped
|
377
|
-
|
378
|
-
def respond_to?: (untyped method, ?bool include_private_methods) -> untyped
|
379
|
-
|
380
|
-
def attribute_method?: (untyped attr_name) -> untyped
|
381
|
-
|
382
|
-
# Returns a struct representing the matching attribute method.
|
383
|
-
# The struct's attributes are prefix, base and suffix.
|
384
|
-
def matched_attribute_method: (untyped method_name) -> untyped
|
385
|
-
|
386
|
-
def missing_attribute: (untyped attr_name, untyped stack) -> untyped
|
387
|
-
|
388
|
-
def _read_attribute: (untyped attr) -> untyped
|
389
|
-
|
390
|
-
module AttrNames
|
391
|
-
# :nodoc:
|
392
|
-
DEF_SAFE_NAME: untyped
|
393
|
-
|
394
|
-
# We want to generate the methods via module_eval rather than
|
395
|
-
# define_method, because define_method is slower on dispatch.
|
396
|
-
# Evaluating many similar methods may use more memory as the instruction
|
397
|
-
# sequences are duplicated and cached (in MRI). define_method may
|
398
|
-
# be slower on dispatch, but if you're careful about the closure
|
399
|
-
# created, then define_method will consume much less memory.
|
400
|
-
#
|
401
|
-
# But sometimes the database might return columns with
|
402
|
-
# characters that are not allowed in normal method names (like
|
403
|
-
# 'my_column(omg)'. So to work around this we first define with
|
404
|
-
# the __temp__ identifier, and then use alias method to rename
|
405
|
-
# it to what we want.
|
406
|
-
#
|
407
|
-
# We are also defining a constant to hold the frozen string of
|
408
|
-
# the attribute name. Using a constant means that we do not have
|
409
|
-
# to allocate an object on each call to the attribute method.
|
410
|
-
# Making it frozen means that it doesn't get duped when used to
|
411
|
-
# key the @attributes in read_attribute.
|
412
|
-
def self.define_attribute_accessor_method: (untyped mod, untyped attr_name, ?writer: bool writer) { (untyped, untyped) -> untyped } -> untyped
|
413
|
-
end
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
module ActiveModel
|
418
|
-
class AttributeMutationTracker
|
419
|
-
# :nodoc:
|
420
|
-
OPTION_NOT_GIVEN: untyped
|
421
|
-
|
422
|
-
def initialize: (untyped attributes, ?untyped forced_changes) -> untyped
|
423
|
-
|
424
|
-
def changed_attribute_names: () -> untyped
|
425
|
-
|
426
|
-
def changed_values: () -> untyped
|
427
|
-
|
428
|
-
def changes: () -> untyped
|
429
|
-
|
430
|
-
def change_to_attribute: (untyped attr_name) -> untyped
|
431
|
-
|
432
|
-
def any_changes?: () -> untyped
|
433
|
-
|
434
|
-
def changed?: (untyped attr_name, ?to: untyped to, ?from: untyped from) -> untyped
|
435
|
-
|
436
|
-
def changed_in_place?: (untyped attr_name) -> untyped
|
437
|
-
|
438
|
-
def forget_change: (untyped attr_name) -> untyped
|
439
|
-
|
440
|
-
def original_value: (untyped attr_name) -> untyped
|
441
|
-
|
442
|
-
def force_change: (untyped attr_name) -> untyped
|
443
|
-
|
444
|
-
attr_reader attributes: untyped
|
445
|
-
|
446
|
-
attr_reader forced_changes: untyped
|
447
|
-
|
448
|
-
def attr_names: () -> untyped
|
449
|
-
|
450
|
-
def attribute_changed?: (untyped attr_name) -> untyped
|
451
|
-
|
452
|
-
def fetch_value: (untyped attr_name) -> untyped
|
453
|
-
end
|
454
|
-
|
455
|
-
class ForcedMutationTracker < AttributeMutationTracker
|
456
|
-
# :nodoc:
|
457
|
-
def initialize: (untyped attributes, ?::Hash[untyped, untyped] forced_changes) -> untyped
|
458
|
-
|
459
|
-
def changed_in_place?: (untyped attr_name) -> ::FalseClass
|
460
|
-
|
461
|
-
def change_to_attribute: (untyped attr_name) -> untyped
|
462
|
-
|
463
|
-
def forget_change: (untyped attr_name) -> untyped
|
464
|
-
|
465
|
-
def original_value: (untyped attr_name) -> untyped
|
466
|
-
|
467
|
-
def force_change: (untyped attr_name) -> untyped
|
468
|
-
|
469
|
-
def finalize_changes: () -> untyped
|
470
|
-
|
471
|
-
attr_reader finalized_changes: untyped
|
472
|
-
|
473
|
-
def attr_names: () -> untyped
|
474
|
-
|
475
|
-
def attribute_changed?: (untyped attr_name) -> untyped
|
476
|
-
|
477
|
-
def fetch_value: (untyped attr_name) -> untyped
|
478
|
-
|
479
|
-
def clone_value: (untyped attr_name) -> untyped
|
480
|
-
end
|
481
|
-
|
482
|
-
class NullMutationTracker
|
483
|
-
# :nodoc:
|
484
|
-
include Singleton
|
485
|
-
|
486
|
-
def changed_attribute_names: () -> ::Array[untyped]
|
487
|
-
|
488
|
-
def changed_values: () -> ::Hash[untyped, untyped]
|
489
|
-
|
490
|
-
def changes: () -> ::Hash[untyped, untyped]
|
491
|
-
|
492
|
-
def change_to_attribute: (untyped attr_name) -> nil
|
493
|
-
|
494
|
-
def any_changes?: () -> ::FalseClass
|
495
|
-
|
496
|
-
def changed?: (untyped attr_name) -> ::FalseClass
|
497
|
-
|
498
|
-
def changed_in_place?: (untyped attr_name) -> ::FalseClass
|
499
|
-
|
500
|
-
def original_value: (untyped attr_name) -> nil
|
501
|
-
end
|
502
|
-
end
|
503
|
-
|
504
|
-
module ActiveModel
|
505
|
-
class Attribute
|
506
|
-
def self.from_database: (untyped name, untyped value, untyped `type`) -> FromDatabase
|
507
|
-
|
508
|
-
def self.from_user: (untyped name, untyped value, untyped `type`, ?untyped? original_attribute) -> FromUser
|
509
|
-
|
510
|
-
def self.with_cast_value: (untyped name, untyped value, untyped `type`) -> WithCastValue
|
511
|
-
|
512
|
-
def self.null: (untyped name) -> Null
|
513
|
-
|
514
|
-
def self.uninitialized: (untyped name, untyped `type`) -> Uninitialized
|
515
|
-
|
516
|
-
attr_reader name: untyped
|
517
|
-
|
518
|
-
attr_reader value_before_type_cast: untyped
|
519
|
-
|
520
|
-
attr_reader type: untyped
|
521
|
-
|
522
|
-
# This method should not be called directly.
|
523
|
-
# Use #from_database or #from_user
|
524
|
-
def initialize: (untyped name, untyped value_before_type_cast, untyped `type`, ?untyped? original_attribute) -> untyped
|
525
|
-
|
526
|
-
def value: () -> untyped
|
527
|
-
|
528
|
-
def original_value: () -> untyped
|
529
|
-
|
530
|
-
def value_for_database: () -> untyped
|
531
|
-
|
532
|
-
def changed?: () -> untyped
|
533
|
-
|
534
|
-
def changed_in_place?: () -> untyped
|
535
|
-
|
536
|
-
def forgetting_assignment: () -> untyped
|
537
|
-
|
538
|
-
def with_value_from_user: (untyped value) -> untyped
|
539
|
-
|
540
|
-
def with_value_from_database: (untyped value) -> untyped
|
541
|
-
|
542
|
-
def with_cast_value: (untyped value) -> untyped
|
543
|
-
|
544
|
-
def with_type: (untyped `type`) -> untyped
|
545
|
-
|
546
|
-
def type_cast: () -> untyped
|
547
|
-
|
548
|
-
def initialized?: () -> ::TrueClass
|
549
|
-
|
550
|
-
def came_from_user?: () -> ::FalseClass
|
551
|
-
|
552
|
-
def has_been_read?: () -> untyped
|
553
|
-
|
554
|
-
def ==: (untyped other) -> untyped
|
555
|
-
|
556
|
-
def hash: () -> untyped
|
557
|
-
|
558
|
-
def init_with: (untyped coder) -> untyped
|
559
|
-
|
560
|
-
def encode_with: (untyped coder) -> untyped
|
561
|
-
|
562
|
-
def original_value_for_database: () -> untyped
|
563
|
-
|
564
|
-
attr_reader original_attribute: untyped
|
565
|
-
|
566
|
-
def initialize_dup: (untyped other) -> untyped
|
567
|
-
|
568
|
-
def changed_from_assignment?: () -> untyped
|
569
|
-
|
570
|
-
def _original_value_for_database: () -> untyped
|
571
|
-
|
572
|
-
class FromDatabase < Attribute
|
573
|
-
# :nodoc:
|
574
|
-
def type_cast: (untyped value) -> untyped
|
575
|
-
|
576
|
-
def _original_value_for_database: () -> untyped
|
577
|
-
end
|
578
|
-
|
579
|
-
class FromUser < Attribute
|
580
|
-
# :nodoc:
|
581
|
-
def type_cast: (untyped value) -> untyped
|
582
|
-
|
583
|
-
def came_from_user?: () -> untyped
|
584
|
-
end
|
585
|
-
|
586
|
-
class WithCastValue < Attribute
|
587
|
-
# :nodoc:
|
588
|
-
def type_cast: (untyped value) -> untyped
|
589
|
-
|
590
|
-
def changed_in_place?: () -> ::FalseClass
|
591
|
-
end
|
592
|
-
|
593
|
-
class Null < Attribute
|
594
|
-
# :nodoc:
|
595
|
-
def initialize: (untyped name) -> untyped
|
596
|
-
|
597
|
-
def type_cast: () -> nil
|
598
|
-
|
599
|
-
def with_type: (untyped `type`) -> untyped
|
600
|
-
|
601
|
-
def with_value_from_database: (untyped value) -> untyped
|
602
|
-
end
|
603
|
-
|
604
|
-
class Uninitialized < Attribute
|
605
|
-
# :nodoc:
|
606
|
-
UNINITIALIZED_ORIGINAL_VALUE: untyped
|
607
|
-
|
608
|
-
def initialize: (untyped name, untyped `type`) -> untyped
|
609
|
-
|
610
|
-
def value: () { (untyped) -> untyped } -> untyped
|
611
|
-
|
612
|
-
def original_value: () -> untyped
|
613
|
-
|
614
|
-
def value_for_database: () -> nil
|
615
|
-
|
616
|
-
def initialized?: () -> ::FalseClass
|
617
|
-
|
618
|
-
def forgetting_assignment: () -> untyped
|
619
|
-
|
620
|
-
def with_type: (untyped `type`) -> untyped
|
621
|
-
end
|
622
|
-
end
|
623
|
-
end
|
624
|
-
|
625
|
-
module ActiveModel
|
626
|
-
class AttributeSet
|
627
|
-
class Builder
|
628
|
-
# :nodoc:
|
629
|
-
# :nodoc:
|
630
|
-
attr_reader types: untyped
|
631
|
-
|
632
|
-
# :nodoc:
|
633
|
-
# :nodoc:
|
634
|
-
attr_reader default_attributes: untyped
|
635
|
-
|
636
|
-
def initialize: (untyped types, ?::Hash[untyped, untyped] default_attributes) -> untyped
|
637
|
-
|
638
|
-
def build_from_database: (?::Hash[untyped, untyped] values, ?::Hash[untyped, untyped] additional_types) -> AttributeSet
|
639
|
-
end
|
640
|
-
end
|
641
|
-
|
642
|
-
class LazyAttributeHash
|
643
|
-
def initialize: (untyped types, untyped values, untyped additional_types, untyped default_attributes, ?::Hash[untyped, untyped] delegate_hash) -> untyped
|
644
|
-
|
645
|
-
def key?: (untyped key) -> untyped
|
646
|
-
|
647
|
-
def []: (untyped key) -> untyped
|
648
|
-
|
649
|
-
def []=: (untyped key, untyped value) -> untyped
|
650
|
-
|
651
|
-
def deep_dup: () -> untyped
|
652
|
-
|
653
|
-
def initialize_dup: (untyped _) -> untyped
|
654
|
-
|
655
|
-
def select: () { (untyped, untyped) -> untyped } -> untyped
|
656
|
-
|
657
|
-
def ==: (untyped other) -> untyped
|
658
|
-
|
659
|
-
def marshal_dump: () -> ::Array[untyped]
|
660
|
-
|
661
|
-
def marshal_load: (untyped values) -> untyped
|
662
|
-
|
663
|
-
def materialize: () -> untyped
|
664
|
-
|
665
|
-
attr_reader types: untyped
|
666
|
-
|
667
|
-
attr_reader values: untyped
|
668
|
-
|
669
|
-
attr_reader additional_types: untyped
|
670
|
-
|
671
|
-
attr_reader delegate_hash: untyped
|
672
|
-
|
673
|
-
attr_reader default_attributes: untyped
|
674
|
-
|
675
|
-
def assign_default_value: (untyped name) -> untyped
|
676
|
-
end
|
677
|
-
end
|
678
|
-
|
679
|
-
module ActiveModel
|
680
|
-
class AttributeSet
|
681
|
-
def initialize: (untyped attributes) -> untyped
|
682
|
-
|
683
|
-
def []: (untyped name) -> untyped
|
684
|
-
|
685
|
-
def []=: (untyped name, untyped value) -> untyped
|
686
|
-
|
687
|
-
def values_before_type_cast: () -> untyped
|
688
|
-
|
689
|
-
def to_hash: () -> untyped
|
690
|
-
|
691
|
-
def key?: (untyped name) -> untyped
|
692
|
-
|
693
|
-
def keys: () -> untyped
|
694
|
-
|
695
|
-
def fetch_value: (untyped name) { () -> untyped } -> untyped
|
696
|
-
|
697
|
-
def write_from_database: (untyped name, untyped value) -> untyped
|
698
|
-
|
699
|
-
def write_from_user: (untyped name, untyped value) -> untyped
|
700
|
-
|
701
|
-
def write_cast_value: (untyped name, untyped value) -> untyped
|
702
|
-
|
703
|
-
def freeze: () -> untyped
|
704
|
-
|
705
|
-
def deep_dup: () -> untyped
|
706
|
-
|
707
|
-
def initialize_dup: (untyped _) -> untyped
|
708
|
-
|
709
|
-
def initialize_clone: (untyped _) -> untyped
|
710
|
-
|
711
|
-
def reset: (untyped key) -> untyped
|
712
|
-
|
713
|
-
def accessed: () -> untyped
|
714
|
-
|
715
|
-
def map: () { () -> untyped } -> AttributeSet
|
716
|
-
|
717
|
-
def ==: (untyped other) -> untyped
|
718
|
-
|
719
|
-
attr_reader attributes: untyped
|
720
|
-
|
721
|
-
def initialized_attributes: () -> untyped
|
722
|
-
end
|
723
|
-
end
|
724
|
-
|
725
|
-
module ActiveModel
|
726
|
-
class AttributeSet
|
727
|
-
class YAMLEncoder
|
728
|
-
# Attempts to do more intelligent YAML dumping of an
|
729
|
-
# ActiveModel::AttributeSet to reduce the size of the resulting string
|
730
|
-
# :nodoc:
|
731
|
-
def initialize: (untyped default_types) -> untyped
|
732
|
-
|
733
|
-
def encode: (untyped attribute_set, untyped coder) -> untyped
|
734
|
-
|
735
|
-
def decode: (untyped coder) -> untyped
|
736
|
-
|
737
|
-
attr_reader default_types: untyped
|
738
|
-
end
|
739
|
-
end
|
740
|
-
end
|
741
|
-
|
742
|
-
module ActiveModel
|
743
|
-
module Attributes
|
744
|
-
# nodoc:
|
745
|
-
extend ActiveSupport::Concern
|
746
|
-
|
747
|
-
include ActiveModel::AttributeMethods
|
748
|
-
|
749
|
-
extend ::ActiveModel::AttributeMethods::ClassMethods
|
750
|
-
|
751
|
-
module ClassMethods
|
752
|
-
def attribute: (untyped name, ?untyped `type`, **untyped options) -> untyped
|
753
|
-
|
754
|
-
# Returns an array of attribute names as strings
|
755
|
-
#
|
756
|
-
# class Person
|
757
|
-
# include ActiveModel::Attributes
|
758
|
-
#
|
759
|
-
# attribute :name, :string
|
760
|
-
# attribute :age, :integer
|
761
|
-
# end
|
762
|
-
#
|
763
|
-
# Person.attribute_names
|
764
|
-
# # => ["name", "age"]
|
765
|
-
def attribute_names: () -> untyped
|
766
|
-
|
767
|
-
def define_method_attribute=: (untyped name) -> untyped
|
768
|
-
|
769
|
-
NO_DEFAULT_PROVIDED: untyped
|
770
|
-
|
771
|
-
def define_default_attribute: (untyped name, untyped value, untyped `type`) -> untyped
|
772
|
-
end
|
773
|
-
|
774
|
-
def initialize: () -> untyped
|
775
|
-
|
776
|
-
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
|
777
|
-
#
|
778
|
-
# class Person
|
779
|
-
# include ActiveModel::Model
|
780
|
-
# include ActiveModel::Attributes
|
781
|
-
#
|
782
|
-
# attribute :name, :string
|
783
|
-
# attribute :age, :integer
|
784
|
-
# end
|
785
|
-
#
|
786
|
-
# person = Person.new(name: 'Francesco', age: 22)
|
787
|
-
# person.attributes
|
788
|
-
# # => {"name"=>"Francesco", "age"=>22}
|
789
|
-
def attributes: () -> untyped
|
790
|
-
|
791
|
-
# Returns an array of attribute names as strings
|
792
|
-
#
|
793
|
-
# class Person
|
794
|
-
# include ActiveModel::Attributes
|
795
|
-
#
|
796
|
-
# attribute :name, :string
|
797
|
-
# attribute :age, :integer
|
798
|
-
# end
|
799
|
-
#
|
800
|
-
# person = Person.new
|
801
|
-
# person.attribute_names
|
802
|
-
# # => ["name", "age"]
|
803
|
-
def attribute_names: () -> untyped
|
804
|
-
|
805
|
-
def write_attribute: (untyped attr_name, untyped value) -> untyped
|
806
|
-
|
807
|
-
def attribute: (untyped attr_name) -> untyped
|
808
|
-
|
809
|
-
# Dispatch target for <tt>*=</tt> attribute methods.
|
810
|
-
def attribute=: (untyped attribute_name, untyped value) -> untyped
|
811
|
-
end
|
812
|
-
end
|
813
|
-
|
814
|
-
module ActiveModel
|
815
|
-
class Attribute
|
816
|
-
class UserProvidedDefault < FromUser
|
817
|
-
# :nodoc:
|
818
|
-
# :nodoc:
|
819
|
-
def initialize: (untyped name, untyped value, untyped `type`, untyped database_default) -> untyped
|
820
|
-
|
821
|
-
def value_before_type_cast: () -> untyped
|
822
|
-
|
823
|
-
def with_type: (untyped `type`) -> untyped
|
824
|
-
|
825
|
-
def marshal_dump: () -> untyped
|
826
|
-
|
827
|
-
def marshal_load: (untyped values) -> untyped
|
828
|
-
|
829
|
-
attr_reader user_provided_value: untyped
|
830
|
-
end
|
831
|
-
end
|
832
|
-
end
|
833
|
-
|
834
|
-
module ActiveModel
|
835
|
-
# == Active \Model \Callbacks
|
836
|
-
#
|
837
|
-
# Provides an interface for any class to have Active Record like callbacks.
|
838
|
-
#
|
839
|
-
# Like the Active Record methods, the callback chain is aborted as soon as
|
840
|
-
# one of the methods throws +:abort+.
|
841
|
-
#
|
842
|
-
# First, extend ActiveModel::Callbacks from the class you are creating:
|
843
|
-
#
|
844
|
-
# class MyModel
|
845
|
-
# extend ActiveModel::Callbacks
|
846
|
-
# end
|
847
|
-
#
|
848
|
-
# Then define a list of methods that you want callbacks attached to:
|
849
|
-
#
|
850
|
-
# define_model_callbacks :create, :update
|
851
|
-
#
|
852
|
-
# This will provide all three standard callbacks (before, around and after)
|
853
|
-
# for both the <tt>:create</tt> and <tt>:update</tt> methods. To implement,
|
854
|
-
# you need to wrap the methods you want callbacks on in a block so that the
|
855
|
-
# callbacks get a chance to fire:
|
856
|
-
#
|
857
|
-
# def create
|
858
|
-
# run_callbacks :create do
|
859
|
-
# # Your create action methods here
|
860
|
-
# end
|
861
|
-
# end
|
862
|
-
#
|
863
|
-
# Then in your class, you can use the +before_create+, +after_create+ and
|
864
|
-
# +around_create+ methods, just as you would in an Active Record model.
|
865
|
-
#
|
866
|
-
# before_create :action_before_create
|
867
|
-
#
|
868
|
-
# def action_before_create
|
869
|
-
# # Your code here
|
870
|
-
# end
|
871
|
-
#
|
872
|
-
# When defining an around callback remember to yield to the block, otherwise
|
873
|
-
# it won't be executed:
|
874
|
-
#
|
875
|
-
# around_create :log_status
|
876
|
-
#
|
877
|
-
# def log_status
|
878
|
-
# puts 'going to call the block...'
|
879
|
-
# yield
|
880
|
-
# puts 'block successfully called.'
|
881
|
-
# end
|
882
|
-
#
|
883
|
-
# You can choose to have only specific callbacks by passing a hash to the
|
884
|
-
# +define_model_callbacks+ method.
|
885
|
-
#
|
886
|
-
# define_model_callbacks :create, only: [:after, :before]
|
887
|
-
#
|
888
|
-
# Would only create the +after_create+ and +before_create+ callback methods in
|
889
|
-
# your class.
|
890
|
-
#
|
891
|
-
# NOTE: Calling the same callback multiple times will overwrite previous callback definitions.
|
892
|
-
#
|
893
|
-
module Callbacks
|
894
|
-
def self.extended: (untyped base) -> untyped
|
895
|
-
|
896
|
-
# define_model_callbacks accepts the same options +define_callbacks+ does,
|
897
|
-
# in case you want to overwrite a default. Besides that, it also accepts an
|
898
|
-
# <tt>:only</tt> option, where you can choose if you want all types (before,
|
899
|
-
# around or after) or just some.
|
900
|
-
#
|
901
|
-
# define_model_callbacks :initializer, only: :after
|
902
|
-
#
|
903
|
-
# Note, the <tt>only: <type></tt> hash will apply to all callbacks defined
|
904
|
-
# on that method call. To get around this you can call the define_model_callbacks
|
905
|
-
# method as many times as you need.
|
906
|
-
#
|
907
|
-
# define_model_callbacks :create, only: :after
|
908
|
-
# define_model_callbacks :update, only: :before
|
909
|
-
# define_model_callbacks :destroy, only: :around
|
910
|
-
#
|
911
|
-
# Would create +after_create+, +before_update+ and +around_destroy+ methods
|
912
|
-
# only.
|
913
|
-
#
|
914
|
-
# You can pass in a class to before_<type>, after_<type> and around_<type>,
|
915
|
-
# in which case the callback will call that class's <action>_<type> method
|
916
|
-
# passing the object that the callback is being called on.
|
917
|
-
#
|
918
|
-
# class MyModel
|
919
|
-
# extend ActiveModel::Callbacks
|
920
|
-
# define_model_callbacks :create
|
921
|
-
#
|
922
|
-
# before_create AnotherClass
|
923
|
-
# end
|
924
|
-
#
|
925
|
-
# class AnotherClass
|
926
|
-
# def self.before_create( obj )
|
927
|
-
# # obj is the MyModel instance that the callback is being called on
|
928
|
-
# end
|
929
|
-
# end
|
930
|
-
#
|
931
|
-
# NOTE: +method_name+ passed to define_model_callbacks must not end with
|
932
|
-
# <tt>!</tt>, <tt>?</tt> or <tt>=</tt>.
|
933
|
-
def define_model_callbacks: (*untyped callbacks) -> untyped
|
934
|
-
|
935
|
-
def _define_before_model_callback: (untyped klass, untyped callback) -> untyped
|
936
|
-
|
937
|
-
def _define_around_model_callback: (untyped klass, untyped callback) -> untyped
|
938
|
-
|
939
|
-
def _define_after_model_callback: (untyped klass, untyped callback) -> untyped
|
940
|
-
end
|
941
|
-
end
|
942
|
-
|
943
|
-
module ActiveModel
|
944
|
-
# == Active \Model \Conversion
|
945
|
-
#
|
946
|
-
# Handles default conversions: to_model, to_key, to_param, and to_partial_path.
|
947
|
-
#
|
948
|
-
# Let's take for example this non-persisted object.
|
949
|
-
#
|
950
|
-
# class ContactMessage
|
951
|
-
# include ActiveModel::Conversion
|
952
|
-
#
|
953
|
-
# # ContactMessage are never persisted in the DB
|
954
|
-
# def persisted?
|
955
|
-
# false
|
956
|
-
# end
|
957
|
-
# end
|
958
|
-
#
|
959
|
-
# cm = ContactMessage.new
|
960
|
-
# cm.to_model == cm # => true
|
961
|
-
# cm.to_key # => nil
|
962
|
-
# cm.to_param # => nil
|
963
|
-
# cm.to_partial_path # => "contact_messages/contact_message"
|
964
|
-
module Conversion
|
965
|
-
extend ActiveSupport::Concern
|
966
|
-
|
967
|
-
# If your object is already designed to implement all of the \Active \Model
|
968
|
-
# you can use the default <tt>:to_model</tt> implementation, which simply
|
969
|
-
# returns +self+.
|
970
|
-
#
|
971
|
-
# class Person
|
972
|
-
# include ActiveModel::Conversion
|
973
|
-
# end
|
974
|
-
#
|
975
|
-
# person = Person.new
|
976
|
-
# person.to_model == person # => true
|
977
|
-
#
|
978
|
-
# If your model does not act like an \Active \Model object, then you should
|
979
|
-
# define <tt>:to_model</tt> yourself returning a proxy object that wraps
|
980
|
-
# your object with \Active \Model compliant methods.
|
981
|
-
def to_model: () -> untyped
|
982
|
-
|
983
|
-
# Returns an Array of all key attributes if any of the attributes is set, whether or not
|
984
|
-
# the object is persisted. Returns +nil+ if there are no key attributes.
|
985
|
-
#
|
986
|
-
# class Person
|
987
|
-
# include ActiveModel::Conversion
|
988
|
-
# attr_accessor :id
|
989
|
-
#
|
990
|
-
# def initialize(id)
|
991
|
-
# @id = id
|
992
|
-
# end
|
993
|
-
# end
|
994
|
-
#
|
995
|
-
# person = Person.new(1)
|
996
|
-
# person.to_key # => [1]
|
997
|
-
def to_key: () -> untyped
|
998
|
-
|
999
|
-
# Returns a +string+ representing the object's key suitable for use in URLs,
|
1000
|
-
# or +nil+ if <tt>persisted?</tt> is +false+.
|
1001
|
-
#
|
1002
|
-
# class Person
|
1003
|
-
# include ActiveModel::Conversion
|
1004
|
-
# attr_accessor :id
|
1005
|
-
#
|
1006
|
-
# def initialize(id)
|
1007
|
-
# @id = id
|
1008
|
-
# end
|
1009
|
-
#
|
1010
|
-
# def persisted?
|
1011
|
-
# true
|
1012
|
-
# end
|
1013
|
-
# end
|
1014
|
-
#
|
1015
|
-
# person = Person.new(1)
|
1016
|
-
# person.to_param # => "1"
|
1017
|
-
def to_param: () -> untyped
|
1018
|
-
|
1019
|
-
# Returns a +string+ identifying the path associated with the object.
|
1020
|
-
# ActionPack uses this to find a suitable partial to represent the object.
|
1021
|
-
#
|
1022
|
-
# class Person
|
1023
|
-
# include ActiveModel::Conversion
|
1024
|
-
# end
|
1025
|
-
#
|
1026
|
-
# person = Person.new
|
1027
|
-
# person.to_partial_path # => "people/person"
|
1028
|
-
def to_partial_path: () -> untyped
|
1029
|
-
|
1030
|
-
module ClassMethods
|
1031
|
-
def _to_partial_path: () -> untyped
|
1032
|
-
end
|
1033
|
-
end
|
1034
|
-
end
|
1035
|
-
|
1036
|
-
module ActiveModel
|
1037
|
-
# == Active \Model \Dirty
|
1038
|
-
#
|
1039
|
-
# Provides a way to track changes in your object in the same way as
|
1040
|
-
# Active Record does.
|
1041
|
-
#
|
1042
|
-
# The requirements for implementing ActiveModel::Dirty are:
|
1043
|
-
#
|
1044
|
-
# * <tt>include ActiveModel::Dirty</tt> in your object.
|
1045
|
-
# * Call <tt>define_attribute_methods</tt> passing each method you want to
|
1046
|
-
# track.
|
1047
|
-
# * Call <tt>[attr_name]_will_change!</tt> before each change to the tracked
|
1048
|
-
# attribute.
|
1049
|
-
# * Call <tt>changes_applied</tt> after the changes are persisted.
|
1050
|
-
# * Call <tt>clear_changes_information</tt> when you want to reset the changes
|
1051
|
-
# information.
|
1052
|
-
# * Call <tt>restore_attributes</tt> when you want to restore previous data.
|
1053
|
-
#
|
1054
|
-
# A minimal implementation could be:
|
1055
|
-
#
|
1056
|
-
# class Person
|
1057
|
-
# include ActiveModel::Dirty
|
1058
|
-
#
|
1059
|
-
# define_attribute_methods :name
|
1060
|
-
#
|
1061
|
-
# def initialize
|
1062
|
-
# @name = nil
|
1063
|
-
# end
|
1064
|
-
#
|
1065
|
-
# def name
|
1066
|
-
# @name
|
1067
|
-
# end
|
1068
|
-
#
|
1069
|
-
# def name=(val)
|
1070
|
-
# name_will_change! unless val == @name
|
1071
|
-
# @name = val
|
1072
|
-
# end
|
1073
|
-
#
|
1074
|
-
# def save
|
1075
|
-
# # do persistence work
|
1076
|
-
#
|
1077
|
-
# changes_applied
|
1078
|
-
# end
|
1079
|
-
#
|
1080
|
-
# def reload!
|
1081
|
-
# # get the values from the persistence layer
|
1082
|
-
#
|
1083
|
-
# clear_changes_information
|
1084
|
-
# end
|
1085
|
-
#
|
1086
|
-
# def rollback!
|
1087
|
-
# restore_attributes
|
1088
|
-
# end
|
1089
|
-
# end
|
1090
|
-
#
|
1091
|
-
# A newly instantiated +Person+ object is unchanged:
|
1092
|
-
#
|
1093
|
-
# person = Person.new
|
1094
|
-
# person.changed? # => false
|
1095
|
-
#
|
1096
|
-
# Change the name:
|
1097
|
-
#
|
1098
|
-
# person.name = 'Bob'
|
1099
|
-
# person.changed? # => true
|
1100
|
-
# person.name_changed? # => true
|
1101
|
-
# person.name_changed?(from: nil, to: "Bob") # => true
|
1102
|
-
# person.name_was # => nil
|
1103
|
-
# person.name_change # => [nil, "Bob"]
|
1104
|
-
# person.name = 'Bill'
|
1105
|
-
# person.name_change # => [nil, "Bill"]
|
1106
|
-
#
|
1107
|
-
# Save the changes:
|
1108
|
-
#
|
1109
|
-
# person.save
|
1110
|
-
# person.changed? # => false
|
1111
|
-
# person.name_changed? # => false
|
1112
|
-
#
|
1113
|
-
# Reset the changes:
|
1114
|
-
#
|
1115
|
-
# person.previous_changes # => {"name" => [nil, "Bill"]}
|
1116
|
-
# person.name_previously_changed? # => true
|
1117
|
-
# person.name_previous_change # => [nil, "Bill"]
|
1118
|
-
# person.reload!
|
1119
|
-
# person.previous_changes # => {}
|
1120
|
-
#
|
1121
|
-
# Rollback the changes:
|
1122
|
-
#
|
1123
|
-
# person.name = "Uncle Bob"
|
1124
|
-
# person.rollback!
|
1125
|
-
# person.name # => "Bill"
|
1126
|
-
# person.name_changed? # => false
|
1127
|
-
#
|
1128
|
-
# Assigning the same value leaves the attribute unchanged:
|
1129
|
-
#
|
1130
|
-
# person.name = 'Bill'
|
1131
|
-
# person.name_changed? # => false
|
1132
|
-
# person.name_change # => nil
|
1133
|
-
#
|
1134
|
-
# Which attributes have changed?
|
1135
|
-
#
|
1136
|
-
# person.name = 'Bob'
|
1137
|
-
# person.changed # => ["name"]
|
1138
|
-
# person.changes # => {"name" => ["Bill", "Bob"]}
|
1139
|
-
#
|
1140
|
-
# If an attribute is modified in-place then make use of
|
1141
|
-
# <tt>[attribute_name]_will_change!</tt> to mark that the attribute is changing.
|
1142
|
-
# Otherwise \Active \Model can't track changes to in-place attributes. Note
|
1143
|
-
# that Active Record can detect in-place modifications automatically. You do
|
1144
|
-
# not need to call <tt>[attribute_name]_will_change!</tt> on Active Record models.
|
1145
|
-
#
|
1146
|
-
# person.name_will_change!
|
1147
|
-
# person.name_change # => ["Bill", "Bill"]
|
1148
|
-
# person.name << 'y'
|
1149
|
-
# person.name_change # => ["Bill", "Billy"]
|
1150
|
-
module Dirty
|
1151
|
-
extend ActiveSupport::Concern
|
1152
|
-
|
1153
|
-
include ActiveModel::AttributeMethods
|
1154
|
-
|
1155
|
-
extend ::ActiveModel::AttributeMethods::ClassMethods
|
1156
|
-
|
1157
|
-
def initialize_dup: (untyped other) -> untyped
|
1158
|
-
|
1159
|
-
# Clears dirty data and moves +changes+ to +previously_changed+ and
|
1160
|
-
# +mutations_from_database+ to +mutations_before_last_save+ respectively.
|
1161
|
-
def changes_applied: () -> untyped
|
1162
|
-
|
1163
|
-
# Returns +true+ if any of the attributes has unsaved changes, +false+ otherwise.
|
1164
|
-
#
|
1165
|
-
# person.changed? # => false
|
1166
|
-
# person.name = 'bob'
|
1167
|
-
# person.changed? # => true
|
1168
|
-
def changed?: () -> untyped
|
1169
|
-
|
1170
|
-
# Returns an array with the name of the attributes with unsaved changes.
|
1171
|
-
#
|
1172
|
-
# person.changed # => []
|
1173
|
-
# person.name = 'bob'
|
1174
|
-
# person.changed # => ["name"]
|
1175
|
-
def changed: () -> untyped
|
1176
|
-
|
1177
|
-
def attribute_changed?: (untyped attr_name, **untyped options) -> untyped
|
1178
|
-
|
1179
|
-
def attribute_was: (untyped attr_name) -> untyped
|
1180
|
-
|
1181
|
-
def attribute_previously_changed?: (untyped attr_name) -> untyped
|
1182
|
-
|
1183
|
-
# Restore all previous data of the provided attributes.
|
1184
|
-
def restore_attributes: (?untyped attr_names) -> untyped
|
1185
|
-
|
1186
|
-
# Clears all dirty data: current changes and previous changes.
|
1187
|
-
def clear_changes_information: () -> untyped
|
1188
|
-
|
1189
|
-
def clear_attribute_changes: (untyped attr_names) -> untyped
|
1190
|
-
|
1191
|
-
# Returns a hash of the attributes with unsaved changes indicating their original
|
1192
|
-
# values like <tt>attr => original value</tt>.
|
1193
|
-
#
|
1194
|
-
# person.name # => "bob"
|
1195
|
-
# person.name = 'robert'
|
1196
|
-
# person.changed_attributes # => {"name" => "bob"}
|
1197
|
-
def changed_attributes: () -> untyped
|
1198
|
-
|
1199
|
-
# Returns a hash of changed attributes indicating their original
|
1200
|
-
# and new values like <tt>attr => [original value, new value]</tt>.
|
1201
|
-
#
|
1202
|
-
# person.changes # => {}
|
1203
|
-
# person.name = 'bob'
|
1204
|
-
# person.changes # => { "name" => ["bill", "bob"] }
|
1205
|
-
def changes: () -> untyped
|
1206
|
-
|
1207
|
-
# Returns a hash of attributes that were changed before the model was saved.
|
1208
|
-
#
|
1209
|
-
# person.name # => "bob"
|
1210
|
-
# person.name = 'robert'
|
1211
|
-
# person.save
|
1212
|
-
# person.previous_changes # => {"name" => ["bob", "robert"]}
|
1213
|
-
def previous_changes: () -> untyped
|
1214
|
-
|
1215
|
-
def attribute_changed_in_place?: (untyped attr_name) -> untyped
|
1216
|
-
|
1217
|
-
def clear_attribute_change: (untyped attr_name) -> untyped
|
1218
|
-
|
1219
|
-
def mutations_from_database: () -> untyped
|
1220
|
-
|
1221
|
-
def forget_attribute_assignments: () -> untyped
|
1222
|
-
|
1223
|
-
def mutations_before_last_save: () -> untyped
|
1224
|
-
|
1225
|
-
# Dispatch target for <tt>*_change</tt> attribute methods.
|
1226
|
-
def attribute_change: (untyped attr_name) -> untyped
|
1227
|
-
|
1228
|
-
# Dispatch target for <tt>*_previous_change</tt> attribute methods.
|
1229
|
-
def attribute_previous_change: (untyped attr_name) -> untyped
|
1230
|
-
|
1231
|
-
# Dispatch target for <tt>*_will_change!</tt> attribute methods.
|
1232
|
-
def attribute_will_change!: (untyped attr_name) -> untyped
|
1233
|
-
|
1234
|
-
# Dispatch target for <tt>restore_*!</tt> attribute methods.
|
1235
|
-
def restore_attribute!: (untyped attr_name) -> untyped
|
1236
|
-
end
|
1237
|
-
end
|
1238
|
-
|
1239
|
-
module ActiveModel
|
1240
|
-
# == Active \Model \Errors
|
1241
|
-
#
|
1242
|
-
# Provides a modified +Hash+ that you can include in your object
|
1243
|
-
# for handling error messages and interacting with Action View helpers.
|
1244
|
-
#
|
1245
|
-
# A minimal implementation could be:
|
1246
|
-
#
|
1247
|
-
# class Person
|
1248
|
-
# # Required dependency for ActiveModel::Errors
|
1249
|
-
# extend ActiveModel::Naming
|
1250
|
-
#
|
1251
|
-
# def initialize
|
1252
|
-
# @errors = ActiveModel::Errors.new(self)
|
1253
|
-
# end
|
1254
|
-
#
|
1255
|
-
# attr_accessor :name
|
1256
|
-
# attr_reader :errors
|
1257
|
-
#
|
1258
|
-
# def validate!
|
1259
|
-
# errors.add(:name, :blank, message: "cannot be nil") if name.nil?
|
1260
|
-
# end
|
1261
|
-
#
|
1262
|
-
# # The following methods are needed to be minimally implemented
|
1263
|
-
#
|
1264
|
-
# def read_attribute_for_validation(attr)
|
1265
|
-
# send(attr)
|
1266
|
-
# end
|
1267
|
-
#
|
1268
|
-
# def self.human_attribute_name(attr, options = {})
|
1269
|
-
# attr
|
1270
|
-
# end
|
1271
|
-
#
|
1272
|
-
# def self.lookup_ancestors
|
1273
|
-
# [self]
|
1274
|
-
# end
|
1275
|
-
# end
|
1276
|
-
#
|
1277
|
-
# The last three methods are required in your object for +Errors+ to be
|
1278
|
-
# able to generate error messages correctly and also handle multiple
|
1279
|
-
# languages. Of course, if you extend your object with <tt>ActiveModel::Translation</tt>
|
1280
|
-
# you will not need to implement the last two. Likewise, using
|
1281
|
-
# <tt>ActiveModel::Validations</tt> will handle the validation related methods
|
1282
|
-
# for you.
|
1283
|
-
#
|
1284
|
-
# The above allows you to do:
|
1285
|
-
#
|
1286
|
-
# person = Person.new
|
1287
|
-
# person.validate! # => ["cannot be nil"]
|
1288
|
-
# person.errors.full_messages # => ["name cannot be nil"]
|
1289
|
-
# # etc..
|
1290
|
-
class Errors
|
1291
|
-
include Enumerable[untyped, untyped]
|
1292
|
-
|
1293
|
-
CALLBACKS_OPTIONS: ::Array[untyped]
|
1294
|
-
|
1295
|
-
MESSAGE_OPTIONS: ::Array[untyped]
|
1296
|
-
|
1297
|
-
attr_accessor i18n_customize_full_message: untyped
|
1298
|
-
|
1299
|
-
attr_reader messages: untyped
|
1300
|
-
|
1301
|
-
attr_reader details: untyped
|
1302
|
-
|
1303
|
-
# Pass in the instance of the object that is using the errors object.
|
1304
|
-
#
|
1305
|
-
# class Person
|
1306
|
-
# def initialize
|
1307
|
-
# @errors = ActiveModel::Errors.new(self)
|
1308
|
-
# end
|
1309
|
-
# end
|
1310
|
-
def initialize: (untyped base) -> untyped
|
1311
|
-
|
1312
|
-
def initialize_dup: (untyped other) -> untyped
|
1313
|
-
|
1314
|
-
def copy!: (untyped other) -> untyped
|
1315
|
-
|
1316
|
-
# Merges the errors from <tt>other</tt>.
|
1317
|
-
#
|
1318
|
-
# other - The ActiveModel::Errors instance.
|
1319
|
-
#
|
1320
|
-
# Examples
|
1321
|
-
#
|
1322
|
-
# person.errors.merge!(other)
|
1323
|
-
def merge!: (untyped other) -> untyped
|
1324
|
-
|
1325
|
-
# Removes all errors except the given keys. Returns a hash containing the removed errors.
|
1326
|
-
#
|
1327
|
-
# person.errors.keys # => [:name, :age, :gender, :city]
|
1328
|
-
# person.errors.slice!(:age, :gender) # => { :name=>["cannot be nil"], :city=>["cannot be nil"] }
|
1329
|
-
# person.errors.keys # => [:age, :gender]
|
1330
|
-
def slice!: (*untyped keys) -> untyped
|
1331
|
-
|
1332
|
-
# Clear the error messages.
|
1333
|
-
#
|
1334
|
-
# person.errors.full_messages # => ["name cannot be nil"]
|
1335
|
-
# person.errors.clear
|
1336
|
-
# person.errors.full_messages # => []
|
1337
|
-
def clear: () -> untyped
|
1338
|
-
|
1339
|
-
# Returns +true+ if the error messages include an error for the given key
|
1340
|
-
# +attribute+, +false+ otherwise.
|
1341
|
-
#
|
1342
|
-
# person.errors.messages # => {:name=>["cannot be nil"]}
|
1343
|
-
# person.errors.include?(:name) # => true
|
1344
|
-
# person.errors.include?(:age) # => false
|
1345
|
-
def include?: (untyped attribute) -> untyped
|
1346
|
-
|
1347
|
-
# Delete messages for +key+. Returns the deleted messages.
|
1348
|
-
#
|
1349
|
-
# person.errors[:name] # => ["cannot be nil"]
|
1350
|
-
# person.errors.delete(:name) # => ["cannot be nil"]
|
1351
|
-
# person.errors[:name] # => []
|
1352
|
-
def delete: (untyped key) -> untyped
|
1353
|
-
|
1354
|
-
# When passed a symbol or a name of a method, returns an array of errors
|
1355
|
-
# for the method.
|
1356
|
-
#
|
1357
|
-
# person.errors[:name] # => ["cannot be nil"]
|
1358
|
-
# person.errors['name'] # => ["cannot be nil"]
|
1359
|
-
def []: (untyped attribute) -> untyped
|
1360
|
-
|
1361
|
-
# Iterates through each error key, value pair in the error messages hash.
|
1362
|
-
# Yields the attribute and the error for that attribute. If the attribute
|
1363
|
-
# has more than one error message, yields once for each error message.
|
1364
|
-
#
|
1365
|
-
# person.errors.add(:name, :blank, message: "can't be blank")
|
1366
|
-
# person.errors.each do |attribute, error|
|
1367
|
-
# # Will yield :name and "can't be blank"
|
1368
|
-
# end
|
1369
|
-
#
|
1370
|
-
# person.errors.add(:name, :not_specified, message: "must be specified")
|
1371
|
-
# person.errors.each do |attribute, error|
|
1372
|
-
# # Will yield :name and "can't be blank"
|
1373
|
-
# # then yield :name and "must be specified"
|
1374
|
-
# end
|
1375
|
-
def each: () { (untyped, untyped) -> untyped } -> untyped
|
1376
|
-
|
1377
|
-
# Returns the number of error messages.
|
1378
|
-
#
|
1379
|
-
# person.errors.add(:name, :blank, message: "can't be blank")
|
1380
|
-
# person.errors.size # => 1
|
1381
|
-
# person.errors.add(:name, :not_specified, message: "must be specified")
|
1382
|
-
# person.errors.size # => 2
|
1383
|
-
def size: () -> untyped
|
1384
|
-
|
1385
|
-
# Returns all message values.
|
1386
|
-
#
|
1387
|
-
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
|
1388
|
-
# person.errors.values # => [["cannot be nil", "must be specified"]]
|
1389
|
-
def values: () -> untyped
|
1390
|
-
|
1391
|
-
# Returns all message keys.
|
1392
|
-
#
|
1393
|
-
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
|
1394
|
-
# person.errors.keys # => [:name]
|
1395
|
-
def keys: () -> untyped
|
1396
|
-
|
1397
|
-
# Returns +true+ if no errors are found, +false+ otherwise.
|
1398
|
-
# If the error message is a string it can be empty.
|
1399
|
-
#
|
1400
|
-
# person.errors.full_messages # => ["name cannot be nil"]
|
1401
|
-
# person.errors.empty? # => false
|
1402
|
-
def empty?: () -> untyped
|
1403
|
-
|
1404
|
-
# Returns an xml formatted representation of the Errors hash.
|
1405
|
-
#
|
1406
|
-
# person.errors.add(:name, :blank, message: "can't be blank")
|
1407
|
-
# person.errors.add(:name, :not_specified, message: "must be specified")
|
1408
|
-
# person.errors.to_xml
|
1409
|
-
# # =>
|
1410
|
-
# # <?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
1411
|
-
# # <errors>
|
1412
|
-
# # <error>name can't be blank</error>
|
1413
|
-
# # <error>name must be specified</error>
|
1414
|
-
# # </errors>
|
1415
|
-
def to_xml: (?::Hash[untyped, untyped] options) -> untyped
|
1416
|
-
|
1417
|
-
# Returns a Hash that can be used as the JSON representation for this
|
1418
|
-
# object. You can pass the <tt>:full_messages</tt> option. This determines
|
1419
|
-
# if the json object should contain full messages or not (false by default).
|
1420
|
-
#
|
1421
|
-
# person.errors.as_json # => {:name=>["cannot be nil"]}
|
1422
|
-
# person.errors.as_json(full_messages: true) # => {:name=>["name cannot be nil"]}
|
1423
|
-
def as_json: (?untyped? options) -> untyped
|
1424
|
-
|
1425
|
-
# Returns a Hash of attributes with their error messages. If +full_messages+
|
1426
|
-
# is +true+, it will contain full messages (see +full_message+).
|
1427
|
-
#
|
1428
|
-
# person.errors.to_hash # => {:name=>["cannot be nil"]}
|
1429
|
-
# person.errors.to_hash(true) # => {:name=>["name cannot be nil"]}
|
1430
|
-
def to_hash: (?bool full_messages) -> untyped
|
1431
|
-
|
1432
|
-
# Adds +message+ to the error messages and used validator type to +details+ on +attribute+.
|
1433
|
-
# More than one error can be added to the same +attribute+.
|
1434
|
-
# If no +message+ is supplied, <tt>:invalid</tt> is assumed.
|
1435
|
-
#
|
1436
|
-
# person.errors.add(:name)
|
1437
|
-
# # => ["is invalid"]
|
1438
|
-
# person.errors.add(:name, :not_implemented, message: "must be implemented")
|
1439
|
-
# # => ["is invalid", "must be implemented"]
|
1440
|
-
#
|
1441
|
-
# person.errors.messages
|
1442
|
-
# # => {:name=>["is invalid", "must be implemented"]}
|
1443
|
-
#
|
1444
|
-
# person.errors.details
|
1445
|
-
# # => {:name=>[{error: :not_implemented}, {error: :invalid}]}
|
1446
|
-
#
|
1447
|
-
# If +message+ is a symbol, it will be translated using the appropriate
|
1448
|
-
# scope (see +generate_message+).
|
1449
|
-
#
|
1450
|
-
# If +message+ is a proc, it will be called, allowing for things like
|
1451
|
-
# <tt>Time.now</tt> to be used within an error.
|
1452
|
-
#
|
1453
|
-
# If the <tt>:strict</tt> option is set to +true+, it will raise
|
1454
|
-
# ActiveModel::StrictValidationFailed instead of adding the error.
|
1455
|
-
# <tt>:strict</tt> option can also be set to any other exception.
|
1456
|
-
#
|
1457
|
-
# person.errors.add(:name, :invalid, strict: true)
|
1458
|
-
# # => ActiveModel::StrictValidationFailed: Name is invalid
|
1459
|
-
# person.errors.add(:name, :invalid, strict: NameIsInvalid)
|
1460
|
-
# # => NameIsInvalid: Name is invalid
|
1461
|
-
#
|
1462
|
-
# person.errors.messages # => {}
|
1463
|
-
#
|
1464
|
-
# +attribute+ should be set to <tt>:base</tt> if the error is not
|
1465
|
-
# directly associated with a single attribute.
|
1466
|
-
#
|
1467
|
-
# person.errors.add(:base, :name_or_email_blank,
|
1468
|
-
# message: "either name or email must be present")
|
1469
|
-
# person.errors.messages
|
1470
|
-
# # => {:base=>["either name or email must be present"]}
|
1471
|
-
# person.errors.details
|
1472
|
-
# # => {:base=>[{error: :name_or_email_blank}]}
|
1473
|
-
def add: (untyped attribute, ?::Symbol message, ?::Hash[untyped, untyped] options) -> untyped
|
1474
|
-
|
1475
|
-
# Returns +true+ if an error on the attribute with the given message is
|
1476
|
-
# present, or +false+ otherwise. +message+ is treated the same as for +add+.
|
1477
|
-
#
|
1478
|
-
# person.errors.add :name, :blank
|
1479
|
-
# person.errors.added? :name, :blank # => true
|
1480
|
-
# person.errors.added? :name, "can't be blank" # => true
|
1481
|
-
#
|
1482
|
-
# If the error message requires options, then it returns +true+ with
|
1483
|
-
# the correct options, or +false+ with incorrect or missing options.
|
1484
|
-
#
|
1485
|
-
# person.errors.add :name, :too_long, { count: 25 }
|
1486
|
-
# person.errors.added? :name, :too_long, count: 25 # => true
|
1487
|
-
# person.errors.added? :name, "is too long (maximum is 25 characters)" # => true
|
1488
|
-
# person.errors.added? :name, :too_long, count: 24 # => false
|
1489
|
-
# person.errors.added? :name, :too_long # => false
|
1490
|
-
# person.errors.added? :name, "is too long" # => false
|
1491
|
-
def added?: (untyped attribute, ?::Symbol message, ?::Hash[untyped, untyped] options) -> untyped
|
1492
|
-
|
1493
|
-
# Returns +true+ if an error on the attribute with the given message is
|
1494
|
-
# present, or +false+ otherwise. +message+ is treated the same as for +add+.
|
1495
|
-
#
|
1496
|
-
# person.errors.add :age
|
1497
|
-
# person.errors.add :name, :too_long, { count: 25 }
|
1498
|
-
# person.errors.of_kind? :age # => true
|
1499
|
-
# person.errors.of_kind? :name # => false
|
1500
|
-
# person.errors.of_kind? :name, :too_long # => true
|
1501
|
-
# person.errors.of_kind? :name, "is too long (maximum is 25 characters)" # => true
|
1502
|
-
# person.errors.of_kind? :name, :not_too_long # => false
|
1503
|
-
# person.errors.of_kind? :name, "is too long" # => false
|
1504
|
-
def of_kind?: (untyped attribute, ?::Symbol message) -> untyped
|
1505
|
-
|
1506
|
-
# Returns all the full error messages in an array.
|
1507
|
-
#
|
1508
|
-
# class Person
|
1509
|
-
# validates_presence_of :name, :address, :email
|
1510
|
-
# validates_length_of :name, in: 5..30
|
1511
|
-
# end
|
1512
|
-
#
|
1513
|
-
# person = Person.create(address: '123 First St.')
|
1514
|
-
# person.errors.full_messages
|
1515
|
-
# # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"]
|
1516
|
-
def full_messages: () -> untyped
|
1517
|
-
|
1518
|
-
# Returns all the full error messages for a given attribute in an array.
|
1519
|
-
#
|
1520
|
-
# class Person
|
1521
|
-
# validates_presence_of :name, :email
|
1522
|
-
# validates_length_of :name, in: 5..30
|
1523
|
-
# end
|
1524
|
-
#
|
1525
|
-
# person = Person.create()
|
1526
|
-
# person.errors.full_messages_for(:name)
|
1527
|
-
# # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
|
1528
|
-
def full_messages_for: (untyped attribute) -> untyped
|
1529
|
-
|
1530
|
-
# Returns a full message for a given attribute.
|
1531
|
-
#
|
1532
|
-
# person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
|
1533
|
-
#
|
1534
|
-
# The `"%{attribute} %{message}"` error format can be overridden with either
|
1535
|
-
#
|
1536
|
-
# * <tt>activemodel.errors.models.person/contacts/addresses.attributes.street.format</tt>
|
1537
|
-
# * <tt>activemodel.errors.models.person/contacts/addresses.format</tt>
|
1538
|
-
# * <tt>activemodel.errors.models.person.attributes.name.format</tt>
|
1539
|
-
# * <tt>activemodel.errors.models.person.format</tt>
|
1540
|
-
# * <tt>errors.format</tt>
|
1541
|
-
def full_message: (untyped attribute, untyped message) -> untyped
|
1542
|
-
|
1543
|
-
# Translates an error message in its default scope
|
1544
|
-
# (<tt>activemodel.errors.messages</tt>).
|
1545
|
-
#
|
1546
|
-
# Error messages are first looked up in <tt>activemodel.errors.models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>,
|
1547
|
-
# if it's not there, it's looked up in <tt>activemodel.errors.models.MODEL.MESSAGE</tt> and if
|
1548
|
-
# that is not there also, it returns the translation of the default message
|
1549
|
-
# (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model
|
1550
|
-
# name, translated attribute name and the value are available for
|
1551
|
-
# interpolation.
|
1552
|
-
#
|
1553
|
-
# When using inheritance in your models, it will check all the inherited
|
1554
|
-
# models too, but only if the model itself hasn't been found. Say you have
|
1555
|
-
# <tt>class Admin < User; end</tt> and you wanted the translation for
|
1556
|
-
# the <tt>:blank</tt> error message for the <tt>title</tt> attribute,
|
1557
|
-
# it looks for these translations:
|
1558
|
-
#
|
1559
|
-
# * <tt>activemodel.errors.models.admin.attributes.title.blank</tt>
|
1560
|
-
# * <tt>activemodel.errors.models.admin.blank</tt>
|
1561
|
-
# * <tt>activemodel.errors.models.user.attributes.title.blank</tt>
|
1562
|
-
# * <tt>activemodel.errors.models.user.blank</tt>
|
1563
|
-
# * any default you provided through the +options+ hash (in the <tt>activemodel.errors</tt> scope)
|
1564
|
-
# * <tt>activemodel.errors.messages.blank</tt>
|
1565
|
-
# * <tt>errors.attributes.title.blank</tt>
|
1566
|
-
# * <tt>errors.messages.blank</tt>
|
1567
|
-
def generate_message: (untyped attribute, ?::Symbol `type`, ?::Hash[untyped, untyped] options) -> untyped
|
1568
|
-
|
1569
|
-
def marshal_dump: () -> ::Array[untyped]
|
1570
|
-
|
1571
|
-
def marshal_load: (untyped array) -> untyped
|
1572
|
-
|
1573
|
-
def init_with: (untyped coder) -> untyped
|
1574
|
-
|
1575
|
-
def normalize_message: (untyped attribute, untyped message, untyped options) -> untyped
|
1576
|
-
|
1577
|
-
def normalize_detail: (untyped message, untyped options) -> untyped
|
1578
|
-
|
1579
|
-
def without_default_proc: (untyped hash) -> untyped
|
1580
|
-
|
1581
|
-
def apply_default_array: (untyped hash) -> untyped
|
1582
|
-
end
|
1583
|
-
|
1584
|
-
# Raised when a validation cannot be corrected by end users and are considered
|
1585
|
-
# exceptional.
|
1586
|
-
#
|
1587
|
-
# class Person
|
1588
|
-
# include ActiveModel::Validations
|
1589
|
-
#
|
1590
|
-
# attr_accessor :name
|
1591
|
-
#
|
1592
|
-
# validates_presence_of :name, strict: true
|
1593
|
-
# end
|
1594
|
-
#
|
1595
|
-
# person = Person.new
|
1596
|
-
# person.name = nil
|
1597
|
-
# person.valid?
|
1598
|
-
# # => ActiveModel::StrictValidationFailed: Name can't be blank
|
1599
|
-
class StrictValidationFailed < StandardError
|
1600
|
-
end
|
1601
|
-
|
1602
|
-
# Raised when attribute values are out of range.
|
1603
|
-
class RangeError < ::RangeError
|
1604
|
-
end
|
1605
|
-
|
1606
|
-
# Raised when unknown attributes are supplied via mass assignment.
|
1607
|
-
#
|
1608
|
-
# class Person
|
1609
|
-
# include ActiveModel::AttributeAssignment
|
1610
|
-
# include ActiveModel::Validations
|
1611
|
-
# end
|
1612
|
-
#
|
1613
|
-
# person = Person.new
|
1614
|
-
# person.assign_attributes(name: 'Gorby')
|
1615
|
-
# # => ActiveModel::UnknownAttributeError: unknown attribute 'name' for Person.
|
1616
|
-
class UnknownAttributeError[T] < NoMethodError[T]
|
1617
|
-
attr_reader record: untyped
|
1618
|
-
|
1619
|
-
attr_reader attribute: untyped
|
1620
|
-
|
1621
|
-
def initialize: (untyped record, untyped attribute) -> untyped
|
1622
|
-
end
|
1623
|
-
end
|
1624
|
-
|
1625
|
-
module ActiveModel
|
1626
|
-
# Raised when forbidden attributes are used for mass assignment.
|
1627
|
-
#
|
1628
|
-
# class Person < ActiveRecord::Base
|
1629
|
-
# end
|
1630
|
-
#
|
1631
|
-
# params = ActionController::Parameters.new(name: 'Bob')
|
1632
|
-
# Person.new(params)
|
1633
|
-
# # => ActiveModel::ForbiddenAttributesError
|
1634
|
-
#
|
1635
|
-
# params.permit!
|
1636
|
-
# Person.new(params)
|
1637
|
-
# # => #<Person id: nil, name: "Bob">
|
1638
|
-
class ForbiddenAttributesError < StandardError
|
1639
|
-
end
|
1640
|
-
|
1641
|
-
module ForbiddenAttributesProtection
|
1642
|
-
def sanitize_for_mass_assignment: (untyped attributes) -> untyped
|
1643
|
-
end
|
1644
|
-
end
|
1645
|
-
|
1646
|
-
module ActiveModel
|
1647
|
-
# Returns the version of the currently loaded \Active \Model as a <tt>Gem::Version</tt>
|
1648
|
-
def self.gem_version: () -> Gem::Version
|
1649
|
-
|
1650
|
-
module VERSION
|
1651
|
-
MAJOR: ::Integer
|
1652
|
-
|
1653
|
-
MINOR: ::Integer
|
1654
|
-
|
1655
|
-
TINY: ::Integer
|
1656
|
-
|
1657
|
-
PRE: ::String
|
1658
|
-
|
1659
|
-
STRING: untyped
|
1660
|
-
end
|
1661
|
-
end
|
1662
|
-
|
1663
|
-
module ActiveModel
|
1664
|
-
module Lint
|
1665
|
-
# == Active \Model \Lint \Tests
|
1666
|
-
#
|
1667
|
-
# You can test whether an object is compliant with the Active \Model API by
|
1668
|
-
# including <tt>ActiveModel::Lint::Tests</tt> in your TestCase. It will
|
1669
|
-
# include tests that tell you whether your object is fully compliant,
|
1670
|
-
# or if not, which aspects of the API are not implemented.
|
1671
|
-
#
|
1672
|
-
# Note an object is not required to implement all APIs in order to work
|
1673
|
-
# with Action Pack. This module only intends to provide guidance in case
|
1674
|
-
# you want all features out of the box.
|
1675
|
-
#
|
1676
|
-
# These tests do not attempt to determine the semantic correctness of the
|
1677
|
-
# returned values. For instance, you could implement <tt>valid?</tt> to
|
1678
|
-
# always return +true+, and the tests would pass. It is up to you to ensure
|
1679
|
-
# that the values are semantically meaningful.
|
1680
|
-
#
|
1681
|
-
# Objects you pass in are expected to return a compliant object from a call
|
1682
|
-
# to <tt>to_model</tt>. It is perfectly fine for <tt>to_model</tt> to return
|
1683
|
-
# +self+.
|
1684
|
-
module Tests
|
1685
|
-
# Passes if the object's model responds to <tt>to_key</tt> and if calling
|
1686
|
-
# this method returns +nil+ when the object is not persisted.
|
1687
|
-
# Fails otherwise.
|
1688
|
-
#
|
1689
|
-
# <tt>to_key</tt> returns an Enumerable of all (primary) key attributes
|
1690
|
-
# of the model, and is used to a generate unique DOM id for the object.
|
1691
|
-
def test_to_key: () -> untyped
|
1692
|
-
|
1693
|
-
# Passes if the object's model responds to <tt>to_param</tt> and if
|
1694
|
-
# calling this method returns +nil+ when the object is not persisted.
|
1695
|
-
# Fails otherwise.
|
1696
|
-
#
|
1697
|
-
# <tt>to_param</tt> is used to represent the object's key in URLs.
|
1698
|
-
# Implementers can decide to either raise an exception or provide a
|
1699
|
-
# default in case the record uses a composite primary key. There are no
|
1700
|
-
# tests for this behavior in lint because it doesn't make sense to force
|
1701
|
-
# any of the possible implementation strategies on the implementer.
|
1702
|
-
def test_to_param: () -> untyped
|
1703
|
-
|
1704
|
-
# Passes if the object's model responds to <tt>to_partial_path</tt> and if
|
1705
|
-
# calling this method returns a string. Fails otherwise.
|
1706
|
-
#
|
1707
|
-
# <tt>to_partial_path</tt> is used for looking up partials. For example,
|
1708
|
-
# a BlogPost model might return "blog_posts/blog_post".
|
1709
|
-
def test_to_partial_path: () -> untyped
|
1710
|
-
|
1711
|
-
# Passes if the object's model responds to <tt>persisted?</tt> and if
|
1712
|
-
# calling this method returns either +true+ or +false+. Fails otherwise.
|
1713
|
-
#
|
1714
|
-
# <tt>persisted?</tt> is used when calculating the URL for an object.
|
1715
|
-
# If the object is not persisted, a form for that object, for instance,
|
1716
|
-
# will route to the create action. If it is persisted, a form for the
|
1717
|
-
# object will route to the update action.
|
1718
|
-
def test_persisted?: () -> untyped
|
1719
|
-
|
1720
|
-
# Passes if the object's model responds to <tt>model_name</tt> both as
|
1721
|
-
# an instance method and as a class method, and if calling this method
|
1722
|
-
# returns a string with some convenience methods: <tt>:human</tt>,
|
1723
|
-
# <tt>:singular</tt> and <tt>:plural</tt>.
|
1724
|
-
#
|
1725
|
-
# Check ActiveModel::Naming for more information.
|
1726
|
-
def test_model_naming: () -> untyped
|
1727
|
-
|
1728
|
-
# Passes if the object's model responds to <tt>errors</tt> and if calling
|
1729
|
-
# <tt>[](attribute)</tt> on the result of this method returns an array.
|
1730
|
-
# Fails otherwise.
|
1731
|
-
#
|
1732
|
-
# <tt>errors[attribute]</tt> is used to retrieve the errors of a model
|
1733
|
-
# for a given attribute. If errors are present, the method should return
|
1734
|
-
# an array of strings that are the errors for the attribute in question.
|
1735
|
-
# If localization is used, the strings should be localized for the current
|
1736
|
-
# locale. If no error is present, the method should return an empty array.
|
1737
|
-
def test_errors_aref: () -> untyped
|
1738
|
-
|
1739
|
-
def model: () -> untyped
|
1740
|
-
|
1741
|
-
def assert_boolean: (untyped result, untyped name) -> untyped
|
1742
|
-
end
|
1743
|
-
end
|
1744
|
-
end
|
1745
|
-
|
1746
|
-
module ActiveModel
|
1747
|
-
# == Active \Model \Basic \Model
|
1748
|
-
#
|
1749
|
-
# Includes the required interface for an object to interact with
|
1750
|
-
# Action Pack and Action View, using different Active Model modules.
|
1751
|
-
# It includes model name introspections, conversions, translations and
|
1752
|
-
# validations. Besides that, it allows you to initialize the object with a
|
1753
|
-
# hash of attributes, pretty much like Active Record does.
|
1754
|
-
#
|
1755
|
-
# A minimal implementation could be:
|
1756
|
-
#
|
1757
|
-
# class Person
|
1758
|
-
# include ActiveModel::Model
|
1759
|
-
# attr_accessor :name, :age
|
1760
|
-
# end
|
1761
|
-
#
|
1762
|
-
# person = Person.new(name: 'bob', age: '18')
|
1763
|
-
# person.name # => "bob"
|
1764
|
-
# person.age # => "18"
|
1765
|
-
#
|
1766
|
-
# Note that, by default, <tt>ActiveModel::Model</tt> implements <tt>persisted?</tt>
|
1767
|
-
# to return +false+, which is the most common case. You may want to override
|
1768
|
-
# it in your class to simulate a different scenario:
|
1769
|
-
#
|
1770
|
-
# class Person
|
1771
|
-
# include ActiveModel::Model
|
1772
|
-
# attr_accessor :id, :name
|
1773
|
-
#
|
1774
|
-
# def persisted?
|
1775
|
-
# self.id == 1
|
1776
|
-
# end
|
1777
|
-
# end
|
1778
|
-
#
|
1779
|
-
# person = Person.new(id: 1, name: 'bob')
|
1780
|
-
# person.persisted? # => true
|
1781
|
-
#
|
1782
|
-
# Also, if for some reason you need to run code on <tt>initialize</tt>, make
|
1783
|
-
# sure you call +super+ if you want the attributes hash initialization to
|
1784
|
-
# happen.
|
1785
|
-
#
|
1786
|
-
# class Person
|
1787
|
-
# include ActiveModel::Model
|
1788
|
-
# attr_accessor :id, :name, :omg
|
1789
|
-
#
|
1790
|
-
# def initialize(attributes={})
|
1791
|
-
# super
|
1792
|
-
# @omg ||= true
|
1793
|
-
# end
|
1794
|
-
# end
|
1795
|
-
#
|
1796
|
-
# person = Person.new(id: 1, name: 'bob')
|
1797
|
-
# person.omg # => true
|
1798
|
-
#
|
1799
|
-
# For more detailed information on other functionalities available, please
|
1800
|
-
# refer to the specific modules included in <tt>ActiveModel::Model</tt>
|
1801
|
-
# (see below).
|
1802
|
-
module Model
|
1803
|
-
extend ActiveSupport::Concern
|
1804
|
-
|
1805
|
-
include ActiveModel::AttributeAssignment
|
1806
|
-
|
1807
|
-
include ActiveModel::Validations
|
1808
|
-
|
1809
|
-
extend ::ActiveModel::Validations::ClassMethods
|
1810
|
-
|
1811
|
-
include ActiveModel::Conversion
|
1812
|
-
|
1813
|
-
extend ::ActiveModel::Conversion::ClassMethods
|
1814
|
-
|
1815
|
-
extend ActiveModel::Naming
|
1816
|
-
|
1817
|
-
extend ActiveModel::Translation
|
1818
|
-
|
1819
|
-
# Initializes a new model with the given +params+.
|
1820
|
-
#
|
1821
|
-
# class Person
|
1822
|
-
# include ActiveModel::Model
|
1823
|
-
# attr_accessor :name, :age
|
1824
|
-
# end
|
1825
|
-
#
|
1826
|
-
# person = Person.new(name: 'bob', age: '18')
|
1827
|
-
# person.name # => "bob"
|
1828
|
-
# person.age # => "18"
|
1829
|
-
def initialize: (?::Hash[untyped, untyped] attributes) -> untyped
|
1830
|
-
|
1831
|
-
# Indicates if the model is persisted. Default is +false+.
|
1832
|
-
#
|
1833
|
-
# class Person
|
1834
|
-
# include ActiveModel::Model
|
1835
|
-
# attr_accessor :id, :name
|
1836
|
-
# end
|
1837
|
-
#
|
1838
|
-
# person = Person.new(id: 1, name: 'bob')
|
1839
|
-
# person.persisted? # => false
|
1840
|
-
def persisted?: () -> ::FalseClass
|
1841
|
-
end
|
1842
|
-
end
|
1843
|
-
|
1844
|
-
module ActiveModel
|
1845
|
-
class Name
|
1846
|
-
include Comparable
|
1847
|
-
|
1848
|
-
attr_reader singular: untyped
|
1849
|
-
|
1850
|
-
attr_reader plural: untyped
|
1851
|
-
|
1852
|
-
attr_reader element: untyped
|
1853
|
-
|
1854
|
-
attr_reader collection: untyped
|
1855
|
-
|
1856
|
-
attr_reader singular_route_key: untyped
|
1857
|
-
|
1858
|
-
attr_reader route_key: untyped
|
1859
|
-
|
1860
|
-
attr_reader param_key: untyped
|
1861
|
-
|
1862
|
-
attr_reader i18n_key: untyped
|
1863
|
-
|
1864
|
-
attr_reader name: untyped
|
1865
|
-
|
1866
|
-
# Returns a new ActiveModel::Name instance. By default, the +namespace+
|
1867
|
-
# and +name+ option will take the namespace and name of the given class
|
1868
|
-
# respectively.
|
1869
|
-
#
|
1870
|
-
# module Foo
|
1871
|
-
# class Bar
|
1872
|
-
# end
|
1873
|
-
# end
|
1874
|
-
#
|
1875
|
-
# ActiveModel::Name.new(Foo::Bar).to_s
|
1876
|
-
# # => "Foo::Bar"
|
1877
|
-
def initialize: (untyped klass, ?untyped? namespace, ?untyped? name) -> untyped
|
1878
|
-
|
1879
|
-
# Transform the model name into a more human format, using I18n. By default,
|
1880
|
-
# it will underscore then humanize the class name.
|
1881
|
-
#
|
1882
|
-
# class BlogPost
|
1883
|
-
# extend ActiveModel::Naming
|
1884
|
-
# end
|
1885
|
-
#
|
1886
|
-
# BlogPost.model_name.human # => "Blog post"
|
1887
|
-
#
|
1888
|
-
# Specify +options+ with additional translating options.
|
1889
|
-
def human: (?::Hash[untyped, untyped] options) -> untyped
|
1890
|
-
|
1891
|
-
def _singularize: (untyped string) -> untyped
|
1892
|
-
end
|
1893
|
-
|
1894
|
-
# == Active \Model \Naming
|
1895
|
-
#
|
1896
|
-
# Creates a +model_name+ method on your object.
|
1897
|
-
#
|
1898
|
-
# To implement, just extend ActiveModel::Naming in your object:
|
1899
|
-
#
|
1900
|
-
# class BookCover
|
1901
|
-
# extend ActiveModel::Naming
|
1902
|
-
# end
|
1903
|
-
#
|
1904
|
-
# BookCover.model_name.name # => "BookCover"
|
1905
|
-
# BookCover.model_name.human # => "Book cover"
|
1906
|
-
#
|
1907
|
-
# BookCover.model_name.i18n_key # => :book_cover
|
1908
|
-
# BookModule::BookCover.model_name.i18n_key # => :"book_module/book_cover"
|
1909
|
-
#
|
1910
|
-
# Providing the functionality that ActiveModel::Naming provides in your object
|
1911
|
-
# is required to pass the \Active \Model Lint test. So either extending the
|
1912
|
-
# provided method below, or rolling your own is required.
|
1913
|
-
module Naming
|
1914
|
-
def self.extended: (untyped base) -> untyped
|
1915
|
-
|
1916
|
-
# Returns an ActiveModel::Name object for module. It can be
|
1917
|
-
# used to retrieve all kinds of naming-related information
|
1918
|
-
# (See ActiveModel::Name for more information).
|
1919
|
-
#
|
1920
|
-
# class Person
|
1921
|
-
# extend ActiveModel::Naming
|
1922
|
-
# end
|
1923
|
-
#
|
1924
|
-
# Person.model_name.name # => "Person"
|
1925
|
-
# Person.model_name.class # => ActiveModel::Name
|
1926
|
-
# Person.model_name.singular # => "person"
|
1927
|
-
# Person.model_name.plural # => "people"
|
1928
|
-
def model_name: () -> untyped
|
1929
|
-
|
1930
|
-
# Returns the plural class name of a record or class.
|
1931
|
-
#
|
1932
|
-
# ActiveModel::Naming.plural(post) # => "posts"
|
1933
|
-
# ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
|
1934
|
-
def self.plural: (untyped record_or_class) -> untyped
|
1935
|
-
|
1936
|
-
# Returns the singular class name of a record or class.
|
1937
|
-
#
|
1938
|
-
# ActiveModel::Naming.singular(post) # => "post"
|
1939
|
-
# ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
|
1940
|
-
def self.singular: (untyped record_or_class) -> untyped
|
1941
|
-
|
1942
|
-
# Identifies whether the class name of a record or class is uncountable.
|
1943
|
-
#
|
1944
|
-
# ActiveModel::Naming.uncountable?(Sheep) # => true
|
1945
|
-
# ActiveModel::Naming.uncountable?(Post) # => false
|
1946
|
-
def self.uncountable?: (untyped record_or_class) -> untyped
|
1947
|
-
|
1948
|
-
# Returns string to use while generating route names. It differs for
|
1949
|
-
# namespaced models regarding whether it's inside isolated engine.
|
1950
|
-
#
|
1951
|
-
# # For isolated engine:
|
1952
|
-
# ActiveModel::Naming.singular_route_key(Blog::Post) # => "post"
|
1953
|
-
#
|
1954
|
-
# # For shared engine:
|
1955
|
-
# ActiveModel::Naming.singular_route_key(Blog::Post) # => "blog_post"
|
1956
|
-
def self.singular_route_key: (untyped record_or_class) -> untyped
|
1957
|
-
|
1958
|
-
# Returns string to use while generating route names. It differs for
|
1959
|
-
# namespaced models regarding whether it's inside isolated engine.
|
1960
|
-
#
|
1961
|
-
# # For isolated engine:
|
1962
|
-
# ActiveModel::Naming.route_key(Blog::Post) # => "posts"
|
1963
|
-
#
|
1964
|
-
# # For shared engine:
|
1965
|
-
# ActiveModel::Naming.route_key(Blog::Post) # => "blog_posts"
|
1966
|
-
#
|
1967
|
-
# The route key also considers if the noun is uncountable and, in
|
1968
|
-
# such cases, automatically appends _index.
|
1969
|
-
def self.route_key: (untyped record_or_class) -> untyped
|
1970
|
-
|
1971
|
-
# Returns string to use for params names. It differs for
|
1972
|
-
# namespaced models regarding whether it's inside isolated engine.
|
1973
|
-
#
|
1974
|
-
# # For isolated engine:
|
1975
|
-
# ActiveModel::Naming.param_key(Blog::Post) # => "post"
|
1976
|
-
#
|
1977
|
-
# # For shared engine:
|
1978
|
-
# ActiveModel::Naming.param_key(Blog::Post) # => "blog_post"
|
1979
|
-
def self.param_key: (untyped record_or_class) -> untyped
|
1980
|
-
|
1981
|
-
def self.model_name_from_record_or_class: (untyped record_or_class) -> untyped
|
1982
|
-
end
|
1983
|
-
end
|
1984
|
-
|
1985
|
-
module ActiveModel
|
1986
|
-
class Railtie < Rails::Railtie
|
1987
|
-
end
|
1988
|
-
end
|
1989
|
-
|
1990
|
-
module ActiveModel
|
1991
|
-
extend ActiveSupport::Autoload
|
1992
|
-
|
1993
|
-
module Serializers
|
1994
|
-
extend ActiveSupport::Autoload
|
1995
|
-
end
|
1996
|
-
|
1997
|
-
def self.eager_load!: () -> untyped
|
1998
|
-
end
|
1999
|
-
|
2000
|
-
module ActiveModel
|
2001
|
-
module SecurePassword
|
2002
|
-
extend ActiveSupport::Concern
|
2003
|
-
|
2004
|
-
# BCrypt hash function can handle maximum 72 bytes, and if we pass
|
2005
|
-
# password of length more than 72 bytes it ignores extra characters.
|
2006
|
-
# Hence need to put a restriction on password length.
|
2007
|
-
MAX_PASSWORD_LENGTH_ALLOWED: ::Integer
|
2008
|
-
|
2009
|
-
attr_accessor min_cost: untyped
|
2010
|
-
|
2011
|
-
module ClassMethods
|
2012
|
-
# Adds methods to set and authenticate against a BCrypt password.
|
2013
|
-
# This mechanism requires you to have a +XXX_digest+ attribute.
|
2014
|
-
# Where +XXX+ is the attribute name of your desired password.
|
2015
|
-
#
|
2016
|
-
# The following validations are added automatically:
|
2017
|
-
# * Password must be present on creation
|
2018
|
-
# * Password length should be less than or equal to 72 bytes
|
2019
|
-
# * Confirmation of password (using a +XXX_confirmation+ attribute)
|
2020
|
-
#
|
2021
|
-
# If confirmation validation is not needed, simply leave out the
|
2022
|
-
# value for +XXX_confirmation+ (i.e. don't provide a form field for
|
2023
|
-
# it). When this attribute has a +nil+ value, the validation will not be
|
2024
|
-
# triggered.
|
2025
|
-
#
|
2026
|
-
# For further customizability, it is possible to suppress the default
|
2027
|
-
# validations by passing <tt>validations: false</tt> as an argument.
|
2028
|
-
#
|
2029
|
-
# Add bcrypt (~> 3.1.7) to Gemfile to use #has_secure_password:
|
2030
|
-
#
|
2031
|
-
# gem 'bcrypt', '~> 3.1.7'
|
2032
|
-
#
|
2033
|
-
# Example using Active Record (which automatically includes ActiveModel::SecurePassword):
|
2034
|
-
#
|
2035
|
-
# # Schema: User(name:string, password_digest:string, recovery_password_digest:string)
|
2036
|
-
# class User < ActiveRecord::Base
|
2037
|
-
# has_secure_password
|
2038
|
-
# has_secure_password :recovery_password, validations: false
|
2039
|
-
# end
|
2040
|
-
#
|
2041
|
-
# user = User.new(name: 'david', password: '', password_confirmation: 'nomatch')
|
2042
|
-
# user.save # => false, password required
|
2043
|
-
# user.password = 'mUc3m00RsqyRe'
|
2044
|
-
# user.save # => false, confirmation doesn't match
|
2045
|
-
# user.password_confirmation = 'mUc3m00RsqyRe'
|
2046
|
-
# user.save # => true
|
2047
|
-
# user.recovery_password = "42password"
|
2048
|
-
# user.recovery_password_digest # => "$2a$04$iOfhwahFymCs5weB3BNH/uXkTG65HR.qpW.bNhEjFP3ftli3o5DQC"
|
2049
|
-
# user.save # => true
|
2050
|
-
# user.authenticate('notright') # => false
|
2051
|
-
# user.authenticate('mUc3m00RsqyRe') # => user
|
2052
|
-
# user.authenticate_recovery_password('42password') # => user
|
2053
|
-
# User.find_by(name: 'david').try(:authenticate, 'notright') # => false
|
2054
|
-
# User.find_by(name: 'david').try(:authenticate, 'mUc3m00RsqyRe') # => user
|
2055
|
-
def has_secure_password: (?::Symbol attribute, ?validations: bool validations) -> untyped
|
2056
|
-
end
|
2057
|
-
|
2058
|
-
class InstanceMethodsOnActivation < Module
|
2059
|
-
def initialize: (untyped attribute) -> untyped
|
2060
|
-
end
|
2061
|
-
end
|
2062
|
-
end
|
2063
|
-
|
2064
|
-
module ActiveModel
|
2065
|
-
# == Active \Model \Serialization
|
2066
|
-
#
|
2067
|
-
# Provides a basic serialization to a serializable_hash for your objects.
|
2068
|
-
#
|
2069
|
-
# A minimal implementation could be:
|
2070
|
-
#
|
2071
|
-
# class Person
|
2072
|
-
# include ActiveModel::Serialization
|
2073
|
-
#
|
2074
|
-
# attr_accessor :name
|
2075
|
-
#
|
2076
|
-
# def attributes
|
2077
|
-
# {'name' => nil}
|
2078
|
-
# end
|
2079
|
-
# end
|
2080
|
-
#
|
2081
|
-
# Which would provide you with:
|
2082
|
-
#
|
2083
|
-
# person = Person.new
|
2084
|
-
# person.serializable_hash # => {"name"=>nil}
|
2085
|
-
# person.name = "Bob"
|
2086
|
-
# person.serializable_hash # => {"name"=>"Bob"}
|
2087
|
-
#
|
2088
|
-
# An +attributes+ hash must be defined and should contain any attributes you
|
2089
|
-
# need to be serialized. Attributes must be strings, not symbols.
|
2090
|
-
# When called, serializable hash will use instance methods that match the name
|
2091
|
-
# of the attributes hash's keys. In order to override this behavior, take a look
|
2092
|
-
# at the private method +read_attribute_for_serialization+.
|
2093
|
-
#
|
2094
|
-
# ActiveModel::Serializers::JSON module automatically includes
|
2095
|
-
# the <tt>ActiveModel::Serialization</tt> module, so there is no need to
|
2096
|
-
# explicitly include <tt>ActiveModel::Serialization</tt>.
|
2097
|
-
#
|
2098
|
-
# A minimal implementation including JSON would be:
|
2099
|
-
#
|
2100
|
-
# class Person
|
2101
|
-
# include ActiveModel::Serializers::JSON
|
2102
|
-
#
|
2103
|
-
# attr_accessor :name
|
2104
|
-
#
|
2105
|
-
# def attributes
|
2106
|
-
# {'name' => nil}
|
2107
|
-
# end
|
2108
|
-
# end
|
2109
|
-
#
|
2110
|
-
# Which would provide you with:
|
2111
|
-
#
|
2112
|
-
# person = Person.new
|
2113
|
-
# person.serializable_hash # => {"name"=>nil}
|
2114
|
-
# person.as_json # => {"name"=>nil}
|
2115
|
-
# person.to_json # => "{\"name\":null}"
|
2116
|
-
#
|
2117
|
-
# person.name = "Bob"
|
2118
|
-
# person.serializable_hash # => {"name"=>"Bob"}
|
2119
|
-
# person.as_json # => {"name"=>"Bob"}
|
2120
|
-
# person.to_json # => "{\"name\":\"Bob\"}"
|
2121
|
-
#
|
2122
|
-
# Valid options are <tt>:only</tt>, <tt>:except</tt>, <tt>:methods</tt> and
|
2123
|
-
# <tt>:include</tt>. The following are all valid examples:
|
2124
|
-
#
|
2125
|
-
# person.serializable_hash(only: 'name')
|
2126
|
-
# person.serializable_hash(include: :address)
|
2127
|
-
# person.serializable_hash(include: { address: { only: 'city' }})
|
2128
|
-
module Serialization
|
2129
|
-
# Returns a serialized hash of your object.
|
2130
|
-
#
|
2131
|
-
# class Person
|
2132
|
-
# include ActiveModel::Serialization
|
2133
|
-
#
|
2134
|
-
# attr_accessor :name, :age
|
2135
|
-
#
|
2136
|
-
# def attributes
|
2137
|
-
# {'name' => nil, 'age' => nil}
|
2138
|
-
# end
|
2139
|
-
#
|
2140
|
-
# def capitalized_name
|
2141
|
-
# name.capitalize
|
2142
|
-
# end
|
2143
|
-
# end
|
2144
|
-
#
|
2145
|
-
# person = Person.new
|
2146
|
-
# person.name = 'bob'
|
2147
|
-
# person.age = 22
|
2148
|
-
# person.serializable_hash # => {"name"=>"bob", "age"=>22}
|
2149
|
-
# person.serializable_hash(only: :name) # => {"name"=>"bob"}
|
2150
|
-
# person.serializable_hash(except: :name) # => {"age"=>22}
|
2151
|
-
# person.serializable_hash(methods: :capitalized_name)
|
2152
|
-
# # => {"name"=>"bob", "age"=>22, "capitalized_name"=>"Bob"}
|
2153
|
-
#
|
2154
|
-
# Example with <tt>:include</tt> option
|
2155
|
-
#
|
2156
|
-
# class User
|
2157
|
-
# include ActiveModel::Serializers::JSON
|
2158
|
-
# attr_accessor :name, :notes # Emulate has_many :notes
|
2159
|
-
# def attributes
|
2160
|
-
# {'name' => nil}
|
2161
|
-
# end
|
2162
|
-
# end
|
2163
|
-
#
|
2164
|
-
# class Note
|
2165
|
-
# include ActiveModel::Serializers::JSON
|
2166
|
-
# attr_accessor :title, :text
|
2167
|
-
# def attributes
|
2168
|
-
# {'title' => nil, 'text' => nil}
|
2169
|
-
# end
|
2170
|
-
# end
|
2171
|
-
#
|
2172
|
-
# note = Note.new
|
2173
|
-
# note.title = 'Battle of Austerlitz'
|
2174
|
-
# note.text = 'Some text here'
|
2175
|
-
#
|
2176
|
-
# user = User.new
|
2177
|
-
# user.name = 'Napoleon'
|
2178
|
-
# user.notes = [note]
|
2179
|
-
#
|
2180
|
-
# user.serializable_hash
|
2181
|
-
# # => {"name" => "Napoleon"}
|
2182
|
-
# user.serializable_hash(include: { notes: { only: 'title' }})
|
2183
|
-
# # => {"name" => "Napoleon", "notes" => [{"title"=>"Battle of Austerlitz"}]}
|
2184
|
-
def serializable_hash: (?untyped? options) -> untyped
|
2185
|
-
|
2186
|
-
def serializable_add_includes: (?::Hash[untyped, untyped] options) { (untyped, untyped, untyped) -> untyped } -> (nil | untyped)
|
2187
|
-
end
|
2188
|
-
end
|
2189
|
-
|
2190
|
-
module ActiveModel
|
2191
|
-
module Serializers
|
2192
|
-
# == Active \Model \JSON \Serializer
|
2193
|
-
module JSON
|
2194
|
-
extend ActiveSupport::Concern
|
2195
|
-
|
2196
|
-
include ActiveModel::Serialization
|
2197
|
-
|
2198
|
-
extend ActiveModel::Naming
|
2199
|
-
|
2200
|
-
# Returns a hash representing the model. Some configuration can be
|
2201
|
-
# passed through +options+.
|
2202
|
-
#
|
2203
|
-
# The option <tt>include_root_in_json</tt> controls the top-level behavior
|
2204
|
-
# of +as_json+. If +true+, +as_json+ will emit a single root node named
|
2205
|
-
# after the object's type. The default value for <tt>include_root_in_json</tt>
|
2206
|
-
# option is +false+.
|
2207
|
-
#
|
2208
|
-
# user = User.find(1)
|
2209
|
-
# user.as_json
|
2210
|
-
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
|
2211
|
-
# # "created_at" => "2006-08-01T17:27:133.000Z", "awesome" => true}
|
2212
|
-
#
|
2213
|
-
# ActiveRecord::Base.include_root_in_json = true
|
2214
|
-
#
|
2215
|
-
# user.as_json
|
2216
|
-
# # => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
|
2217
|
-
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true } }
|
2218
|
-
#
|
2219
|
-
# This behavior can also be achieved by setting the <tt>:root</tt> option
|
2220
|
-
# to +true+ as in:
|
2221
|
-
#
|
2222
|
-
# user = User.find(1)
|
2223
|
-
# user.as_json(root: true)
|
2224
|
-
# # => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
|
2225
|
-
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true } }
|
2226
|
-
#
|
2227
|
-
# Without any +options+, the returned Hash will include all the model's
|
2228
|
-
# attributes.
|
2229
|
-
#
|
2230
|
-
# user = User.find(1)
|
2231
|
-
# user.as_json
|
2232
|
-
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
|
2233
|
-
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true}
|
2234
|
-
#
|
2235
|
-
# The <tt>:only</tt> and <tt>:except</tt> options can be used to limit
|
2236
|
-
# the attributes included, and work similar to the +attributes+ method.
|
2237
|
-
#
|
2238
|
-
# user.as_json(only: [:id, :name])
|
2239
|
-
# # => { "id" => 1, "name" => "Konata Izumi" }
|
2240
|
-
#
|
2241
|
-
# user.as_json(except: [:id, :created_at, :age])
|
2242
|
-
# # => { "name" => "Konata Izumi", "awesome" => true }
|
2243
|
-
#
|
2244
|
-
# To include the result of some method calls on the model use <tt>:methods</tt>:
|
2245
|
-
#
|
2246
|
-
# user.as_json(methods: :permalink)
|
2247
|
-
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
|
2248
|
-
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true,
|
2249
|
-
# # "permalink" => "1-konata-izumi" }
|
2250
|
-
#
|
2251
|
-
# To include associations use <tt>:include</tt>:
|
2252
|
-
#
|
2253
|
-
# user.as_json(include: :posts)
|
2254
|
-
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
|
2255
|
-
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true,
|
2256
|
-
# # "posts" => [ { "id" => 1, "author_id" => 1, "title" => "Welcome to the weblog" },
|
2257
|
-
# # { "id" => 2, "author_id" => 1, "title" => "So I was thinking" } ] }
|
2258
|
-
#
|
2259
|
-
# Second level and higher order associations work as well:
|
2260
|
-
#
|
2261
|
-
# user.as_json(include: { posts: {
|
2262
|
-
# include: { comments: {
|
2263
|
-
# only: :body } },
|
2264
|
-
# only: :title } })
|
2265
|
-
# # => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
|
2266
|
-
# # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true,
|
2267
|
-
# # "posts" => [ { "comments" => [ { "body" => "1st post!" }, { "body" => "Second!" } ],
|
2268
|
-
# # "title" => "Welcome to the weblog" },
|
2269
|
-
# # { "comments" => [ { "body" => "Don't think too hard" } ],
|
2270
|
-
# # "title" => "So I was thinking" } ] }
|
2271
|
-
def as_json: (?untyped? options) -> untyped
|
2272
|
-
|
2273
|
-
# Sets the model +attributes+ from a JSON string. Returns +self+.
|
2274
|
-
#
|
2275
|
-
# class Person
|
2276
|
-
# include ActiveModel::Serializers::JSON
|
2277
|
-
#
|
2278
|
-
# attr_accessor :name, :age, :awesome
|
2279
|
-
#
|
2280
|
-
# def attributes=(hash)
|
2281
|
-
# hash.each do |key, value|
|
2282
|
-
# send("#{key}=", value)
|
2283
|
-
# end
|
2284
|
-
# end
|
2285
|
-
#
|
2286
|
-
# def attributes
|
2287
|
-
# instance_values
|
2288
|
-
# end
|
2289
|
-
# end
|
2290
|
-
#
|
2291
|
-
# json = { name: 'bob', age: 22, awesome:true }.to_json
|
2292
|
-
# person = Person.new
|
2293
|
-
# person.from_json(json) # => #<Person:0x007fec5e7a0088 @age=22, @awesome=true, @name="bob">
|
2294
|
-
# person.name # => "bob"
|
2295
|
-
# person.age # => 22
|
2296
|
-
# person.awesome # => true
|
2297
|
-
#
|
2298
|
-
# The default value for +include_root+ is +false+. You can change it to
|
2299
|
-
# +true+ if the given JSON string includes a single root node.
|
2300
|
-
#
|
2301
|
-
# json = { person: { name: 'bob', age: 22, awesome:true } }.to_json
|
2302
|
-
# person = Person.new
|
2303
|
-
# person.from_json(json, true) # => #<Person:0x007fec5e7a0088 @age=22, @awesome=true, @name="bob">
|
2304
|
-
# person.name # => "bob"
|
2305
|
-
# person.age # => 22
|
2306
|
-
# person.awesome # => true
|
2307
|
-
def from_json: (untyped json, ?untyped include_root) -> untyped
|
2308
|
-
end
|
2309
|
-
end
|
2310
|
-
end
|
2311
|
-
|
2312
|
-
module ActiveModel
|
2313
|
-
# == Active \Model \Translation
|
2314
|
-
#
|
2315
|
-
# Provides integration between your object and the Rails internationalization
|
2316
|
-
# (i18n) framework.
|
2317
|
-
#
|
2318
|
-
# A minimal implementation could be:
|
2319
|
-
#
|
2320
|
-
# class TranslatedPerson
|
2321
|
-
# extend ActiveModel::Translation
|
2322
|
-
# end
|
2323
|
-
#
|
2324
|
-
# TranslatedPerson.human_attribute_name('my_attribute')
|
2325
|
-
# # => "My attribute"
|
2326
|
-
#
|
2327
|
-
# This also provides the required class methods for hooking into the
|
2328
|
-
# Rails internationalization API, including being able to define a
|
2329
|
-
# class based +i18n_scope+ and +lookup_ancestors+ to find translations in
|
2330
|
-
# parent classes.
|
2331
|
-
module Translation
|
2332
|
-
include ActiveModel::Naming
|
2333
|
-
|
2334
|
-
# Returns the +i18n_scope+ for the class. Overwrite if you want custom lookup.
|
2335
|
-
def i18n_scope: () -> :activemodel
|
2336
|
-
|
2337
|
-
# When localizing a string, it goes through the lookup returned by this
|
2338
|
-
# method, which is used in ActiveModel::Name#human,
|
2339
|
-
# ActiveModel::Errors#full_messages and
|
2340
|
-
# ActiveModel::Translation#human_attribute_name.
|
2341
|
-
def lookup_ancestors: () -> untyped
|
2342
|
-
|
2343
|
-
# Transforms attribute names into a more human format, such as "First name"
|
2344
|
-
# instead of "first_name".
|
2345
|
-
#
|
2346
|
-
# Person.human_attribute_name("first_name") # => "First name"
|
2347
|
-
#
|
2348
|
-
# Specify +options+ with additional translating options.
|
2349
|
-
def human_attribute_name: (untyped attribute, ?::Hash[untyped, untyped] options) -> untyped
|
2350
|
-
end
|
2351
|
-
end
|
2352
|
-
|
2353
|
-
module ActiveModel
|
2354
|
-
module Type
|
2355
|
-
class BigInteger < Integer
|
2356
|
-
def max_value: () -> untyped
|
2357
|
-
end
|
2358
|
-
end
|
2359
|
-
end
|
2360
|
-
|
2361
|
-
module ActiveModel
|
2362
|
-
module Type
|
2363
|
-
class Binary < Value
|
2364
|
-
# :nodoc:
|
2365
|
-
def `type`: () -> :binary
|
2366
|
-
|
2367
|
-
def binary?: () -> ::TrueClass
|
2368
|
-
|
2369
|
-
def cast: (untyped value) -> untyped
|
2370
|
-
|
2371
|
-
def serialize: (untyped value) -> (nil | Data)
|
2372
|
-
|
2373
|
-
def changed_in_place?: (untyped raw_old_value, untyped value) -> untyped
|
2374
|
-
|
2375
|
-
class Data
|
2376
|
-
# :nodoc:
|
2377
|
-
def initialize: (untyped value) -> untyped
|
2378
|
-
|
2379
|
-
def to_s: () -> untyped
|
2380
|
-
|
2381
|
-
def hex: () -> untyped
|
2382
|
-
|
2383
|
-
def ==: (untyped other) -> untyped
|
2384
|
-
end
|
2385
|
-
end
|
2386
|
-
end
|
2387
|
-
end
|
2388
|
-
|
2389
|
-
module ActiveModel
|
2390
|
-
module Type
|
2391
|
-
# == Active \Model \Type \Boolean
|
2392
|
-
#
|
2393
|
-
# A class that behaves like a boolean type, including rules for coercion of user input.
|
2394
|
-
#
|
2395
|
-
# === Coercion
|
2396
|
-
# Values set from user input will first be coerced into the appropriate ruby type.
|
2397
|
-
# Coercion behavior is roughly mapped to Ruby's boolean semantics.
|
2398
|
-
#
|
2399
|
-
# - "false", "f" , "0", +0+ or any other value in +FALSE_VALUES+ will be coerced to +false+
|
2400
|
-
# - Empty strings are coerced to +nil+
|
2401
|
-
# - All other values will be coerced to +true+
|
2402
|
-
class Boolean < Value
|
2403
|
-
FALSE_VALUES: untyped
|
2404
|
-
|
2405
|
-
def `type`: () -> :boolean
|
2406
|
-
|
2407
|
-
def serialize: (untyped value) -> untyped
|
2408
|
-
|
2409
|
-
def cast_value: (untyped value) -> untyped
|
2410
|
-
end
|
2411
|
-
end
|
2412
|
-
end
|
2413
|
-
|
2414
|
-
module ActiveModel
|
2415
|
-
module Type
|
2416
|
-
class Date < Value
|
2417
|
-
# :nodoc:
|
2418
|
-
include Helpers::Timezone
|
2419
|
-
|
2420
|
-
def `type`: () -> :date
|
2421
|
-
|
2422
|
-
def type_cast_for_schema: (untyped value) -> untyped
|
2423
|
-
|
2424
|
-
def cast_value: (untyped value) -> untyped
|
2425
|
-
|
2426
|
-
ISO_DATE: untyped
|
2427
|
-
|
2428
|
-
def fast_string_to_date: (untyped string) -> untyped
|
2429
|
-
|
2430
|
-
def fallback_string_to_date: (untyped string) -> untyped
|
2431
|
-
|
2432
|
-
def new_date: (untyped year, untyped mon, untyped mday) -> untyped
|
2433
|
-
|
2434
|
-
def value_from_multiparameter_assignment: () -> untyped
|
2435
|
-
end
|
2436
|
-
end
|
2437
|
-
end
|
2438
|
-
|
2439
|
-
module ActiveModel
|
2440
|
-
module Type
|
2441
|
-
class DateTime < Value
|
2442
|
-
# :nodoc:
|
2443
|
-
include Helpers::Timezone
|
2444
|
-
|
2445
|
-
include Helpers::TimeValue
|
2446
|
-
|
2447
|
-
def `type`: () -> :datetime
|
2448
|
-
|
2449
|
-
def cast_value: (untyped value) -> (untyped | nil)
|
2450
|
-
|
2451
|
-
# '0.123456' -> 123456
|
2452
|
-
# '1.123456' -> 123456
|
2453
|
-
def microseconds: (untyped time) -> untyped
|
2454
|
-
|
2455
|
-
def fallback_string_to_time: (untyped string) -> untyped
|
2456
|
-
|
2457
|
-
def value_from_multiparameter_assignment: (untyped values_hash) -> untyped
|
2458
|
-
end
|
2459
|
-
end
|
2460
|
-
end
|
2461
|
-
|
2462
|
-
module ActiveModel
|
2463
|
-
module Type
|
2464
|
-
class Decimal < Value
|
2465
|
-
# :nodoc:
|
2466
|
-
include Helpers::Numeric
|
2467
|
-
|
2468
|
-
BIGDECIMAL_PRECISION: ::Integer
|
2469
|
-
|
2470
|
-
def `type`: () -> :decimal
|
2471
|
-
|
2472
|
-
def type_cast_for_schema: (untyped value) -> untyped
|
2473
|
-
|
2474
|
-
def cast_value: (untyped value) -> untyped
|
2475
|
-
|
2476
|
-
def convert_float_to_big_decimal: (untyped value) -> untyped
|
2477
|
-
|
2478
|
-
def float_precision: () -> untyped
|
2479
|
-
|
2480
|
-
def apply_scale: (untyped value) -> untyped
|
2481
|
-
end
|
2482
|
-
end
|
2483
|
-
end
|
2484
|
-
|
2485
|
-
module ActiveModel
|
2486
|
-
module Type
|
2487
|
-
class Float < Value
|
2488
|
-
# :nodoc:
|
2489
|
-
include Helpers::Numeric
|
2490
|
-
|
2491
|
-
def `type`: () -> :float
|
2492
|
-
|
2493
|
-
def type_cast_for_schema: (untyped value) -> ("::Float::NAN" | untyped)
|
2494
|
-
|
2495
|
-
def cast_value: (untyped value) -> untyped
|
2496
|
-
end
|
2497
|
-
end
|
2498
|
-
end
|
2499
|
-
|
2500
|
-
module ActiveModel
|
2501
|
-
module Type
|
2502
|
-
module Helpers
|
2503
|
-
# :nodoc: all
|
2504
|
-
class AcceptsMultiparameterTime < Module
|
2505
|
-
def initialize: (?defaults: ::Hash[untyped, untyped] defaults) -> (nil | untyped)
|
2506
|
-
end
|
2507
|
-
end
|
2508
|
-
end
|
2509
|
-
end
|
2510
|
-
|
2511
|
-
module ActiveModel
|
2512
|
-
module Type
|
2513
|
-
module Helpers
|
2514
|
-
# :nodoc: all
|
2515
|
-
module Mutable
|
2516
|
-
def cast: (untyped value) -> untyped
|
2517
|
-
|
2518
|
-
# +raw_old_value+ will be the `_before_type_cast` version of the
|
2519
|
-
# value (likely a string). +new_value+ will be the current, type
|
2520
|
-
# cast value.
|
2521
|
-
def changed_in_place?: (untyped raw_old_value, untyped new_value) -> untyped
|
2522
|
-
end
|
2523
|
-
end
|
2524
|
-
end
|
2525
|
-
end
|
2526
|
-
|
2527
|
-
module ActiveModel
|
2528
|
-
module Type
|
2529
|
-
module Helpers
|
2530
|
-
# :nodoc: all
|
2531
|
-
module Numeric
|
2532
|
-
def serialize: (untyped value) -> untyped
|
2533
|
-
|
2534
|
-
def cast: (untyped value) -> untyped
|
2535
|
-
|
2536
|
-
def changed?: (untyped old_value, untyped _new_value, untyped new_value_before_type_cast) -> untyped
|
2537
|
-
|
2538
|
-
def number_to_non_number?: (untyped old_value, untyped new_value_before_type_cast) -> untyped
|
2539
|
-
|
2540
|
-
def non_numeric_string?: (untyped value) -> untyped
|
2541
|
-
|
2542
|
-
NUMERIC_REGEX: untyped
|
2543
|
-
end
|
2544
|
-
end
|
2545
|
-
end
|
2546
|
-
end
|
2547
|
-
|
2548
|
-
module ActiveModel
|
2549
|
-
module Type
|
2550
|
-
module Helpers
|
2551
|
-
# :nodoc: all
|
2552
|
-
module TimeValue
|
2553
|
-
def serialize: (untyped value) -> untyped
|
2554
|
-
|
2555
|
-
def apply_seconds_precision: (untyped value) -> untyped
|
2556
|
-
|
2557
|
-
def type_cast_for_schema: (untyped value) -> untyped
|
2558
|
-
|
2559
|
-
def user_input_in_time_zone: (untyped value) -> untyped
|
2560
|
-
|
2561
|
-
def new_time: (untyped year, untyped mon, untyped mday, untyped hour, untyped min, untyped sec, untyped microsec, ?untyped? offset) -> (nil | untyped)
|
2562
|
-
|
2563
|
-
ISO_DATETIME: untyped
|
2564
|
-
|
2565
|
-
# Doesn't handle time zones.
|
2566
|
-
def fast_string_to_time: (untyped string) -> untyped
|
2567
|
-
end
|
2568
|
-
end
|
2569
|
-
end
|
2570
|
-
end
|
2571
|
-
|
2572
|
-
module ActiveModel
|
2573
|
-
module Type
|
2574
|
-
module Helpers
|
2575
|
-
# :nodoc: all
|
2576
|
-
module Timezone
|
2577
|
-
def is_utc?: () -> untyped
|
2578
|
-
|
2579
|
-
def default_timezone: () -> untyped
|
2580
|
-
end
|
2581
|
-
end
|
2582
|
-
end
|
2583
|
-
end
|
2584
|
-
|
2585
|
-
module ActiveModel
|
2586
|
-
module Type
|
2587
|
-
class ImmutableString < Value
|
2588
|
-
# :nodoc:
|
2589
|
-
def `type`: () -> :string
|
2590
|
-
|
2591
|
-
def serialize: (untyped value) -> untyped
|
2592
|
-
|
2593
|
-
def cast_value: (untyped value) -> untyped
|
2594
|
-
end
|
2595
|
-
end
|
2596
|
-
end
|
2597
|
-
|
2598
|
-
module ActiveModel
|
2599
|
-
module Type
|
2600
|
-
class Integer < Value
|
2601
|
-
# :nodoc:
|
2602
|
-
include Helpers::Numeric
|
2603
|
-
|
2604
|
-
# Column storage size in bytes.
|
2605
|
-
# 4 bytes means an integer as opposed to smallint etc.
|
2606
|
-
DEFAULT_LIMIT: ::Integer
|
2607
|
-
|
2608
|
-
def initialize: () -> untyped
|
2609
|
-
|
2610
|
-
def `type`: () -> :integer
|
2611
|
-
|
2612
|
-
def deserialize: (untyped value) -> (nil | untyped)
|
2613
|
-
|
2614
|
-
def serialize: (untyped value) -> (nil | untyped)
|
2615
|
-
|
2616
|
-
attr_reader range: untyped
|
2617
|
-
|
2618
|
-
def cast_value: (untyped value) -> untyped
|
2619
|
-
|
2620
|
-
def ensure_in_range: (untyped value) -> untyped
|
2621
|
-
|
2622
|
-
def max_value: () -> untyped
|
2623
|
-
|
2624
|
-
def min_value: () -> untyped
|
2625
|
-
|
2626
|
-
def _limit: () -> untyped
|
2627
|
-
end
|
2628
|
-
end
|
2629
|
-
end
|
2630
|
-
|
2631
|
-
module ActiveModel
|
2632
|
-
module Type
|
2633
|
-
attr_accessor registry: untyped
|
2634
|
-
|
2635
|
-
# Add a new type to the registry, allowing it to be gotten through ActiveModel::Type#lookup
|
2636
|
-
def self.register: (untyped type_name, ?untyped? klass, **untyped options) { () -> untyped } -> untyped
|
2637
|
-
|
2638
|
-
def self.lookup: (*untyped args, **untyped kwargs) -> untyped
|
2639
|
-
|
2640
|
-
def self.default_value: () -> untyped
|
2641
|
-
end
|
2642
|
-
end
|
2643
|
-
|
2644
|
-
module ActiveModel
|
2645
|
-
# :stopdoc:
|
2646
|
-
module Type
|
2647
|
-
class Registry
|
2648
|
-
def initialize: () -> untyped
|
2649
|
-
|
2650
|
-
def register: (untyped type_name, ?untyped? klass, **untyped options) { () -> untyped } -> untyped
|
2651
|
-
|
2652
|
-
def lookup: (untyped symbol, *untyped args, **untyped kwargs) -> untyped
|
2653
|
-
|
2654
|
-
attr_reader registrations: untyped
|
2655
|
-
|
2656
|
-
def registration_klass: () -> untyped
|
2657
|
-
|
2658
|
-
def find_registration: (untyped symbol, *untyped args) -> untyped
|
2659
|
-
end
|
2660
|
-
|
2661
|
-
class Registration
|
2662
|
-
# Options must be taken because of https://bugs.ruby-lang.org/issues/10856
|
2663
|
-
def initialize: (untyped name, untyped block) -> untyped
|
2664
|
-
|
2665
|
-
def call: (untyped _registry, *untyped args, **untyped kwargs) -> untyped
|
2666
|
-
|
2667
|
-
def matches?: (untyped type_name, *untyped args, **untyped kwargs) -> untyped
|
2668
|
-
|
2669
|
-
attr_reader name: untyped
|
2670
|
-
|
2671
|
-
attr_reader block: untyped
|
2672
|
-
end
|
2673
|
-
end
|
2674
|
-
end
|
2675
|
-
|
2676
|
-
module ActiveModel
|
2677
|
-
module Type
|
2678
|
-
class String < ImmutableString
|
2679
|
-
# :nodoc:
|
2680
|
-
def changed_in_place?: (untyped raw_old_value, untyped new_value) -> untyped
|
2681
|
-
|
2682
|
-
def cast_value: (untyped value) -> untyped
|
2683
|
-
end
|
2684
|
-
end
|
2685
|
-
end
|
2686
|
-
|
2687
|
-
module ActiveModel
|
2688
|
-
module Type
|
2689
|
-
class Time < Value
|
2690
|
-
# :nodoc:
|
2691
|
-
include Helpers::Timezone
|
2692
|
-
|
2693
|
-
include Helpers::TimeValue
|
2694
|
-
|
2695
|
-
def `type`: () -> :time
|
2696
|
-
|
2697
|
-
def user_input_in_time_zone: (untyped value) -> (nil | untyped)
|
2698
|
-
|
2699
|
-
def cast_value: (untyped value) -> (untyped | nil)
|
2700
|
-
end
|
2701
|
-
end
|
2702
|
-
end
|
2703
|
-
|
2704
|
-
module ActiveModel
|
2705
|
-
module Type
|
2706
|
-
class Value
|
2707
|
-
attr_reader precision: untyped
|
2708
|
-
|
2709
|
-
attr_reader scale: untyped
|
2710
|
-
|
2711
|
-
attr_reader limit: untyped
|
2712
|
-
|
2713
|
-
def initialize: (?scale: untyped? scale, ?limit: untyped? limit, ?precision: untyped? precision) -> untyped
|
2714
|
-
|
2715
|
-
def `type`: () -> nil
|
2716
|
-
|
2717
|
-
# Converts a value from database input to the appropriate ruby type. The
|
2718
|
-
# return value of this method will be returned from
|
2719
|
-
# ActiveRecord::AttributeMethods::Read#read_attribute. The default
|
2720
|
-
# implementation just calls Value#cast.
|
2721
|
-
#
|
2722
|
-
# +value+ The raw input, as provided from the database.
|
2723
|
-
def deserialize: (untyped value) -> untyped
|
2724
|
-
|
2725
|
-
# Type casts a value from user input (e.g. from a setter). This value may
|
2726
|
-
# be a string from the form builder, or a ruby object passed to a setter.
|
2727
|
-
# There is currently no way to differentiate between which source it came
|
2728
|
-
# from.
|
2729
|
-
#
|
2730
|
-
# The return value of this method will be returned from
|
2731
|
-
# ActiveRecord::AttributeMethods::Read#read_attribute. See also:
|
2732
|
-
# Value#cast_value.
|
2733
|
-
#
|
2734
|
-
# +value+ The raw input, as provided to the attribute setter.
|
2735
|
-
def cast: (untyped value) -> untyped
|
2736
|
-
|
2737
|
-
# Casts a value from the ruby type to a type that the database knows how
|
2738
|
-
# to understand. The returned value from this method should be a
|
2739
|
-
# +String+, +Numeric+, +Date+, +Time+, +Symbol+, +true+, +false+, or
|
2740
|
-
# +nil+.
|
2741
|
-
def serialize: (untyped value) -> untyped
|
2742
|
-
|
2743
|
-
def type_cast_for_schema: (untyped value) -> untyped
|
2744
|
-
|
2745
|
-
def binary?: () -> ::FalseClass
|
2746
|
-
|
2747
|
-
# Determines whether a value has changed for dirty checking. +old_value+
|
2748
|
-
# and +new_value+ will always be type-cast. Types should not need to
|
2749
|
-
# override this method.
|
2750
|
-
def changed?: (untyped old_value, untyped new_value, untyped _new_value_before_type_cast) -> untyped
|
2751
|
-
|
2752
|
-
# Determines whether the mutable value has been modified since it was
|
2753
|
-
# read. Returns +false+ by default. If your type returns an object
|
2754
|
-
# which could be mutated, you should override this method. You will need
|
2755
|
-
# to either:
|
2756
|
-
#
|
2757
|
-
# - pass +new_value+ to Value#serialize and compare it to
|
2758
|
-
# +raw_old_value+
|
2759
|
-
#
|
2760
|
-
# or
|
2761
|
-
#
|
2762
|
-
# - pass +raw_old_value+ to Value#deserialize and compare it to
|
2763
|
-
# +new_value+
|
2764
|
-
#
|
2765
|
-
# +raw_old_value+ The original value, before being passed to
|
2766
|
-
# +deserialize+.
|
2767
|
-
#
|
2768
|
-
# +new_value+ The current value, after type casting.
|
2769
|
-
def changed_in_place?: (untyped raw_old_value, untyped new_value) -> ::FalseClass
|
2770
|
-
|
2771
|
-
def value_constructed_by_mass_assignment?: (untyped _value) -> ::FalseClass
|
2772
|
-
|
2773
|
-
def force_equality?: (untyped _value) -> ::FalseClass
|
2774
|
-
|
2775
|
-
def map: (untyped value) { (untyped) -> untyped } -> untyped
|
2776
|
-
|
2777
|
-
def ==: (untyped other) -> untyped
|
2778
|
-
|
2779
|
-
def hash: () -> untyped
|
2780
|
-
|
2781
|
-
def assert_valid_value: () -> nil
|
2782
|
-
|
2783
|
-
def cast_value: (untyped value) -> untyped
|
2784
|
-
end
|
2785
|
-
end
|
2786
|
-
end
|
2787
|
-
|
2788
|
-
module ActiveModel
|
2789
|
-
module Validations
|
2790
|
-
class AbsenceValidator < EachValidator
|
2791
|
-
# == \Active \Model Absence Validator
|
2792
|
-
# nodoc:
|
2793
|
-
def validate_each: (untyped record, untyped attr_name, untyped value) -> untyped
|
2794
|
-
end
|
2795
|
-
|
2796
|
-
module HelperMethods
|
2797
|
-
# Validates that the specified attributes are blank (as defined by
|
2798
|
-
# Object#present?). Happens by default on save.
|
2799
|
-
#
|
2800
|
-
# class Person < ActiveRecord::Base
|
2801
|
-
# validates_absence_of :first_name
|
2802
|
-
# end
|
2803
|
-
#
|
2804
|
-
# The first_name attribute must be in the object and it must be blank.
|
2805
|
-
#
|
2806
|
-
# Configuration options:
|
2807
|
-
# * <tt>:message</tt> - A custom error message (default is: "must be blank").
|
2808
|
-
#
|
2809
|
-
# There is also a list of default options supported by every validator:
|
2810
|
-
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
2811
|
-
# See <tt>ActiveModel::Validations#validates</tt> for more information
|
2812
|
-
def validates_absence_of: (*untyped attr_names) -> untyped
|
2813
|
-
end
|
2814
|
-
end
|
2815
|
-
end
|
2816
|
-
|
2817
|
-
module ActiveModel
|
2818
|
-
module Validations
|
2819
|
-
class AcceptanceValidator < EachValidator
|
2820
|
-
# :nodoc:
|
2821
|
-
def initialize: (untyped options) -> untyped
|
2822
|
-
|
2823
|
-
def validate_each: (untyped record, untyped attribute, untyped value) -> untyped
|
2824
|
-
|
2825
|
-
def setup!: (untyped klass) -> untyped
|
2826
|
-
|
2827
|
-
def acceptable_option?: (untyped value) -> untyped
|
2828
|
-
|
2829
|
-
class LazilyDefineAttributes < Module
|
2830
|
-
def initialize: (untyped attributes) -> untyped
|
2831
|
-
|
2832
|
-
def included: (untyped klass) -> untyped
|
2833
|
-
|
2834
|
-
def matches?: (untyped method_name) -> untyped
|
2835
|
-
|
2836
|
-
def define_on: (untyped klass) -> untyped
|
2837
|
-
|
2838
|
-
def ==: (untyped other) -> untyped
|
2839
|
-
|
2840
|
-
attr_reader attributes: untyped
|
2841
|
-
end
|
2842
|
-
end
|
2843
|
-
|
2844
|
-
module HelperMethods
|
2845
|
-
# Encapsulates the pattern of wanting to validate the acceptance of a
|
2846
|
-
# terms of service check box (or similar agreement).
|
2847
|
-
#
|
2848
|
-
# class Person < ActiveRecord::Base
|
2849
|
-
# validates_acceptance_of :terms_of_service
|
2850
|
-
# validates_acceptance_of :eula, message: 'must be abided'
|
2851
|
-
# end
|
2852
|
-
#
|
2853
|
-
# If the database column does not exist, the +terms_of_service+ attribute
|
2854
|
-
# is entirely virtual. This check is performed only if +terms_of_service+
|
2855
|
-
# is not +nil+ and by default on save.
|
2856
|
-
#
|
2857
|
-
# Configuration options:
|
2858
|
-
# * <tt>:message</tt> - A custom error message (default is: "must be
|
2859
|
-
# accepted").
|
2860
|
-
# * <tt>:accept</tt> - Specifies a value that is considered accepted.
|
2861
|
-
# Also accepts an array of possible values. The default value is
|
2862
|
-
# an array ["1", true], which makes it easy to relate to an HTML
|
2863
|
-
# checkbox. This should be set to, or include, +true+ if you are validating
|
2864
|
-
# a database column, since the attribute is typecast from "1" to +true+
|
2865
|
-
# before validation.
|
2866
|
-
#
|
2867
|
-
# There is also a list of default options supported by every validator:
|
2868
|
-
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
2869
|
-
# See <tt>ActiveModel::Validations#validates</tt> for more information.
|
2870
|
-
def validates_acceptance_of: (*untyped attr_names) -> untyped
|
2871
|
-
end
|
2872
|
-
end
|
2873
|
-
end
|
2874
|
-
|
2875
|
-
module ActiveModel
|
2876
|
-
module Validations
|
2877
|
-
# == Active \Model \Validation \Callbacks
|
2878
|
-
#
|
2879
|
-
# Provides an interface for any class to have +before_validation+ and
|
2880
|
-
# +after_validation+ callbacks.
|
2881
|
-
#
|
2882
|
-
# First, include ActiveModel::Validations::Callbacks from the class you are
|
2883
|
-
# creating:
|
2884
|
-
#
|
2885
|
-
# class MyModel
|
2886
|
-
# include ActiveModel::Validations::Callbacks
|
2887
|
-
#
|
2888
|
-
# before_validation :do_stuff_before_validation
|
2889
|
-
# after_validation :do_stuff_after_validation
|
2890
|
-
# end
|
2891
|
-
#
|
2892
|
-
# Like other <tt>before_*</tt> callbacks if +before_validation+ throws
|
2893
|
-
# +:abort+ then <tt>valid?</tt> will not be called.
|
2894
|
-
module Callbacks
|
2895
|
-
extend ActiveSupport::Concern
|
2896
|
-
|
2897
|
-
include ActiveSupport::Callbacks
|
2898
|
-
|
2899
|
-
extend ::ActiveSupport::Callbacks::ClassMethods
|
2900
|
-
|
2901
|
-
module ClassMethods
|
2902
|
-
# Defines a callback that will get called right before validation.
|
2903
|
-
#
|
2904
|
-
# class Person
|
2905
|
-
# include ActiveModel::Validations
|
2906
|
-
# include ActiveModel::Validations::Callbacks
|
2907
|
-
#
|
2908
|
-
# attr_accessor :name
|
2909
|
-
#
|
2910
|
-
# validates_length_of :name, maximum: 6
|
2911
|
-
#
|
2912
|
-
# before_validation :remove_whitespaces
|
2913
|
-
#
|
2914
|
-
# private
|
2915
|
-
#
|
2916
|
-
# def remove_whitespaces
|
2917
|
-
# name.strip!
|
2918
|
-
# end
|
2919
|
-
# end
|
2920
|
-
#
|
2921
|
-
# person = Person.new
|
2922
|
-
# person.name = ' bob '
|
2923
|
-
# person.valid? # => true
|
2924
|
-
# person.name # => "bob"
|
2925
|
-
def before_validation: (*untyped args) { () -> untyped } -> untyped
|
2926
|
-
|
2927
|
-
# Defines a callback that will get called right after validation.
|
2928
|
-
#
|
2929
|
-
# class Person
|
2930
|
-
# include ActiveModel::Validations
|
2931
|
-
# include ActiveModel::Validations::Callbacks
|
2932
|
-
#
|
2933
|
-
# attr_accessor :name, :status
|
2934
|
-
#
|
2935
|
-
# validates_presence_of :name
|
2936
|
-
#
|
2937
|
-
# after_validation :set_status
|
2938
|
-
#
|
2939
|
-
# private
|
2940
|
-
#
|
2941
|
-
# def set_status
|
2942
|
-
# self.status = errors.empty?
|
2943
|
-
# end
|
2944
|
-
# end
|
2945
|
-
#
|
2946
|
-
# person = Person.new
|
2947
|
-
# person.name = ''
|
2948
|
-
# person.valid? # => false
|
2949
|
-
# person.status # => false
|
2950
|
-
# person.name = 'bob'
|
2951
|
-
# person.valid? # => true
|
2952
|
-
# person.status # => true
|
2953
|
-
def after_validation: (*untyped args) { () -> untyped } -> untyped
|
2954
|
-
end
|
2955
|
-
|
2956
|
-
# Overwrite run validations to include callbacks.
|
2957
|
-
def run_validations!: () -> untyped
|
2958
|
-
end
|
2959
|
-
end
|
2960
|
-
end
|
2961
|
-
|
2962
|
-
module ActiveModel
|
2963
|
-
module Validations
|
2964
|
-
module Clusivity
|
2965
|
-
# nodoc:
|
2966
|
-
ERROR_MESSAGE: ::String
|
2967
|
-
|
2968
|
-
def check_validity!: () -> untyped
|
2969
|
-
|
2970
|
-
def include?: (untyped record, untyped value) -> untyped
|
2971
|
-
|
2972
|
-
def delimiter: () -> untyped
|
2973
|
-
|
2974
|
-
# After Ruby 2.2, <tt>Range#include?</tt> on non-number-or-time-ish ranges checks all
|
2975
|
-
# possible values in the range for equality, which is slower but more accurate.
|
2976
|
-
# <tt>Range#cover?</tt> uses the previous logic of comparing a value with the range
|
2977
|
-
# endpoints, which is fast but is only accurate on Numeric, Time, Date,
|
2978
|
-
# or DateTime ranges.
|
2979
|
-
def inclusion_method: (untyped enumerable) -> untyped
|
2980
|
-
end
|
2981
|
-
end
|
2982
|
-
end
|
2983
|
-
|
2984
|
-
module ActiveModel
|
2985
|
-
module Validations
|
2986
|
-
class ConfirmationValidator < EachValidator
|
2987
|
-
# :nodoc:
|
2988
|
-
def initialize: (untyped options) -> untyped
|
2989
|
-
|
2990
|
-
def validate_each: (untyped record, untyped attribute, untyped value) -> untyped
|
2991
|
-
|
2992
|
-
def setup!: (untyped klass) -> untyped
|
2993
|
-
|
2994
|
-
def confirmation_value_equal?: (untyped record, untyped attribute, untyped value, untyped confirmed) -> untyped
|
2995
|
-
end
|
2996
|
-
|
2997
|
-
module HelperMethods
|
2998
|
-
# Encapsulates the pattern of wanting to validate a password or email
|
2999
|
-
# address field with a confirmation.
|
3000
|
-
#
|
3001
|
-
# Model:
|
3002
|
-
# class Person < ActiveRecord::Base
|
3003
|
-
# validates_confirmation_of :user_name, :password
|
3004
|
-
# validates_confirmation_of :email_address,
|
3005
|
-
# message: 'should match confirmation'
|
3006
|
-
# end
|
3007
|
-
#
|
3008
|
-
# View:
|
3009
|
-
# <%= password_field "person", "password" %>
|
3010
|
-
# <%= password_field "person", "password_confirmation" %>
|
3011
|
-
#
|
3012
|
-
# The added +password_confirmation+ attribute is virtual; it exists only
|
3013
|
-
# as an in-memory attribute for validating the password. To achieve this,
|
3014
|
-
# the validation adds accessors to the model for the confirmation
|
3015
|
-
# attribute.
|
3016
|
-
#
|
3017
|
-
# NOTE: This check is performed only if +password_confirmation+ is not
|
3018
|
-
# +nil+. To require confirmation, make sure to add a presence check for
|
3019
|
-
# the confirmation attribute:
|
3020
|
-
#
|
3021
|
-
# validates_presence_of :password_confirmation, if: :password_changed?
|
3022
|
-
#
|
3023
|
-
# Configuration options:
|
3024
|
-
# * <tt>:message</tt> - A custom error message (default is: "doesn't match
|
3025
|
-
# <tt>%{translated_attribute_name}</tt>").
|
3026
|
-
# * <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by
|
3027
|
-
# non-text columns (+true+ by default).
|
3028
|
-
#
|
3029
|
-
# There is also a list of default options supported by every validator:
|
3030
|
-
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
3031
|
-
# See <tt>ActiveModel::Validations#validates</tt> for more information
|
3032
|
-
def validates_confirmation_of: (*untyped attr_names) -> untyped
|
3033
|
-
end
|
3034
|
-
end
|
3035
|
-
end
|
3036
|
-
|
3037
|
-
module ActiveModel
|
3038
|
-
module Validations
|
3039
|
-
class ExclusionValidator < EachValidator
|
3040
|
-
# :nodoc:
|
3041
|
-
include Clusivity
|
3042
|
-
|
3043
|
-
def validate_each: (untyped record, untyped attribute, untyped value) -> untyped
|
3044
|
-
end
|
3045
|
-
|
3046
|
-
module HelperMethods
|
3047
|
-
# Validates that the value of the specified attribute is not in a
|
3048
|
-
# particular enumerable object.
|
3049
|
-
#
|
3050
|
-
# class Person < ActiveRecord::Base
|
3051
|
-
# validates_exclusion_of :username, in: %w( admin superuser ), message: "You don't belong here"
|
3052
|
-
# validates_exclusion_of :age, in: 30..60, message: 'This site is only for under 30 and over 60'
|
3053
|
-
# validates_exclusion_of :format, in: %w( mov avi ), message: "extension %{value} is not allowed"
|
3054
|
-
# validates_exclusion_of :password, in: ->(person) { [person.username, person.first_name] },
|
3055
|
-
# message: 'should not be the same as your username or first name'
|
3056
|
-
# validates_exclusion_of :karma, in: :reserved_karmas
|
3057
|
-
# end
|
3058
|
-
#
|
3059
|
-
# Configuration options:
|
3060
|
-
# * <tt>:in</tt> - An enumerable object of items that the value shouldn't
|
3061
|
-
# be part of. This can be supplied as a proc, lambda or symbol which returns an
|
3062
|
-
# enumerable. If the enumerable is a numerical, time or datetime range the test
|
3063
|
-
# is performed with <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>. When
|
3064
|
-
# using a proc or lambda the instance under validation is passed as an argument.
|
3065
|
-
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
|
3066
|
-
# <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>.
|
3067
|
-
# * <tt>:message</tt> - Specifies a custom error message (default is: "is
|
3068
|
-
# reserved").
|
3069
|
-
#
|
3070
|
-
# There is also a list of default options supported by every validator:
|
3071
|
-
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
3072
|
-
# See <tt>ActiveModel::Validations#validates</tt> for more information
|
3073
|
-
def validates_exclusion_of: (*untyped attr_names) -> untyped
|
3074
|
-
end
|
3075
|
-
end
|
3076
|
-
end
|
3077
|
-
|
3078
|
-
module ActiveModel
|
3079
|
-
module Validations
|
3080
|
-
class FormatValidator < EachValidator
|
3081
|
-
# :nodoc:
|
3082
|
-
def validate_each: (untyped record, untyped attribute, untyped value) -> untyped
|
3083
|
-
|
3084
|
-
def check_validity!: () -> untyped
|
3085
|
-
|
3086
|
-
def option_call: (untyped record, untyped name) -> untyped
|
3087
|
-
|
3088
|
-
def record_error: (untyped record, untyped attribute, untyped name, untyped value) -> untyped
|
3089
|
-
|
3090
|
-
def check_options_validity: (untyped name) -> untyped
|
3091
|
-
|
3092
|
-
def regexp_using_multiline_anchors?: (untyped regexp) -> untyped
|
3093
|
-
end
|
3094
|
-
|
3095
|
-
module HelperMethods
|
3096
|
-
# Validates whether the value of the specified attribute is of the correct
|
3097
|
-
# form, going by the regular expression provided. You can require that the
|
3098
|
-
# attribute matches the regular expression:
|
3099
|
-
#
|
3100
|
-
# class Person < ActiveRecord::Base
|
3101
|
-
# validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create
|
3102
|
-
# end
|
3103
|
-
#
|
3104
|
-
# Alternatively, you can require that the specified attribute does _not_
|
3105
|
-
# match the regular expression:
|
3106
|
-
#
|
3107
|
-
# class Person < ActiveRecord::Base
|
3108
|
-
# validates_format_of :email, without: /NOSPAM/
|
3109
|
-
# end
|
3110
|
-
#
|
3111
|
-
# You can also provide a proc or lambda which will determine the regular
|
3112
|
-
# expression that will be used to validate the attribute.
|
3113
|
-
#
|
3114
|
-
# class Person < ActiveRecord::Base
|
3115
|
-
# # Admin can have number as a first letter in their screen name
|
3116
|
-
# validates_format_of :screen_name,
|
3117
|
-
# with: ->(person) { person.admin? ? /\A[a-z0-9][a-z0-9_\-]*\z/i : /\A[a-z][a-z0-9_\-]*\z/i }
|
3118
|
-
# end
|
3119
|
-
#
|
3120
|
-
# Note: use <tt>\A</tt> and <tt>\z</tt> to match the start and end of the
|
3121
|
-
# string, <tt>^</tt> and <tt>$</tt> match the start/end of a line.
|
3122
|
-
#
|
3123
|
-
# Due to frequent misuse of <tt>^</tt> and <tt>$</tt>, you need to pass
|
3124
|
-
# the <tt>multiline: true</tt> option in case you use any of these two
|
3125
|
-
# anchors in the provided regular expression. In most cases, you should be
|
3126
|
-
# using <tt>\A</tt> and <tt>\z</tt>.
|
3127
|
-
#
|
3128
|
-
# You must pass either <tt>:with</tt> or <tt>:without</tt> as an option.
|
3129
|
-
# In addition, both must be a regular expression or a proc or lambda, or
|
3130
|
-
# else an exception will be raised.
|
3131
|
-
#
|
3132
|
-
# Configuration options:
|
3133
|
-
# * <tt>:message</tt> - A custom error message (default is: "is invalid").
|
3134
|
-
# * <tt>:with</tt> - Regular expression that if the attribute matches will
|
3135
|
-
# result in a successful validation. This can be provided as a proc or
|
3136
|
-
# lambda returning regular expression which will be called at runtime.
|
3137
|
-
# * <tt>:without</tt> - Regular expression that if the attribute does not
|
3138
|
-
# match will result in a successful validation. This can be provided as
|
3139
|
-
# a proc or lambda returning regular expression which will be called at
|
3140
|
-
# runtime.
|
3141
|
-
# * <tt>:multiline</tt> - Set to true if your regular expression contains
|
3142
|
-
# anchors that match the beginning or end of lines as opposed to the
|
3143
|
-
# beginning or end of the string. These anchors are <tt>^</tt> and <tt>$</tt>.
|
3144
|
-
#
|
3145
|
-
# There is also a list of default options supported by every validator:
|
3146
|
-
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
3147
|
-
# See <tt>ActiveModel::Validations#validates</tt> for more information
|
3148
|
-
def validates_format_of: (*untyped attr_names) -> untyped
|
3149
|
-
end
|
3150
|
-
end
|
3151
|
-
end
|
3152
|
-
|
3153
|
-
module ActiveModel
|
3154
|
-
module Validations
|
3155
|
-
module HelperMethods
|
3156
|
-
def _merge_attributes: (untyped attr_names) -> untyped
|
3157
|
-
end
|
3158
|
-
end
|
3159
|
-
end
|
3160
|
-
|
3161
|
-
module ActiveModel
|
3162
|
-
module Validations
|
3163
|
-
class InclusionValidator < EachValidator
|
3164
|
-
# :nodoc:
|
3165
|
-
include Clusivity
|
3166
|
-
|
3167
|
-
def validate_each: (untyped record, untyped attribute, untyped value) -> untyped
|
3168
|
-
end
|
3169
|
-
|
3170
|
-
module HelperMethods
|
3171
|
-
# Validates whether the value of the specified attribute is available in a
|
3172
|
-
# particular enumerable object.
|
3173
|
-
#
|
3174
|
-
# class Person < ActiveRecord::Base
|
3175
|
-
# validates_inclusion_of :role, in: %w( admin contributor )
|
3176
|
-
# validates_inclusion_of :age, in: 0..99
|
3177
|
-
# validates_inclusion_of :format, in: %w( jpg gif png ), message: "extension %{value} is not included in the list"
|
3178
|
-
# validates_inclusion_of :states, in: ->(person) { STATES[person.country] }
|
3179
|
-
# validates_inclusion_of :karma, in: :available_karmas
|
3180
|
-
# end
|
3181
|
-
#
|
3182
|
-
# Configuration options:
|
3183
|
-
# * <tt>:in</tt> - An enumerable object of available items. This can be
|
3184
|
-
# supplied as a proc, lambda or symbol which returns an enumerable. If the
|
3185
|
-
# enumerable is a numerical, time or datetime range the test is performed
|
3186
|
-
# with <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>. When using
|
3187
|
-
# a proc or lambda the instance under validation is passed as an argument.
|
3188
|
-
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
|
3189
|
-
# * <tt>:message</tt> - Specifies a custom error message (default is: "is
|
3190
|
-
# not included in the list").
|
3191
|
-
#
|
3192
|
-
# There is also a list of default options supported by every validator:
|
3193
|
-
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
3194
|
-
# See <tt>ActiveModel::Validations#validates</tt> for more information
|
3195
|
-
def validates_inclusion_of: (*untyped attr_names) -> untyped
|
3196
|
-
end
|
3197
|
-
end
|
3198
|
-
end
|
3199
|
-
|
3200
|
-
module ActiveModel
|
3201
|
-
module Validations
|
3202
|
-
class LengthValidator < EachValidator
|
3203
|
-
# :nodoc:
|
3204
|
-
MESSAGES: untyped
|
3205
|
-
|
3206
|
-
CHECKS: untyped
|
3207
|
-
|
3208
|
-
RESERVED_OPTIONS: ::Array[untyped]
|
3209
|
-
|
3210
|
-
def initialize: (untyped options) -> untyped
|
3211
|
-
|
3212
|
-
def check_validity!: () -> untyped
|
3213
|
-
|
3214
|
-
def validate_each: (untyped record, untyped attribute, untyped value) -> untyped
|
3215
|
-
|
3216
|
-
def skip_nil_check?: (untyped key) -> untyped
|
3217
|
-
end
|
3218
|
-
|
3219
|
-
module HelperMethods
|
3220
|
-
# Validates that the specified attributes match the length restrictions
|
3221
|
-
# supplied. Only one constraint option can be used at a time apart from
|
3222
|
-
# +:minimum+ and +:maximum+ that can be combined together:
|
3223
|
-
#
|
3224
|
-
# class Person < ActiveRecord::Base
|
3225
|
-
# validates_length_of :first_name, maximum: 30
|
3226
|
-
# validates_length_of :last_name, maximum: 30, message: "less than 30 if you don't mind"
|
3227
|
-
# validates_length_of :fax, in: 7..32, allow_nil: true
|
3228
|
-
# validates_length_of :phone, in: 7..32, allow_blank: true
|
3229
|
-
# validates_length_of :user_name, within: 6..20, too_long: 'pick a shorter name', too_short: 'pick a longer name'
|
3230
|
-
# validates_length_of :zip_code, minimum: 5, too_short: 'please enter at least 5 characters'
|
3231
|
-
# validates_length_of :smurf_leader, is: 4, message: "papa is spelled with 4 characters... don't play me."
|
3232
|
-
# validates_length_of :words_in_essay, minimum: 100, too_short: 'Your essay must be at least 100 words.'
|
3233
|
-
#
|
3234
|
-
# private
|
3235
|
-
#
|
3236
|
-
# def words_in_essay
|
3237
|
-
# essay.scan(/\w+/)
|
3238
|
-
# end
|
3239
|
-
# end
|
3240
|
-
#
|
3241
|
-
# Constraint options:
|
3242
|
-
#
|
3243
|
-
# * <tt>:minimum</tt> - The minimum size of the attribute.
|
3244
|
-
# * <tt>:maximum</tt> - The maximum size of the attribute. Allows +nil+ by
|
3245
|
-
# default if not used with +:minimum+.
|
3246
|
-
# * <tt>:is</tt> - The exact size of the attribute.
|
3247
|
-
# * <tt>:within</tt> - A range specifying the minimum and maximum size of
|
3248
|
-
# the attribute.
|
3249
|
-
# * <tt>:in</tt> - A synonym (or alias) for <tt>:within</tt>.
|
3250
|
-
#
|
3251
|
-
# Other options:
|
3252
|
-
#
|
3253
|
-
# * <tt>:allow_nil</tt> - Attribute may be +nil+; skip validation.
|
3254
|
-
# * <tt>:allow_blank</tt> - Attribute may be blank; skip validation.
|
3255
|
-
# * <tt>:too_long</tt> - The error message if the attribute goes over the
|
3256
|
-
# maximum (default is: "is too long (maximum is %{count} characters)").
|
3257
|
-
# * <tt>:too_short</tt> - The error message if the attribute goes under the
|
3258
|
-
# minimum (default is: "is too short (minimum is %{count} characters)").
|
3259
|
-
# * <tt>:wrong_length</tt> - The error message if using the <tt>:is</tt>
|
3260
|
-
# method and the attribute is the wrong size (default is: "is the wrong
|
3261
|
-
# length (should be %{count} characters)").
|
3262
|
-
# * <tt>:message</tt> - The error message to use for a <tt>:minimum</tt>,
|
3263
|
-
# <tt>:maximum</tt>, or <tt>:is</tt> violation. An alias of the appropriate
|
3264
|
-
# <tt>too_long</tt>/<tt>too_short</tt>/<tt>wrong_length</tt> message.
|
3265
|
-
#
|
3266
|
-
# There is also a list of default options supported by every validator:
|
3267
|
-
# +:if+, +:unless+, +:on+ and +:strict+.
|
3268
|
-
# See <tt>ActiveModel::Validations#validates</tt> for more information
|
3269
|
-
def validates_length_of: (*untyped attr_names) -> untyped
|
3270
|
-
end
|
3271
|
-
end
|
3272
|
-
end
|
3273
|
-
|
3274
|
-
module ActiveModel
|
3275
|
-
module Validations
|
3276
|
-
class NumericalityValidator < EachValidator
|
3277
|
-
# :nodoc:
|
3278
|
-
CHECKS: untyped
|
3279
|
-
|
3280
|
-
RESERVED_OPTIONS: untyped
|
3281
|
-
|
3282
|
-
INTEGER_REGEX: untyped
|
3283
|
-
|
3284
|
-
HEXADECIMAL_REGEX: untyped
|
3285
|
-
|
3286
|
-
def check_validity!: () -> untyped
|
3287
|
-
|
3288
|
-
def validate_each: (untyped record, untyped attr_name, untyped value) -> (nil | untyped)
|
3289
|
-
|
3290
|
-
def is_number?: (untyped raw_value) -> untyped
|
3291
|
-
|
3292
|
-
def parse_as_number: (untyped raw_value) -> untyped
|
3293
|
-
|
3294
|
-
def is_integer?: (untyped raw_value) -> untyped
|
3295
|
-
|
3296
|
-
def is_hexadecimal_literal?: (untyped raw_value) -> untyped
|
3297
|
-
|
3298
|
-
def filtered_options: (untyped value) -> untyped
|
3299
|
-
|
3300
|
-
def allow_only_integer?: (untyped record) -> untyped
|
3301
|
-
|
3302
|
-
def record_attribute_changed_in_place?: (untyped record, untyped attr_name) -> untyped
|
3303
|
-
end
|
3304
|
-
|
3305
|
-
module HelperMethods
|
3306
|
-
# Validates whether the value of the specified attribute is numeric by
|
3307
|
-
# trying to convert it to a float with Kernel.Float (if <tt>only_integer</tt>
|
3308
|
-
# is +false+) or applying it to the regular expression <tt>/\A[\+\-]?\d+\z/</tt>
|
3309
|
-
# (if <tt>only_integer</tt> is set to +true+).
|
3310
|
-
#
|
3311
|
-
# class Person < ActiveRecord::Base
|
3312
|
-
# validates_numericality_of :value, on: :create
|
3313
|
-
# end
|
3314
|
-
#
|
3315
|
-
# Configuration options:
|
3316
|
-
# * <tt>:message</tt> - A custom error message (default is: "is not a number").
|
3317
|
-
# * <tt>:only_integer</tt> - Specifies whether the value has to be an
|
3318
|
-
# integer, e.g. an integral value (default is +false+).
|
3319
|
-
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+ (default is
|
3320
|
-
# +false+). Notice that for Integer and Float columns empty strings are
|
3321
|
-
# converted to +nil+.
|
3322
|
-
# * <tt>:greater_than</tt> - Specifies the value must be greater than the
|
3323
|
-
# supplied value.
|
3324
|
-
# * <tt>:greater_than_or_equal_to</tt> - Specifies the value must be
|
3325
|
-
# greater than or equal the supplied value.
|
3326
|
-
# * <tt>:equal_to</tt> - Specifies the value must be equal to the supplied
|
3327
|
-
# value.
|
3328
|
-
# * <tt>:less_than</tt> - Specifies the value must be less than the
|
3329
|
-
# supplied value.
|
3330
|
-
# * <tt>:less_than_or_equal_to</tt> - Specifies the value must be less
|
3331
|
-
# than or equal the supplied value.
|
3332
|
-
# * <tt>:other_than</tt> - Specifies the value must be other than the
|
3333
|
-
# supplied value.
|
3334
|
-
# * <tt>:odd</tt> - Specifies the value must be an odd number.
|
3335
|
-
# * <tt>:even</tt> - Specifies the value must be an even number.
|
3336
|
-
#
|
3337
|
-
# There is also a list of default options supported by every validator:
|
3338
|
-
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+ .
|
3339
|
-
# See <tt>ActiveModel::Validations#validates</tt> for more information
|
3340
|
-
#
|
3341
|
-
# The following checks can also be supplied with a proc or a symbol which
|
3342
|
-
# corresponds to a method:
|
3343
|
-
#
|
3344
|
-
# * <tt>:greater_than</tt>
|
3345
|
-
# * <tt>:greater_than_or_equal_to</tt>
|
3346
|
-
# * <tt>:equal_to</tt>
|
3347
|
-
# * <tt>:less_than</tt>
|
3348
|
-
# * <tt>:less_than_or_equal_to</tt>
|
3349
|
-
# * <tt>:only_integer</tt>
|
3350
|
-
#
|
3351
|
-
# For example:
|
3352
|
-
#
|
3353
|
-
# class Person < ActiveRecord::Base
|
3354
|
-
# validates_numericality_of :width, less_than: ->(person) { person.height }
|
3355
|
-
# validates_numericality_of :width, greater_than: :minimum_weight
|
3356
|
-
# end
|
3357
|
-
def validates_numericality_of: (*untyped attr_names) -> untyped
|
3358
|
-
end
|
3359
|
-
end
|
3360
|
-
end
|
3361
|
-
|
3362
|
-
module ActiveModel
|
3363
|
-
module Validations
|
3364
|
-
class PresenceValidator < EachValidator
|
3365
|
-
# :nodoc:
|
3366
|
-
def validate_each: (untyped record, untyped attr_name, untyped value) -> untyped
|
3367
|
-
end
|
3368
|
-
|
3369
|
-
module HelperMethods
|
3370
|
-
# Validates that the specified attributes are not blank (as defined by
|
3371
|
-
# Object#blank?). Happens by default on save.
|
3372
|
-
#
|
3373
|
-
# class Person < ActiveRecord::Base
|
3374
|
-
# validates_presence_of :first_name
|
3375
|
-
# end
|
3376
|
-
#
|
3377
|
-
# The first_name attribute must be in the object and it cannot be blank.
|
3378
|
-
#
|
3379
|
-
# If you want to validate the presence of a boolean field (where the real
|
3380
|
-
# values are +true+ and +false+), you will want to use
|
3381
|
-
# <tt>validates_inclusion_of :field_name, in: [true, false]</tt>.
|
3382
|
-
#
|
3383
|
-
# This is due to the way Object#blank? handles boolean values:
|
3384
|
-
# <tt>false.blank? # => true</tt>.
|
3385
|
-
#
|
3386
|
-
# Configuration options:
|
3387
|
-
# * <tt>:message</tt> - A custom error message (default is: "can't be blank").
|
3388
|
-
#
|
3389
|
-
# There is also a list of default options supported by every validator:
|
3390
|
-
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
3391
|
-
# See <tt>ActiveModel::Validations#validates</tt> for more information
|
3392
|
-
def validates_presence_of: (*untyped attr_names) -> untyped
|
3393
|
-
end
|
3394
|
-
end
|
3395
|
-
end
|
3396
|
-
|
3397
|
-
module ActiveModel
|
3398
|
-
# == Active \Model \Validations
|
3399
|
-
#
|
3400
|
-
# Provides a full validation framework to your objects.
|
3401
|
-
#
|
3402
|
-
# A minimal implementation could be:
|
3403
|
-
#
|
3404
|
-
# class Person
|
3405
|
-
# include ActiveModel::Validations
|
3406
|
-
#
|
3407
|
-
# attr_accessor :first_name, :last_name
|
3408
|
-
#
|
3409
|
-
# validates_each :first_name, :last_name do |record, attr, value|
|
3410
|
-
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
|
3411
|
-
# end
|
3412
|
-
# end
|
3413
|
-
#
|
3414
|
-
# Which provides you with the full standard validation stack that you
|
3415
|
-
# know from Active Record:
|
3416
|
-
#
|
3417
|
-
# person = Person.new
|
3418
|
-
# person.valid? # => true
|
3419
|
-
# person.invalid? # => false
|
3420
|
-
#
|
3421
|
-
# person.first_name = 'zoolander'
|
3422
|
-
# person.valid? # => false
|
3423
|
-
# person.invalid? # => true
|
3424
|
-
# person.errors.messages # => {first_name:["starts with z."]}
|
3425
|
-
#
|
3426
|
-
# Note that <tt>ActiveModel::Validations</tt> automatically adds an +errors+
|
3427
|
-
# method to your instances initialized with a new <tt>ActiveModel::Errors</tt>
|
3428
|
-
# object, so there is no need for you to do this manually.
|
3429
|
-
module Validations
|
3430
|
-
extend ActiveSupport::Concern
|
3431
|
-
|
3432
|
-
extend ActiveModel::Naming
|
3433
|
-
|
3434
|
-
extend ActiveModel::Callbacks
|
3435
|
-
|
3436
|
-
extend ActiveModel::Translation
|
3437
|
-
|
3438
|
-
extend HelperMethods
|
3439
|
-
|
3440
|
-
include HelperMethods
|
3441
|
-
|
3442
|
-
attr_accessor validation_context: untyped
|
3443
|
-
|
3444
|
-
module ClassMethods
|
3445
|
-
# Validates each attribute against a block.
|
3446
|
-
#
|
3447
|
-
# class Person
|
3448
|
-
# include ActiveModel::Validations
|
3449
|
-
#
|
3450
|
-
# attr_accessor :first_name, :last_name
|
3451
|
-
#
|
3452
|
-
# validates_each :first_name, :last_name, allow_blank: true do |record, attr, value|
|
3453
|
-
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
|
3454
|
-
# end
|
3455
|
-
# end
|
3456
|
-
#
|
3457
|
-
# Options:
|
3458
|
-
# * <tt>:on</tt> - Specifies the contexts where this validation is active.
|
3459
|
-
# Runs in all validation contexts by default +nil+. You can pass a symbol
|
3460
|
-
# or an array of symbols. (e.g. <tt>on: :create</tt> or
|
3461
|
-
# <tt>on: :custom_validation_context</tt> or
|
3462
|
-
# <tt>on: [:create, :custom_validation_context]</tt>)
|
3463
|
-
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
|
3464
|
-
# * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
|
3465
|
-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
3466
|
-
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
3467
|
-
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
|
3468
|
-
# proc or string should return or evaluate to a +true+ or +false+ value.
|
3469
|
-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
3470
|
-
# determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
3471
|
-
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
3472
|
-
# method, proc or string should return or evaluate to a +true+ or +false+
|
3473
|
-
# value.
|
3474
|
-
def validates_each: (*untyped attr_names) { () -> untyped } -> untyped
|
3475
|
-
|
3476
|
-
VALID_OPTIONS_FOR_VALIDATE: untyped
|
3477
|
-
|
3478
|
-
# Adds a validation method or block to the class. This is useful when
|
3479
|
-
# overriding the +validate+ instance method becomes too unwieldy and
|
3480
|
-
# you're looking for more descriptive declaration of your validations.
|
3481
|
-
#
|
3482
|
-
# This can be done with a symbol pointing to a method:
|
3483
|
-
#
|
3484
|
-
# class Comment
|
3485
|
-
# include ActiveModel::Validations
|
3486
|
-
#
|
3487
|
-
# validate :must_be_friends
|
3488
|
-
#
|
3489
|
-
# def must_be_friends
|
3490
|
-
# errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
|
3491
|
-
# end
|
3492
|
-
# end
|
3493
|
-
#
|
3494
|
-
# With a block which is passed with the current record to be validated:
|
3495
|
-
#
|
3496
|
-
# class Comment
|
3497
|
-
# include ActiveModel::Validations
|
3498
|
-
#
|
3499
|
-
# validate do |comment|
|
3500
|
-
# comment.must_be_friends
|
3501
|
-
# end
|
3502
|
-
#
|
3503
|
-
# def must_be_friends
|
3504
|
-
# errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
|
3505
|
-
# end
|
3506
|
-
# end
|
3507
|
-
#
|
3508
|
-
# Or with a block where self points to the current record to be validated:
|
3509
|
-
#
|
3510
|
-
# class Comment
|
3511
|
-
# include ActiveModel::Validations
|
3512
|
-
#
|
3513
|
-
# validate do
|
3514
|
-
# errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
|
3515
|
-
# end
|
3516
|
-
# end
|
3517
|
-
#
|
3518
|
-
# Note that the return value of validation methods is not relevant.
|
3519
|
-
# It's not possible to halt the validate callback chain.
|
3520
|
-
#
|
3521
|
-
# Options:
|
3522
|
-
# * <tt>:on</tt> - Specifies the contexts where this validation is active.
|
3523
|
-
# Runs in all validation contexts by default +nil+. You can pass a symbol
|
3524
|
-
# or an array of symbols. (e.g. <tt>on: :create</tt> or
|
3525
|
-
# <tt>on: :custom_validation_context</tt> or
|
3526
|
-
# <tt>on: [:create, :custom_validation_context]</tt>)
|
3527
|
-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
3528
|
-
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
3529
|
-
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
|
3530
|
-
# proc or string should return or evaluate to a +true+ or +false+ value.
|
3531
|
-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
3532
|
-
# determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
3533
|
-
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
3534
|
-
# method, proc or string should return or evaluate to a +true+ or +false+
|
3535
|
-
# value.
|
3536
|
-
#
|
3537
|
-
# NOTE: Calling +validate+ multiple times on the same method will overwrite previous definitions.
|
3538
|
-
#
|
3539
|
-
def validate: (*untyped args) { () -> untyped } -> untyped
|
3540
|
-
|
3541
|
-
# List all validators that are being used to validate the model using
|
3542
|
-
# +validates_with+ method.
|
3543
|
-
#
|
3544
|
-
# class Person
|
3545
|
-
# include ActiveModel::Validations
|
3546
|
-
#
|
3547
|
-
# validates_with MyValidator
|
3548
|
-
# validates_with OtherValidator, on: :create
|
3549
|
-
# validates_with StrictValidator, strict: true
|
3550
|
-
# end
|
3551
|
-
#
|
3552
|
-
# Person.validators
|
3553
|
-
# # => [
|
3554
|
-
# # #<MyValidator:0x007fbff403e808 @options={}>,
|
3555
|
-
# # #<OtherValidator:0x007fbff403d930 @options={on: :create}>,
|
3556
|
-
# # #<StrictValidator:0x007fbff3204a30 @options={strict:true}>
|
3557
|
-
# # ]
|
3558
|
-
def validators: () -> untyped
|
3559
|
-
|
3560
|
-
# Clears all of the validators and validations.
|
3561
|
-
#
|
3562
|
-
# Note that this will clear anything that is being used to validate
|
3563
|
-
# the model for both the +validates_with+ and +validate+ methods.
|
3564
|
-
# It clears the validators that are created with an invocation of
|
3565
|
-
# +validates_with+ and the callbacks that are set by an invocation
|
3566
|
-
# of +validate+.
|
3567
|
-
#
|
3568
|
-
# class Person
|
3569
|
-
# include ActiveModel::Validations
|
3570
|
-
#
|
3571
|
-
# validates_with MyValidator
|
3572
|
-
# validates_with OtherValidator, on: :create
|
3573
|
-
# validates_with StrictValidator, strict: true
|
3574
|
-
# validate :cannot_be_robot
|
3575
|
-
#
|
3576
|
-
# def cannot_be_robot
|
3577
|
-
# errors.add(:base, 'A person cannot be a robot') if person_is_robot
|
3578
|
-
# end
|
3579
|
-
# end
|
3580
|
-
#
|
3581
|
-
# Person.validators
|
3582
|
-
# # => [
|
3583
|
-
# # #<MyValidator:0x007fbff403e808 @options={}>,
|
3584
|
-
# # #<OtherValidator:0x007fbff403d930 @options={on: :create}>,
|
3585
|
-
# # #<StrictValidator:0x007fbff3204a30 @options={strict:true}>
|
3586
|
-
# # ]
|
3587
|
-
#
|
3588
|
-
# If one runs <tt>Person.clear_validators!</tt> and then checks to see what
|
3589
|
-
# validators this class has, you would obtain:
|
3590
|
-
#
|
3591
|
-
# Person.validators # => []
|
3592
|
-
#
|
3593
|
-
# Also, the callback set by <tt>validate :cannot_be_robot</tt> will be erased
|
3594
|
-
# so that:
|
3595
|
-
#
|
3596
|
-
# Person._validate_callbacks.empty? # => true
|
3597
|
-
#
|
3598
|
-
def clear_validators!: () -> untyped
|
3599
|
-
|
3600
|
-
# List all validators that are being used to validate a specific attribute.
|
3601
|
-
#
|
3602
|
-
# class Person
|
3603
|
-
# include ActiveModel::Validations
|
3604
|
-
#
|
3605
|
-
# attr_accessor :name , :age
|
3606
|
-
#
|
3607
|
-
# validates_presence_of :name
|
3608
|
-
# validates_inclusion_of :age, in: 0..99
|
3609
|
-
# end
|
3610
|
-
#
|
3611
|
-
# Person.validators_on(:name)
|
3612
|
-
# # => [
|
3613
|
-
# # #<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>,
|
3614
|
-
# # ]
|
3615
|
-
def validators_on: (*untyped attributes) -> untyped
|
3616
|
-
|
3617
|
-
# Returns +true+ if +attribute+ is an attribute method, +false+ otherwise.
|
3618
|
-
#
|
3619
|
-
# class Person
|
3620
|
-
# include ActiveModel::Validations
|
3621
|
-
#
|
3622
|
-
# attr_accessor :name
|
3623
|
-
# end
|
3624
|
-
#
|
3625
|
-
# User.attribute_method?(:name) # => true
|
3626
|
-
# User.attribute_method?(:age) # => false
|
3627
|
-
def attribute_method?: (untyped attribute) -> untyped
|
3628
|
-
|
3629
|
-
def inherited: (untyped base) -> untyped
|
3630
|
-
end
|
3631
|
-
|
3632
|
-
def initialize_dup: (untyped other) -> untyped
|
3633
|
-
|
3634
|
-
# Returns the +Errors+ object that holds all information about attribute
|
3635
|
-
# error messages.
|
3636
|
-
#
|
3637
|
-
# class Person
|
3638
|
-
# include ActiveModel::Validations
|
3639
|
-
#
|
3640
|
-
# attr_accessor :name
|
3641
|
-
# validates_presence_of :name
|
3642
|
-
# end
|
3643
|
-
#
|
3644
|
-
# person = Person.new
|
3645
|
-
# person.valid? # => false
|
3646
|
-
# person.errors # => #<ActiveModel::Errors:0x007fe603816640 @messages={name:["can't be blank"]}>
|
3647
|
-
def errors: () -> untyped
|
3648
|
-
|
3649
|
-
# Runs all the specified validations and returns +true+ if no errors were
|
3650
|
-
# added otherwise +false+.
|
3651
|
-
#
|
3652
|
-
# class Person
|
3653
|
-
# include ActiveModel::Validations
|
3654
|
-
#
|
3655
|
-
# attr_accessor :name
|
3656
|
-
# validates_presence_of :name
|
3657
|
-
# end
|
3658
|
-
#
|
3659
|
-
# person = Person.new
|
3660
|
-
# person.name = ''
|
3661
|
-
# person.valid? # => false
|
3662
|
-
# person.name = 'david'
|
3663
|
-
# person.valid? # => true
|
3664
|
-
#
|
3665
|
-
# Context can optionally be supplied to define which callbacks to test
|
3666
|
-
# against (the context is defined on the validations using <tt>:on</tt>).
|
3667
|
-
#
|
3668
|
-
# class Person
|
3669
|
-
# include ActiveModel::Validations
|
3670
|
-
#
|
3671
|
-
# attr_accessor :name
|
3672
|
-
# validates_presence_of :name, on: :new
|
3673
|
-
# end
|
3674
|
-
#
|
3675
|
-
# person = Person.new
|
3676
|
-
# person.valid? # => true
|
3677
|
-
# person.valid?(:new) # => false
|
3678
|
-
def valid?: (?untyped? context) -> untyped
|
3679
|
-
|
3680
|
-
# Performs the opposite of <tt>valid?</tt>. Returns +true+ if errors were
|
3681
|
-
# added, +false+ otherwise.
|
3682
|
-
#
|
3683
|
-
# class Person
|
3684
|
-
# include ActiveModel::Validations
|
3685
|
-
#
|
3686
|
-
# attr_accessor :name
|
3687
|
-
# validates_presence_of :name
|
3688
|
-
# end
|
3689
|
-
#
|
3690
|
-
# person = Person.new
|
3691
|
-
# person.name = ''
|
3692
|
-
# person.invalid? # => true
|
3693
|
-
# person.name = 'david'
|
3694
|
-
# person.invalid? # => false
|
3695
|
-
#
|
3696
|
-
# Context can optionally be supplied to define which callbacks to test
|
3697
|
-
# against (the context is defined on the validations using <tt>:on</tt>).
|
3698
|
-
#
|
3699
|
-
# class Person
|
3700
|
-
# include ActiveModel::Validations
|
3701
|
-
#
|
3702
|
-
# attr_accessor :name
|
3703
|
-
# validates_presence_of :name, on: :new
|
3704
|
-
# end
|
3705
|
-
#
|
3706
|
-
# person = Person.new
|
3707
|
-
# person.invalid? # => false
|
3708
|
-
# person.invalid?(:new) # => true
|
3709
|
-
def invalid?: (?untyped? context) -> untyped
|
3710
|
-
|
3711
|
-
# Runs all the validations within the specified context. Returns +true+ if
|
3712
|
-
# no errors are found, raises +ValidationError+ otherwise.
|
3713
|
-
#
|
3714
|
-
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
|
3715
|
-
# some <tt>:on</tt> option will only run in the specified context.
|
3716
|
-
def validate!: (?untyped? context) -> untyped
|
3717
|
-
|
3718
|
-
def run_validations!: () -> untyped
|
3719
|
-
|
3720
|
-
def raise_validation_error: () -> untyped
|
3721
|
-
end
|
3722
|
-
|
3723
|
-
# = Active Model ValidationError
|
3724
|
-
#
|
3725
|
-
# Raised by <tt>validate!</tt> when the model is invalid. Use the
|
3726
|
-
# +model+ method to retrieve the record which did not validate.
|
3727
|
-
#
|
3728
|
-
# begin
|
3729
|
-
# complex_operation_that_internally_calls_validate!
|
3730
|
-
# rescue ActiveModel::ValidationError => invalid
|
3731
|
-
# puts invalid.model.errors
|
3732
|
-
# end
|
3733
|
-
class ValidationError < StandardError
|
3734
|
-
attr_reader model: untyped
|
3735
|
-
|
3736
|
-
def initialize: (untyped model) -> untyped
|
3737
|
-
end
|
3738
|
-
end
|
3739
|
-
|
3740
|
-
module ActiveModel
|
3741
|
-
module Validations
|
3742
|
-
module ClassMethods
|
3743
|
-
# This method is a shortcut to all default validators and any custom
|
3744
|
-
# validator classes ending in 'Validator'. Note that Rails default
|
3745
|
-
# validators can be overridden inside specific classes by creating
|
3746
|
-
# custom validator classes in their place such as PresenceValidator.
|
3747
|
-
#
|
3748
|
-
# Examples of using the default rails validators:
|
3749
|
-
#
|
3750
|
-
# validates :terms, acceptance: true
|
3751
|
-
# validates :password, confirmation: true
|
3752
|
-
# validates :username, exclusion: { in: %w(admin superuser) }
|
3753
|
-
# validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }
|
3754
|
-
# validates :age, inclusion: { in: 0..9 }
|
3755
|
-
# validates :first_name, length: { maximum: 30 }
|
3756
|
-
# validates :age, numericality: true
|
3757
|
-
# validates :username, presence: true
|
3758
|
-
#
|
3759
|
-
# The power of the +validates+ method comes when using custom validators
|
3760
|
-
# and default validators in one call for a given attribute.
|
3761
|
-
#
|
3762
|
-
# class EmailValidator < ActiveModel::EachValidator
|
3763
|
-
# def validate_each(record, attribute, value)
|
3764
|
-
# record.errors.add attribute, (options[:message] || "is not an email") unless
|
3765
|
-
# value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
3766
|
-
# end
|
3767
|
-
# end
|
3768
|
-
#
|
3769
|
-
# class Person
|
3770
|
-
# include ActiveModel::Validations
|
3771
|
-
# attr_accessor :name, :email
|
3772
|
-
#
|
3773
|
-
# validates :name, presence: true, length: { maximum: 100 }
|
3774
|
-
# validates :email, presence: true, email: true
|
3775
|
-
# end
|
3776
|
-
#
|
3777
|
-
# Validator classes may also exist within the class being validated
|
3778
|
-
# allowing custom modules of validators to be included as needed.
|
3779
|
-
#
|
3780
|
-
# class Film
|
3781
|
-
# include ActiveModel::Validations
|
3782
|
-
#
|
3783
|
-
# class TitleValidator < ActiveModel::EachValidator
|
3784
|
-
# def validate_each(record, attribute, value)
|
3785
|
-
# record.errors.add attribute, "must start with 'the'" unless value =~ /\Athe/i
|
3786
|
-
# end
|
3787
|
-
# end
|
3788
|
-
#
|
3789
|
-
# validates :name, title: true
|
3790
|
-
# end
|
3791
|
-
#
|
3792
|
-
# Additionally validator classes may be in another namespace and still
|
3793
|
-
# used within any class.
|
3794
|
-
#
|
3795
|
-
# validates :name, :'film/title' => true
|
3796
|
-
#
|
3797
|
-
# The validators hash can also handle regular expressions, ranges, arrays
|
3798
|
-
# and strings in shortcut form.
|
3799
|
-
#
|
3800
|
-
# validates :email, format: /@/
|
3801
|
-
# validates :role, inclusion: %(admin contributor)
|
3802
|
-
# validates :password, length: 6..20
|
3803
|
-
#
|
3804
|
-
# When using shortcut form, ranges and arrays are passed to your
|
3805
|
-
# validator's initializer as <tt>options[:in]</tt> while other types
|
3806
|
-
# including regular expressions and strings are passed as <tt>options[:with]</tt>.
|
3807
|
-
#
|
3808
|
-
# There is also a list of options that could be used along with validators:
|
3809
|
-
#
|
3810
|
-
# * <tt>:on</tt> - Specifies the contexts where this validation is active.
|
3811
|
-
# Runs in all validation contexts by default +nil+. You can pass a symbol
|
3812
|
-
# or an array of symbols. (e.g. <tt>on: :create</tt> or
|
3813
|
-
# <tt>on: :custom_validation_context</tt> or
|
3814
|
-
# <tt>on: [:create, :custom_validation_context]</tt>)
|
3815
|
-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
3816
|
-
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
3817
|
-
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
|
3818
|
-
# proc or string should return or evaluate to a +true+ or +false+ value.
|
3819
|
-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
|
3820
|
-
# if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
3821
|
-
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
3822
|
-
# method, proc or string should return or evaluate to a +true+ or
|
3823
|
-
# +false+ value.
|
3824
|
-
# * <tt>:allow_nil</tt> - Skip validation if the attribute is +nil+.
|
3825
|
-
# * <tt>:allow_blank</tt> - Skip validation if the attribute is blank.
|
3826
|
-
# * <tt>:strict</tt> - If the <tt>:strict</tt> option is set to true
|
3827
|
-
# will raise ActiveModel::StrictValidationFailed instead of adding the error.
|
3828
|
-
# <tt>:strict</tt> option can also be set to any other exception.
|
3829
|
-
#
|
3830
|
-
# Example:
|
3831
|
-
#
|
3832
|
-
# validates :password, presence: true, confirmation: true, if: :password_required?
|
3833
|
-
# validates :token, length: 24, strict: TokenLengthException
|
3834
|
-
#
|
3835
|
-
#
|
3836
|
-
# Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+, +:strict+
|
3837
|
-
# and +:message+ can be given to one specific validator, as a hash:
|
3838
|
-
#
|
3839
|
-
# validates :password, presence: { if: :password_required?, message: 'is forgotten.' }, confirmation: true
|
3840
|
-
def validates: (*untyped attributes) -> untyped
|
3841
|
-
|
3842
|
-
# This method is used to define validations that cannot be corrected by end
|
3843
|
-
# users and are considered exceptional. So each validator defined with bang
|
3844
|
-
# or <tt>:strict</tt> option set to <tt>true</tt> will always raise
|
3845
|
-
# <tt>ActiveModel::StrictValidationFailed</tt> instead of adding error
|
3846
|
-
# when validation fails. See <tt>validates</tt> for more information about
|
3847
|
-
# the validation itself.
|
3848
|
-
#
|
3849
|
-
# class Person
|
3850
|
-
# include ActiveModel::Validations
|
3851
|
-
#
|
3852
|
-
# attr_accessor :name
|
3853
|
-
# validates! :name, presence: true
|
3854
|
-
# end
|
3855
|
-
#
|
3856
|
-
# person = Person.new
|
3857
|
-
# person.name = ''
|
3858
|
-
# person.valid?
|
3859
|
-
# # => ActiveModel::StrictValidationFailed: Name can't be blank
|
3860
|
-
def validates!: (*untyped attributes) -> untyped
|
3861
|
-
|
3862
|
-
# When creating custom validators, it might be useful to be able to specify
|
3863
|
-
# additional default keys. This can be done by overwriting this method.
|
3864
|
-
def _validates_default_keys: () -> ::Array[:if | :unless | :on | :allow_blank | :allow_nil | :strict]
|
3865
|
-
|
3866
|
-
def _parse_validates_options: (untyped options) -> untyped
|
3867
|
-
end
|
3868
|
-
end
|
3869
|
-
end
|
3870
|
-
|
3871
|
-
module ActiveModel
|
3872
|
-
module Validations
|
3873
|
-
class WithValidator < EachValidator
|
3874
|
-
# :nodoc:
|
3875
|
-
def validate_each: (untyped record, untyped attr, untyped val) -> untyped
|
3876
|
-
end
|
3877
|
-
|
3878
|
-
module ClassMethods
|
3879
|
-
# Passes the record off to the class or classes specified and allows them
|
3880
|
-
# to add errors based on more complex conditions.
|
3881
|
-
#
|
3882
|
-
# class Person
|
3883
|
-
# include ActiveModel::Validations
|
3884
|
-
# validates_with MyValidator
|
3885
|
-
# end
|
3886
|
-
#
|
3887
|
-
# class MyValidator < ActiveModel::Validator
|
3888
|
-
# def validate(record)
|
3889
|
-
# if some_complex_logic
|
3890
|
-
# record.errors.add :base, 'This record is invalid'
|
3891
|
-
# end
|
3892
|
-
# end
|
3893
|
-
#
|
3894
|
-
# private
|
3895
|
-
# def some_complex_logic
|
3896
|
-
# # ...
|
3897
|
-
# end
|
3898
|
-
# end
|
3899
|
-
#
|
3900
|
-
# You may also pass it multiple classes, like so:
|
3901
|
-
#
|
3902
|
-
# class Person
|
3903
|
-
# include ActiveModel::Validations
|
3904
|
-
# validates_with MyValidator, MyOtherValidator, on: :create
|
3905
|
-
# end
|
3906
|
-
#
|
3907
|
-
# Configuration options:
|
3908
|
-
# * <tt>:on</tt> - Specifies the contexts where this validation is active.
|
3909
|
-
# Runs in all validation contexts by default +nil+. You can pass a symbol
|
3910
|
-
# or an array of symbols. (e.g. <tt>on: :create</tt> or
|
3911
|
-
# <tt>on: :custom_validation_context</tt> or
|
3912
|
-
# <tt>on: [:create, :custom_validation_context]</tt>)
|
3913
|
-
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
3914
|
-
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
3915
|
-
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>).
|
3916
|
-
# The method, proc or string should return or evaluate to a +true+ or
|
3917
|
-
# +false+ value.
|
3918
|
-
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
3919
|
-
# determine if the validation should not occur
|
3920
|
-
# (e.g. <tt>unless: :skip_validation</tt>, or
|
3921
|
-
# <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>).
|
3922
|
-
# The method, proc or string should return or evaluate to a +true+ or
|
3923
|
-
# +false+ value.
|
3924
|
-
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
3925
|
-
# See <tt>ActiveModel::Validations#validates!</tt> for more information.
|
3926
|
-
#
|
3927
|
-
# If you pass any additional configuration options, they will be passed
|
3928
|
-
# to the class and available as +options+:
|
3929
|
-
#
|
3930
|
-
# class Person
|
3931
|
-
# include ActiveModel::Validations
|
3932
|
-
# validates_with MyValidator, my_custom_key: 'my custom value'
|
3933
|
-
# end
|
3934
|
-
#
|
3935
|
-
# class MyValidator < ActiveModel::Validator
|
3936
|
-
# def validate(record)
|
3937
|
-
# options[:my_custom_key] # => "my custom value"
|
3938
|
-
# end
|
3939
|
-
# end
|
3940
|
-
def validates_with: (*untyped args) { () -> untyped } -> untyped
|
3941
|
-
end
|
3942
|
-
|
3943
|
-
# Passes the record off to the class or classes specified and allows them
|
3944
|
-
# to add errors based on more complex conditions.
|
3945
|
-
#
|
3946
|
-
# class Person
|
3947
|
-
# include ActiveModel::Validations
|
3948
|
-
#
|
3949
|
-
# validate :instance_validations
|
3950
|
-
#
|
3951
|
-
# def instance_validations
|
3952
|
-
# validates_with MyValidator
|
3953
|
-
# end
|
3954
|
-
# end
|
3955
|
-
#
|
3956
|
-
# Please consult the class method documentation for more information on
|
3957
|
-
# creating your own validator.
|
3958
|
-
#
|
3959
|
-
# You may also pass it multiple classes, like so:
|
3960
|
-
#
|
3961
|
-
# class Person
|
3962
|
-
# include ActiveModel::Validations
|
3963
|
-
#
|
3964
|
-
# validate :instance_validations, on: :create
|
3965
|
-
#
|
3966
|
-
# def instance_validations
|
3967
|
-
# validates_with MyValidator, MyOtherValidator
|
3968
|
-
# end
|
3969
|
-
# end
|
3970
|
-
#
|
3971
|
-
# Standard configuration options (<tt>:on</tt>, <tt>:if</tt> and
|
3972
|
-
# <tt>:unless</tt>), which are available on the class version of
|
3973
|
-
# +validates_with+, should instead be placed on the +validates+ method
|
3974
|
-
# as these are applied and tested in the callback.
|
3975
|
-
#
|
3976
|
-
# If you pass any additional configuration options, they will be passed
|
3977
|
-
# to the class and available as +options+, please refer to the
|
3978
|
-
# class version of this method for more information.
|
3979
|
-
def validates_with: (*untyped args) { () -> untyped } -> untyped
|
3980
|
-
end
|
3981
|
-
end
|
3982
|
-
|
3983
|
-
module ActiveModel
|
3984
|
-
# == Active \Model \Validator
|
3985
|
-
#
|
3986
|
-
# A simple base class that can be used along with
|
3987
|
-
# ActiveModel::Validations::ClassMethods.validates_with
|
3988
|
-
#
|
3989
|
-
# class Person
|
3990
|
-
# include ActiveModel::Validations
|
3991
|
-
# validates_with MyValidator
|
3992
|
-
# end
|
3993
|
-
#
|
3994
|
-
# class MyValidator < ActiveModel::Validator
|
3995
|
-
# def validate(record)
|
3996
|
-
# if some_complex_logic
|
3997
|
-
# record.errors.add(:base, "This record is invalid")
|
3998
|
-
# end
|
3999
|
-
# end
|
4000
|
-
#
|
4001
|
-
# private
|
4002
|
-
# def some_complex_logic
|
4003
|
-
# # ...
|
4004
|
-
# end
|
4005
|
-
# end
|
4006
|
-
#
|
4007
|
-
# Any class that inherits from ActiveModel::Validator must implement a method
|
4008
|
-
# called +validate+ which accepts a +record+.
|
4009
|
-
#
|
4010
|
-
# class Person
|
4011
|
-
# include ActiveModel::Validations
|
4012
|
-
# validates_with MyValidator
|
4013
|
-
# end
|
4014
|
-
#
|
4015
|
-
# class MyValidator < ActiveModel::Validator
|
4016
|
-
# def validate(record)
|
4017
|
-
# record # => The person instance being validated
|
4018
|
-
# options # => Any non-standard options passed to validates_with
|
4019
|
-
# end
|
4020
|
-
# end
|
4021
|
-
#
|
4022
|
-
# To cause a validation error, you must add to the +record+'s errors directly
|
4023
|
-
# from within the validators message.
|
4024
|
-
#
|
4025
|
-
# class MyValidator < ActiveModel::Validator
|
4026
|
-
# def validate(record)
|
4027
|
-
# record.errors.add :base, "This is some custom error message"
|
4028
|
-
# record.errors.add :first_name, "This is some complex validation"
|
4029
|
-
# # etc...
|
4030
|
-
# end
|
4031
|
-
# end
|
4032
|
-
#
|
4033
|
-
# To add behavior to the initialize method, use the following signature:
|
4034
|
-
#
|
4035
|
-
# class MyValidator < ActiveModel::Validator
|
4036
|
-
# def initialize(options)
|
4037
|
-
# super
|
4038
|
-
# @my_custom_field = options[:field_name] || :first_name
|
4039
|
-
# end
|
4040
|
-
# end
|
4041
|
-
#
|
4042
|
-
# Note that the validator is initialized only once for the whole application
|
4043
|
-
# life cycle, and not on each validation run.
|
4044
|
-
#
|
4045
|
-
# The easiest way to add custom validators for validating individual attributes
|
4046
|
-
# is with the convenient <tt>ActiveModel::EachValidator</tt>.
|
4047
|
-
#
|
4048
|
-
# class TitleValidator < ActiveModel::EachValidator
|
4049
|
-
# def validate_each(record, attribute, value)
|
4050
|
-
# record.errors.add attribute, 'must be Mr., Mrs., or Dr.' unless %w(Mr. Mrs. Dr.).include?(value)
|
4051
|
-
# end
|
4052
|
-
# end
|
4053
|
-
#
|
4054
|
-
# This can now be used in combination with the +validates+ method
|
4055
|
-
# (see <tt>ActiveModel::Validations::ClassMethods.validates</tt> for more on this).
|
4056
|
-
#
|
4057
|
-
# class Person
|
4058
|
-
# include ActiveModel::Validations
|
4059
|
-
# attr_accessor :title
|
4060
|
-
#
|
4061
|
-
# validates :title, presence: true, title: true
|
4062
|
-
# end
|
4063
|
-
#
|
4064
|
-
# It can be useful to access the class that is using that validator when there are prerequisites such
|
4065
|
-
# as an +attr_accessor+ being present. This class is accessible via <tt>options[:class]</tt> in the constructor.
|
4066
|
-
# To setup your validator override the constructor.
|
4067
|
-
#
|
4068
|
-
# class MyValidator < ActiveModel::Validator
|
4069
|
-
# def initialize(options={})
|
4070
|
-
# super
|
4071
|
-
# options[:class].attr_accessor :custom_attribute
|
4072
|
-
# end
|
4073
|
-
# end
|
4074
|
-
class Validator
|
4075
|
-
attr_reader options: untyped
|
4076
|
-
|
4077
|
-
# Returns the kind of the validator.
|
4078
|
-
#
|
4079
|
-
# PresenceValidator.kind # => :presence
|
4080
|
-
# AcceptanceValidator.kind # => :acceptance
|
4081
|
-
def self.kind: () -> untyped
|
4082
|
-
|
4083
|
-
# Accepts options that will be made available through the +options+ reader.
|
4084
|
-
def initialize: (?::Hash[untyped, untyped] options) -> untyped
|
4085
|
-
|
4086
|
-
# Returns the kind for this validator.
|
4087
|
-
#
|
4088
|
-
# PresenceValidator.new(attributes: [:username]).kind # => :presence
|
4089
|
-
# AcceptanceValidator.new(attributes: [:terms]).kind # => :acceptance
|
4090
|
-
def kind: () -> untyped
|
4091
|
-
|
4092
|
-
# Override this method in subclasses with validation logic, adding errors
|
4093
|
-
# to the records +errors+ array where necessary.
|
4094
|
-
def validate: (untyped record) -> untyped
|
4095
|
-
end
|
4096
|
-
|
4097
|
-
class EachValidator < Validator
|
4098
|
-
# +EachValidator+ is a validator which iterates through the attributes given
|
4099
|
-
# in the options hash invoking the <tt>validate_each</tt> method passing in the
|
4100
|
-
# record, attribute and value.
|
4101
|
-
#
|
4102
|
-
# All \Active \Model validations are built on top of this validator.
|
4103
|
-
# nodoc:
|
4104
|
-
attr_reader attributes: untyped
|
4105
|
-
|
4106
|
-
# Returns a new validator instance. All options will be available via the
|
4107
|
-
# +options+ reader, however the <tt>:attributes</tt> option will be removed
|
4108
|
-
# and instead be made available through the +attributes+ reader.
|
4109
|
-
def initialize: (untyped options) -> untyped
|
4110
|
-
|
4111
|
-
# Performs validation on the supplied record. By default this will call
|
4112
|
-
# +validate_each+ to determine validity therefore subclasses should
|
4113
|
-
# override +validate_each+ with validation logic.
|
4114
|
-
def validate: (untyped record) -> untyped
|
4115
|
-
|
4116
|
-
# Override this method in subclasses with the validation logic, adding
|
4117
|
-
# errors to the records +errors+ array where necessary.
|
4118
|
-
def validate_each: (untyped record, untyped attribute, untyped value) -> untyped
|
4119
|
-
|
4120
|
-
# Hook method that gets called by the initializer allowing verification
|
4121
|
-
# that the arguments supplied are valid. You could for example raise an
|
4122
|
-
# +ArgumentError+ when invalid options are supplied.
|
4123
|
-
def check_validity!: () -> nil
|
4124
|
-
end
|
4125
|
-
|
4126
|
-
class BlockValidator < EachValidator
|
4127
|
-
# +BlockValidator+ is a special +EachValidator+ which receives a block on initialization
|
4128
|
-
# and call this block for each attribute being validated. +validates_each+ uses this validator.
|
4129
|
-
# nodoc:
|
4130
|
-
def initialize: (untyped options) { () -> untyped } -> untyped
|
4131
|
-
|
4132
|
-
def validate_each: (untyped record, untyped attribute, untyped value) -> untyped
|
4133
|
-
end
|
4134
|
-
end
|
4135
|
-
|
4136
|
-
module ActiveModel
|
4137
|
-
# Returns the version of the currently loaded \Active \Model as a <tt>Gem::Version</tt>
|
4138
|
-
def self.version: () -> untyped
|
4139
|
-
end
|