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