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
-
#validates_email(*fields) ⇒ Object
Configuration options are:.
-
#validates_url(*fields) ⇒ Object
Configuration options are:.
Instance Method Details
#validates_email(*fields) ⇒ Object
Configuration options are:
-
:allow_blank
- If set totrue
, skips this validation if the attribute is blank (default:false
) -
:allow_nil
- If set totrue
, 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 totrue
, 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) = { :message => MSG_EMAIL_BAD, :on => :save, :with => RE_EMAIL_OK, :allow_blank => false, :allow_nil => false } .update(fields.) validates_each(fields, ) do |record, attr, value| validates_presence_of(attr) unless [:allow_nil] or [:allow_blank] (:allow_blank => [:allow_blank], :allow_nil => [:allow_nil]) do |el| el.validates_length_of attr, :within => 6..100 el.validates_uniqueness_of attr, :case_sensitive => false if [:uniq] el.validates_format_of attr, :with => [:with], :message => [:message] end end end |
#validates_url(*fields) ⇒ Object
Configuration options are:
-
:allow_blank
- If set totrue
, skips this validation if the attribute is blank (default:false
) -
:allow_nil
- If set totrue
, 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) = { :message => MSG_URL_BAD, :on => :save, :with => RE_URL_OK, :allow_blank => false, :allow_nil => false } .update(fields.) validates_each(fields, ) do |record, attr, value| validates_presence_of(attr) unless [:allow_nil] or [:allow_blank] if [:with] == RE_URL_OK record.send(attr.to_s + "=", "http://#{value}") unless value =~ [:with] open(record.send(attr)) rescue record.errors.add(attr, [:message]) unless value.blank? else record.errors.add(attr, [:message]) unless value =~ [:with] end end end |