Module Sequel::Plugins::DeprecatedValidationClassMethods::ClassMethods
In: lib/sequel/model/deprecated_validations.rb

Methods

Classes and Modules

Class Sequel::Plugins::DeprecatedValidationClassMethods::ClassMethods::Generator

Public Instance methods

Returns true if validations are defined.

[Source]

    # File lib/sequel/model/deprecated_validations.rb, line 21
21:         def has_validations?
22:           !validations.empty?
23:         end

Instructs the model to skip validations defined in superclasses

[Source]

    # File lib/sequel/model/deprecated_validations.rb, line 26
26:         def skip_superclass_validations
27:           @skip_superclass_validations = true
28:         end

Validates the given instance.

[Source]

    # File lib/sequel/model/deprecated_validations.rb, line 51
51:         def validate(o)
52:           if superclass.respond_to?(:validate) && !@skip_superclass_validations
53:             superclass.validate(o)
54:           end
55:           validations.each do |att, procs|
56:             v = case att
57:             when Array
58:               att.collect{|a| o.send(a)}
59:             else
60:               o.send(att)
61:             end
62:             procs.each {|tag, p| p.call(o, att, v)}
63:           end
64:         end

Defines validations by converting a longhand block into a series of shorthand definitions. For example:

  class MyClass < Sequel::Model
    validates do
      length_of :name, :minimum => 6
      length_of :password, :minimum => 8
    end
  end

is equivalent to:

  class MyClass < Sequel::Model
    validates_length_of :name, :minimum => 6
    validates_length_of :password, :minimum => 8
  end

[Source]

    # File lib/sequel/model/deprecated_validations.rb, line 45
45:         def validates(&block)
46:           Deprecation.deprecate('Sequel::Model.validates', 'Use Model.plugin(:validation_class_methods) first')
47:           Generator.new(self, &block)
48:         end

Validates acceptance of an attribute. Just checks that the value is equal to the :accept option. This method is unique in that :allow_nil is assumed to be true instead of false.

Possible Options:

  • :accept - The value required for the object to be valid (default: ‘1’)
  • :message - The message to use (default: ‘is not accepted’)

[Source]

    # File lib/sequel/model/deprecated_validations.rb, line 73
73:         def validates_acceptance_of(*atts)
74:           Deprecation.deprecate('Sequel::Model.validates_acceptance_of', 'Use Model.plugin(:validation_class_methods) first')
75:           opts = {
76:             :message => 'is not accepted',
77:             :allow_nil => true,
78:             :accept => '1',
79:             :tag => :acceptance,
80:           }.merge!(extract_options!(atts))
81:           atts << opts
82:           validates_each(*atts) do |o, a, v|
83:             o.errors[a] << opts[:message] unless v == opts[:accept]
84:           end
85:         end

Validates confirmation of an attribute. Checks that the object has a _confirmation value matching the current value. For example:

  validates_confirmation_of :blah

Just makes sure that object.blah = object.blah_confirmation. Often used for passwords or email addresses on web forms.

Possible Options:

  • :message - The message to use (default: ‘is not confirmed’)

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 97
 97:         def validates_confirmation_of(*atts)
 98:           Deprecation.deprecate('Sequel::Model.validates_confirmation_of', 'Use Model.plugin(:validation_class_methods) first')
 99:           opts = {
100:             :message => 'is not confirmed',
101:             :tag => :confirmation,
102:           }.merge!(extract_options!(atts))
103:           atts << opts
104:           validates_each(*atts) do |o, a, v|
105:             o.errors[a] << opts[:message] unless v == o.send("#{a}_confirmation""#{a}_confirmation")
106:           end
107:         end

Adds a validation for each of the given attributes using the supplied block. The block must accept three arguments: instance, attribute and value, e.g.:

  validates_each :name, :password do |object, attribute, value|
    object.errors[attribute] << 'is not nice' unless value.nice?
  end

Possible Options:

  • :allow_blank - Whether to skip the validation if the value is blank.
  • :allow_missing - Whether to skip the validation if the attribute isn‘t a key in the values hash. This is different from allow_nil, because Sequel only sends the attributes in the values when doing an insert or update. If the attribute is not present, Sequel doesn‘t specify it, so the database will use the table‘s default value. This is different from having an attribute in values with a value of nil, which Sequel will send as NULL. If your database table has a non NULL default, this may be a good option to use. You don‘t want to use allow_nil, because if the attribute is in values but has a value nil, Sequel will attempt to insert a NULL value into the database, instead of using the database‘s default.
  • :allow_nil - Whether to skip the validation if the value is nil.
  • :if - A symbol (indicating an instance_method) or proc (which is instance_evaled) skipping this validation if it returns nil or false.
  • :tag - The tag to use for this validation.

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 132
132:         def validates_each(*atts, &block)
133:           Deprecation.deprecate('Sequel::Model.validates_each', 'Use Model.plugin(:validation_class_methods) first')
134:           opts = extract_options!(atts)
135:           blk = if (i = opts[:if]) || (am = opts[:allow_missing]) || (an = opts[:allow_nil]) || (ab = opts[:allow_blank])
136:             proc do |o,a,v|
137:               next if i && !validation_if_proc(o, i)
138:               next if an && Array(v).all?{|x| x.nil?}
139:               next if ab && Array(v).all?{|x| x.blank?}
140:               next if am && Array(a).all?{|x| !o.values.has_key?(x)}
141:               block.call(o,a,v)
142:             end
143:           else
144:             block
145:           end
146:           tag = opts[:tag]
147:           atts.each do |a| 
148:             a_vals = validations[a]
149:             if tag && (old = a_vals.find{|x| x[0] == tag})
150:               old[1] = blk
151:             else
152:               a_vals << [tag, blk]
153:             end
154:           end
155:         end

Validates the format of an attribute, checking the string representation of the value against the regular expression provided by the :with option.

Possible Options:

  • :message - The message to use (default: ‘is invalid’)
  • :with - The regular expression to validate the value with (required).

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 163
163:         def validates_format_of(*atts)
164:           Deprecation.deprecate('Sequel::Model.validates_format_of', 'Use Model.plugin(:validation_class_methods) first')
165:           opts = {
166:             :message => 'is invalid',
167:             :tag => :format,
168:           }.merge!(extract_options!(atts))
169:           
170:           unless opts[:with].is_a?(Regexp)
171:             raise ArgumentError, "A regular expression must be supplied as the :with option of the options hash"
172:           end
173:           
174:           atts << opts
175:           validates_each(*atts) do |o, a, v|
176:             o.errors[a] << opts[:message] unless v.to_s =~ opts[:with]
177:           end
178:         end

Validates that an attribute is within a specified range or set of values.

Possible Options:

  • :in - An array or range of values to check for validity (required)
  • :message - The message to use (default: ‘is not in range or set: <specified range>’)

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 294
294:         def validates_inclusion_of(*atts)
295:           Deprecation.deprecate('Sequel::Model.validates_inclusion_of', 'Use Model.plugin(:validation_class_methods) first')
296:           opts = extract_options!(atts)
297:           unless opts[:in] && opts[:in].respond_to?(:include?) 
298:             raise ArgumentError, "The :in parameter is required, and respond to include?"
299:           end
300:           opts[:message] ||= "is not in range or set: #{opts[:in].inspect}"
301:           atts << opts
302:           validates_each(*atts) do |o, a, v|
303:             o.errors[a] << opts[:message] unless opts[:in].include?(v)
304:           end
305:         end

Validates the length of an attribute.

Possible Options:

  • :is - The exact size required for the value to be valid (no default)
  • :maximum - The maximum size allowed for the value (no default)
  • :message - The message to use (no default, overrides :too_long, :too_short, and :wrong_length options if present)
  • :minimum - The minimum size allowed for the value (no default)
  • :too_long - The message to use use if it the value is too long (default: ‘is too long’)
  • :too_short - The message to use use if it the value is too short (default: ‘is too short’)
  • :within - The array/range that must include the size of the value for it to be valid (no default)
  • :wrong_length - The message to use use if it the value is not valid (default: ‘is the wrong length’)

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 192
192:         def validates_length_of(*atts)
193:           Deprecation.deprecate('Sequel::Model.validates_length_of', 'Use Model.plugin(:validation_class_methods) first')
194:           opts = {
195:             :too_long     => 'is too long',
196:             :too_short    => 'is too short',
197:             :wrong_length => 'is the wrong length'
198:           }.merge!(extract_options!(atts))
199:           
200:           opts[:tag] ||= ([:length] + [:maximum, :minimum, :is, :within].reject{|x| !opts.include?(x)}).join('-').to_sym
201:           atts << opts
202:           validates_each(*atts) do |o, a, v|
203:             if m = opts[:maximum]
204:               o.errors[a] << (opts[:message] || opts[:too_long]) unless v && v.size <= m
205:             end
206:             if m = opts[:minimum]
207:               o.errors[a] << (opts[:message] || opts[:too_short]) unless v && v.size >= m
208:             end
209:             if i = opts[:is]
210:               o.errors[a] << (opts[:message] || opts[:wrong_length]) unless v && v.size == i
211:             end
212:             if w = opts[:within]
213:               o.errors[a] << (opts[:message] || opts[:wrong_length]) unless v && w.include?(v.size)
214:             end
215:           end
216:         end

Validates whether an attribute is not a string. This is generally useful in conjunction with raise_on_typecast_failure = false, where you are passing in string values for non-string attributes (such as numbers and dates). If typecasting fails (invalid number or date), the value of the attribute will be a string in an invalid format, and if typecasting succeeds, the value will not be a string.

Possible Options:

  • :message - The message to use (default: ‘is a string’ or ‘is not a valid (integer|datetime|etc.)’ if the type is known)

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 227
227:         def validates_not_string(*atts)
228:           Deprecation.deprecate('Sequel::Model.validates_not_string', 'Use Model.plugin(:validation_class_methods) first')
229:           opts = {
230:             :tag => :not_string,
231:           }.merge!(extract_options!(atts))
232:           atts << opts
233:           validates_each(*atts) do |o, a, v|
234:             if v.is_a?(String)
235:               unless message = opts[:message]
236:                 message = if sch = o.db_schema[a] and typ = sch[:type]
237:                   "is not a valid #{typ}"
238:                 else
239:                   "is a string"
240:                 end
241:               end
242:               o.errors[a] << message
243:             end
244:           end
245:         end

Validates whether an attribute is a number.

Possible Options:

  • :message - The message to use (default: ‘is not a number’)
  • :only_integer - Whether only integers are valid values (default: false)

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 252
252:         def validates_numericality_of(*atts)
253:           Deprecation.deprecate('Sequel::Model.validates_numericality_of', 'Use Model.plugin(:validation_class_methods) first')
254:           opts = {
255:             :message => 'is not a number',
256:             :tag => :numericality,
257:           }.merge!(extract_options!(atts))
258:           atts << opts
259:           validates_each(*atts) do |o, a, v|
260:             begin
261:               if opts[:only_integer]
262:                 Kernel.Integer(v.to_s)
263:               else
264:                 Kernel.Float(v.to_s)
265:               end
266:             rescue
267:               o.errors[a] << opts[:message]
268:             end
269:           end
270:         end

Validates the presence of an attribute. Requires the value not be blank, with false considered present instead of absent.

Possible Options:

  • :message - The message to use (default: ‘is not present’)

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 277
277:         def validates_presence_of(*atts)
278:           Deprecation.deprecate('Sequel::Model.validates_presence_of', 'Use Model.plugin(:validation_class_methods) first')
279:           opts = {
280:             :message => 'is not present',
281:             :tag => :presence,
282:           }.merge!(extract_options!(atts))
283:           atts << opts
284:           validates_each(*atts) do |o, a, v|
285:             o.errors[a] << opts[:message] if v.blank? && v != false
286:           end
287:         end

Validates only if the fields in the model (specified by atts) are unique in the database. Pass an array of fields instead of multiple fields to specify that the combination of fields must be unique, instead of that each field should have a unique value.

This means that the code:

  validates_uniqueness_of([:column1, :column2])

validates the grouping of column1 and column2 while

  validates_uniqueness_of(:column1, :column2)

validates them separately.

You should also add a unique index in the database, as this suffers from a fairly obvious race condition.

Possible Options:

  • :message - The message to use (default: ‘is already taken’)

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 323
323:         def validates_uniqueness_of(*atts)
324:           Deprecation.deprecate('Sequel::Model.validates_uniqueness_of', 'Use Model.plugin(:validation_class_methods) first')
325:           opts = {
326:             :message => 'is already taken',
327:             :tag => :uniqueness,
328:           }.merge!(extract_options!(atts))
329:     
330:           atts << opts
331:           validates_each(*atts) do |o, a, v|
332:             error_field = a
333:             a = Array(a)
334:             v = Array(v)
335:             ds = o.class.filter(a.zip(v))
336:             num_dups = ds.count
337:             allow = if num_dups == 0
338:               # No unique value in the database
339:               true
340:             elsif num_dups > 1
341:               # Multiple "unique" values in the database!!
342:               # Someone didn't add a unique index
343:               false
344:             elsif o.new?
345:               # New record, but unique value already exists in the database
346:               false
347:             elsif ds.first === o
348:               # Unique value exists in database, but for the same record, so the update won't cause a duplicate record
349:               true
350:             else
351:               false
352:             end
353:             o.errors[error_field] << opts[:message] unless allow
354:           end
355:         end

Returns the validations hash for the class.

[Source]

     # File lib/sequel/model/deprecated_validations.rb, line 358
358:         def validations
359:           @validations ||= Hash.new {|h, k| h[k] = []}
360:         end

[Validate]