module BigRecord::Validations

Active Records implement validation by overwriting Base#validate (or the variations, validate_on_create and validate_on_update). Each of these methods can inspect the state of the object, which usually means ensuring that a number of attributes have a certain value (such as not empty, within a given range, matching a certain regular expression).

Example:

class Person < ActiveRecord::Base
  protected
    def validate
      errors.add_on_empty %w( first_name last_name )
      errors.add("phone_number", "has invalid format") unless phone_number =~ /[0-9]*/
    end

    def validate_on_create # is only run the first time a new object is saved
      unless valid_discount?(membership_discount)
        errors.add("membership_discount", "has expired")
      end
    end

    def validate_on_update
      errors.add_to_base("No changes have occurred") if unchanged_attributes?
    end
end

person = Person.new("first_name" => "David", "phone_number" => "what?")
person.save                         # => false (and doesn't do the save)
person.errors.empty?                # => false
person.errors.count                 # => 2
person.errors.on "last_name"        # => "can't be empty"
person.errors.on "phone_number"     # => "has invalid format"
person.errors.each_full { |msg| puts msg }
                                    # => "Last name can't be empty\n" +
                                         "Phone number has invalid format"

person.attributes = { "last_name" => "Heinemeier", "phone_number" => "555-555" }
person.save # => true (and person is now saved in the database)

An Errors object is automatically created for every Active Record.

Please do have a look at ActiveRecord::Validations::ClassMethods for a higher level of validations.

Constants

VALIDATIONS

Public Instance Methods

errors() click to toggle source

Returns the Errors object that holds all information about attribute error messages.

# File lib/big_record/validations.rb, line 784
def errors
  @errors ||= Errors.new(self)
end
save_with_validation(perform_validation = true) click to toggle source

The validation process on save can be skipped by passing false. The regular BigRecord::Base#save method is replaced with this when the validations module is mixed in, which it is by default.

# File lib/big_record/validations.rb, line 739
def save_with_validation(perform_validation = true)
  if perform_validation && valid? || !perform_validation
    save_without_validation
  else
    false
  end
end
save_with_validation!() click to toggle source

Attempts to save the record just like BigRecord::Base#save but will raise a RecordInvalid exception instead of returning false if the record is not valid.

# File lib/big_record/validations.rb, line 749
def save_with_validation!
  if valid?
    save_without_validation!
  else
    raise RecordInvalid.new(self)
  end
end
update_attribute_with_validation_skipping(name, value) click to toggle source

Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.

# File lib/big_record/validations.rb, line 760
def update_attribute_with_validation_skipping(name, value)
  send(name.to_s + '=', value)
  save(false)
end
valid?() click to toggle source

Runs validate and #validate_on_create or #validate_on_update and returns true if no errors were added otherwise false.

# File lib/big_record/validations.rb, line 766
def valid?
  errors.clear

  run_validations(:validate)
  validate

  if new_record?
    run_validations(:validate_on_create)
    validate_on_create
  else
    run_validations(:validate_on_update)
    validate_on_update
  end

  errors.empty?
end

Protected Instance Methods

validate() click to toggle source

Overwrite this method for validation checks on all saves and use BigRecord::Errors#add(field, msg) for invalid attributes.

# File lib/big_record/validations.rb, line 790
def validate #:doc:
end
validate_on_create() click to toggle source

Overwrite this method for validation checks used only on creation.

# File lib/big_record/validations.rb, line 794
def validate_on_create #:doc:
end
validate_on_update() click to toggle source

Overwrite this method for validation checks used only on updates.

# File lib/big_record/validations.rb, line 798
def validate_on_update # :doc:
end

Private Instance Methods

run_validations(validation_method) click to toggle source
# File lib/big_record/validations.rb, line 802
def run_validations(validation_method)
  validations = self.class.read_inheritable_attribute(validation_method.to_sym)
  if validations.nil? then return end
  validations.each do |validation|
    if validation.is_a?(Symbol)
      self.send(validation)
    elsif validation.is_a?(String)
      eval(validation, binding)
    elsif validation_block?(validation)
      validation.call(self)
    elsif validation_class?(validation, validation_method)
      validation.send(validation_method, self)
    else
      raise(
        BigRecord,
        "Validations need to be either a symbol, string (to be eval'ed), proc/method, or " +
        "class implementing a static validation method"
      )
    end
  end
end
validation_block?(validation) click to toggle source
# File lib/big_record/validations.rb, line 824
def validation_block?(validation)
  validation.respond_to?("call") && (validation.arity == 1 || validation.arity == -1)
end
validation_class?(validation, validation_method) click to toggle source
# File lib/big_record/validations.rb, line 828
def validation_class?(validation, validation_method)
  validation.respond_to?(validation_method)
end