Module: Poncho::Validations::ClassMethods
- Defined in:
- lib/poncho/validations.rb
Constant Summary collapse
- VALIDATES_DEFAULT_KEYS =
[:if, :unless, :on, :allow_blank, :allow_nil , :strict]
Instance Method Summary collapse
-
#validate(proc = nil, &block) ⇒ Object
Adds a validation method or block to the class.
- #validates(*attributes) ⇒ Object
-
#validates_each(*attr_names, &block) ⇒ Object
Validates each attribute against a block.
- #validates_with(*args, &block) ⇒ Object
-
#validators ⇒ Object
List all validators that are being used to validate the model using
validates_withmethod.
Instance Method Details
#validate(proc = nil, &block) ⇒ Object
Adds a validation method or block to the class. This is useful when overriding the validate instance method becomes too unwieldy and you’re looking for more descriptive declaration of your validations.
This can be done with a symbol pointing to a method:
class Comment
include Poncho::Validations
validate :must_be_friends
def must_be_friends
errors.add(:base, "Must be friends to leave a comment") unless commenter.friend_of?(commentee)
end
end
With a block which is passed with the current record to be validated:
class Comment
include Poncho::Validations
validate do |comment|
comment.must_be_friends
end
def must_be_friends
errors.add(:base, "Must be friends to leave a comment") unless commenter.friend_of?(commentee)
end
end
Or with a block where self points to the current record to be validated:
class Comment
include Poncho::Validations
validate do
errors.add(:base, "Must be friends to leave a comment") unless commenter.friend_of?(commentee)
end
end
117 118 119 120 121 122 |
# File 'lib/poncho/validations.rb', line 117 def validate(proc = nil, &block) proc ||= block proc = method(proc) if proc.is_a?(Symbol) validators << proc end |
#validates(*attributes) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/poncho/validations.rb', line 139 def validates(*attributes) = attributes.last.is_a?(::Hash) ? attributes.pop : {} validations = .reject {|key, value| VALIDATES_DEFAULT_KEYS.include?(key) || !value } = .merge(:attributes => attributes) validations.each do |key, | = {} if == true validates_with(validator_for_kind(key), .merge(:attributes => attributes)) end end |
#validates_each(*attr_names, &block) ⇒ Object
Validates each attribute against a block.
class Person
include Poncho::Validations
attr_accessor :first_name, :last_name
validates_each :first_name, :last_name do |record, attr, value|
record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
end
end
Options:
-
:on- Specifies the context where this validation is active (e.g.:on => :createor:on => :custom_validation_context) -
:allow_nil- Skip validation if attribute isnil. -
:allow_blank- Skip validation if attribute is blank. -
: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.
72 73 74 75 |
# File 'lib/poncho/validations.rb', line 72 def validates_each(*attr_names, &block) = attr_names.last.is_a?(::Hash) ? attr_names.pop : {} validates_with BlockValidator, .merge(:attributes => attr_names.flatten), &block end |
#validates_with(*args, &block) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/poncho/validations.rb', line 130 def validates_with(*args, &block) = args.last.is_a?(::Hash) ? args.pop : {} args.each do |klass| validator = klass.new(, &block) validate(validator.method(:validate)) end end |
#validators ⇒ Object
List all validators that are being used to validate the model using validates_with method.
126 127 128 |
# File 'lib/poncho/validations.rb', line 126 def validators @validators ||= [] end |