Module: Bounga::ActiveRecord::Validations::ClassMethods

Defined in:
lib/ar-validations.rb

Overview

This ActiveRecord extension provides more validation schemes. You can validate:

  • email: It checks for presence, length, format and uniqueness. There is no guarantee that a validated email is real and deliverable.

  • URLs: It checks for validity of the link by contacting it. If not protocol is not specified, it’ll automatically add “http://” in front of the url.

User example:

class User < ActiveRecord::Base
  validates_email :email
  validates_url :url
end

Instance Method Summary collapse

Instance Method Details

#validates_email(*fields) ⇒ Object

Configuration options are:

  • :allow_blank - If set to true, skips this validation if the attribute is blank (default: false)

  • :allow_nil - If set to true, skips this validation if the attribute is nil (default: false)

  • :on - Specifies when this validation is active (default is :save, other options :create, :update)

  • :with - The regular expression used to validate the email format with (default: RFC-2822 compliant)

  • :uniq - If set to true, ensure uniqueness of the attribute (default: false)

  • :if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.

  • :unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.

  • :message - A custom error message (default: "should look like an email address.")



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ar-validations.rb', line 47

def validates_email(*fields)
  options = { :message => MSG_EMAIL_BAD, :on => :save, :with => RE_EMAIL_OK, :allow_blank => false, :allow_nil => false }
  options.update(fields.extract_options!)
  
  validates_each(fields, options) do |record, attr, value|
    validates_presence_of(attr) unless options[:allow_nil] or options[:allow_blank]
    
    with_options(:allow_blank => options[:allow_blank], :allow_nil => options[:allow_nil]) do |el|
      el.validates_length_of     attr, :within => 6..100
      el.validates_uniqueness_of attr, :case_sensitive => false if options[:uniq]
      el.validates_format_of     attr, :with => options[:with], :message => options[:message]
    end
  end
end

#validates_url(*fields) ⇒ Object

Configuration options are:

  • :allow_blank - If set to true, skips this validation if the attribute is blank (default: false)

  • :allow_nil - If set to true, skips this validation if the attribute is nil (default: false)

  • :on - Specifies when this validation is active (default is :save, other options :create, :update)

  • :with - The regular expression used to validate the format with. Custom regexp will disable real URI verification (no contact with URI) (default: RE_URL_OK)

  • :if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.

  • :unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.

  • :message - A custom error message (default: "should be a valid url.")



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ar-validations.rb', line 76

def validates_url(*fields)
  options = { :message => MSG_URL_BAD, :on => :save, :with => RE_URL_OK, :allow_blank => false, :allow_nil => false }
  options.update(fields.extract_options!)

  validates_each(fields, options) do |record, attr, value|
    validates_presence_of(attr) unless options[:allow_nil] or options[:allow_blank]

    if options[:with] == RE_URL_OK
      record.send(attr.to_s + "=", "http://#{value}") unless value =~ options[:with] 
      open(record.send(attr)) rescue record.errors.add(attr, options[:message]) unless value.blank?
    else
      record.errors.add(attr, options[:message]) unless value =~ options[:with]
    end
  end
end