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


27
28
29
30
31
32
# File 'lib/validators/validates_cnpj_format_of.rb', line 27

def validates_cnpj_format_of(*attr_names)
  require "cnpj"
  validates_with CnpjValidator, _merge_attributes(attr_names)
rescue LoadError
  raise "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


27
28
29
30
31
32
# File 'lib/validators/validates_cpf_format_of.rb', line 27

def validates_cpf_format_of(*attr_names)
  require "cpf"
  validates_with CpfValidator, _merge_attributes(attr_names)
rescue LoadError
  raise "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


101
102
103
# File 'lib/validators/validates_datetime.rb', line 101

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


66
67
68
# File 'lib/validators/validates_email_format_of.rb', line 66

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


31
32
33
# File 'lib/validators/validates_hostname_format_of.rb', line 31

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


50
51
52
# File 'lib/validators/validates_ip_address.rb', line 50

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


52
53
54
# File 'lib/validators/validates_ownership_of.rb', line 52

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


58
59
60
61
62
63
# File 'lib/validators/validates_ssh_private_key.rb', line 58

def validates_ssh_private_key(*attr_names)
  require "sshkey"
  validates_with SshPrivateKeyValidator, _merge_attributes(attr_names)
rescue LoadError
  raise "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


27
28
29
30
31
32
# File 'lib/validators/validates_ssh_public_key.rb', line 27

def validates_ssh_public_key(*attr_names)
  require "sshkey"
  validates_with SshPublicKeyValidator, _merge_attributes(attr_names)
rescue LoadError
  raise "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


51
52
53
# File 'lib/validators/validates_url_format_of.rb', line 51

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