module ActiveModel::Validations::HelperMethods

Use these validators in your model

Public Instance Methods

validates_presence_of_flags(*attr_names) click to toggle source

Validates that the specified attributes are flags and are not blank. Happens by default on save. Example:

class Spaceship < ActiveRecord::Base
  include FlagShihTzu

  has_flags({ 1 => :warpdrive, 2 => :hyperspace }, :column => 'engines')
  validates_presence_of_flags :engines
end

The engines attribute must be a flag in the object and it cannot be blank.

Configuration options:

  • :message - A custom error message (default is: “can't be blank”).

  • :on - Specifies when this validation is active. Runs in all validation contexts by default (nil), other options are :create and :update.

  • :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 { |spaceship| spaceship.warp_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.

  • :strict - Specifies whether validation should be strict. See ActiveModel::Validation#validates! for more information.

# File lib/flag_shih_tzu/validators.rb, line 64
def validates_presence_of_flags(*attr_names)
  validates_with PresenceOfFlagsValidator, _merge_attributes(attr_names)
end