Module: ActiveModel::Validations::ClassMethods

Defined in:
lib/validators/validates_datetime.rb,
lib/validators/validates_ip_address.rb,
lib/validators/validates_ownership_of.rb,
lib/validators/validates_cpf_format_of.rb,
lib/validators/validates_url_format_of.rb,
lib/validators/validates_cnpj_format_of.rb,
lib/validators/validates_ssh_public_key.rb,
lib/validators/validates_email_format_of.rb,
lib/validators/validates_ssh_private_key.rb,
lib/validators/validates_hostname_format_of.rb

Instance Method Summary collapse

Instance Method Details

#validates_cnpj_format_of(*attr_names) ⇒ Object Also known as: validates_cnpj

Validates whether or not the specified CNPJ is valid.

class User < ActiveRecord::Base
  validates_cnpj_format_of :document
end


22
23
24
25
26
27
# File 'lib/validators/validates_cnpj_format_of.rb', line 22

def validates_cnpj_format_of(*attr_names)
  require "cnpj"
  validates_with CnpjValidator, _merge_attributes(attr_names)
rescue LoadError
  fail "cpf_cnpj is not part of the bundle. Add it to Gemfile."
end

#validates_cpf_format_of(*attr_names) ⇒ Object Also known as: validates_cpf

Validates whether or not the specified CPF is valid.

class User < ActiveRecord::Base
  validates_cpf_format_of :document
end


22
23
24
25
26
27
# File 'lib/validators/validates_cpf_format_of.rb', line 22

def validates_cpf_format_of(*attr_names)
  require "cpf"
  validates_with CpfValidator, _merge_attributes(attr_names)
rescue LoadError
  fail "cpf_cnpj is not part of the bundle. Add it to Gemfile."
end

#validates_datetime(*attr_names) ⇒ Object

Validates whether or not the specified e-mail address is valid.

class User < ActiveRecord::Base
  validates_datetime :birth
end

Other usages:

validates_datetime :starts_at, :after => 2.years.ago
validates_datetime :starts_at, :before => 2.years.ago
validates_datetime :starts_at, :before => :today
validates_datetime :starts_at, :before => :now
validates_datetime :starts_at, :before => :ends_at
validates_datetime :ends_at, :after => :starts_at


89
90
91
# File 'lib/validators/validates_datetime.rb', line 89

def validates_datetime(*attr_names)
  validates_with DatetimeValidator, _merge_attributes(attr_names)
end

#validates_email_format_of(*attr_names) ⇒ Object Also known as: validates_email

Validates whether or not the specified e-mail address is valid.

class User < ActiveRecord::Base
  validates_email_format_of :email
end


40
41
42
# File 'lib/validators/validates_email_format_of.rb', line 40

def validates_email_format_of(*attr_names)
  validates_with EmailValidator, _merge_attributes(attr_names)
end

#validates_hostname_format_of(*attr_names) ⇒ Object Also known as: validates_hostname

Validates whether or not the specified URL is valid.

class User < ActiveRecord::Base
  validates_hostname_format_of :site

  # Validates against a list of valid TLD.
  validates_hostname_format_of :site, tld: true
end


55
56
57
# File 'lib/validators/validates_hostname_format_of.rb', line 55

def validates_hostname_format_of(*attr_names)
  validates_with HostnameValidator, _merge_attributes(attr_names)
end

#validates_ip_address(*attr_names) ⇒ Object

Validates whether or not the specified URL is valid.

validates_ip_address :ip  #=> accepts both v4 and v6
validates_ip_address :ip, :only => :v4
validates_ip_address :ip, :only => :v6


42
43
44
# File 'lib/validators/validates_ip_address.rb', line 42

def validates_ip_address(*attr_names)
  validates_with IpAddressValidator, _merge_attributes(attr_names)
end

#validates_ownership_of(*attr_names) ⇒ Object

Validates whether the owner of the specified attribute is the same from the current object.

class Task < ActiveRecord::Base
  belongs_to :user
  belongs_to :category

  validates_ownership_of :category, :with => :user
end

user = User.find(1)
another_user = User.find(2)

user_category = user.categories.first
another_user_category = another_user.categories.first

task = user.tasks.create(:category => user_category)
task.valid?
#=> true

task = user.tasks.create(:category => another_user_category)
task.valid?
#=> false


46
47
48
# File 'lib/validators/validates_ownership_of.rb', line 46

def validates_ownership_of(*attr_names)
  validates_with OwnershipValidator, _merge_attributes(attr_names)
end

#validates_ssh_private_key(*attr_names) ⇒ Object

Validates whether or not the specified CNPJ is valid.

class User < ActiveRecord::Base
  validates_ssh_private_key :key
end


50
51
52
53
54
55
# File 'lib/validators/validates_ssh_private_key.rb', line 50

def validates_ssh_private_key(*attr_names)
  require "sshkey"
  validates_with SshPrivateKeyValidator, _merge_attributes(attr_names)
rescue LoadError
  fail "sshkey is not part of the bundle. Add it to Gemfile."
end

#validates_ssh_public_key(*attr_names) ⇒ Object

Validates whether or not the specified CNPJ is valid.

class User < ActiveRecord::Base
  validates_ssh_public_key :key
end


22
23
24
25
26
27
# File 'lib/validators/validates_ssh_public_key.rb', line 22

def validates_ssh_public_key(*attr_names)
  require "sshkey"
  validates_with SshPublicKeyValidator, _merge_attributes(attr_names)
rescue LoadError
  fail "sshkey is not part of the bundle. Add it to Gemfile."
end

#validates_url_format_of(*attr_names) ⇒ Object Also known as: validates_url

Validates whether or not the specified URL is valid.

class User < ActiveRecord::Base
  validates_url_format_of :site

  # Validates against a list of valid TLD.
  validates_url_format_of :site, tld: true
end


43
44
45
# File 'lib/validators/validates_url_format_of.rb', line 43

def validates_url_format_of(*attr_names)
  validates_with UrlValidator, _merge_attributes(attr_names)
end