Module: Validatable
- Defined in:
- lib/errors.rb,
lib/macros.rb,
lib/requireable.rb,
lib/understandable.rb,
lib/child_validation.rb,
lib/included_validation.rb,
lib/validatable_class_methods.rb,
lib/validations/validates_each.rb,
lib/validations/validation_base.rb,
lib/validatable_instance_methods.rb,
lib/validations/validates_true_for.rb,
lib/validations/validates_format_of.rb,
lib/validations/validates_length_of.rb,
lib/validations/validates_associated.rb,
lib/validations/validates_presence_of.rb,
lib/validations/validates_exclusion_of.rb,
lib/validations/validates_inclusion_of.rb,
lib/validations/validates_acceptance_of.rb,
lib/validations/validates_uniqueness_of.rb,
lib/validations/validates_confirmation_of.rb,
lib/validations/validates_numericality_of.rb
Defined Under Namespace
Modules: ClassMethods, Macros, Requireable, Understandable Classes: ChildValidation, Errors, IncludedValidation, ValidatesAcceptanceOf, ValidatesAssociated, ValidatesConfirmationOf, ValidatesEach, ValidatesExclusionOf, ValidatesFormatOf, ValidatesInclusionOf, ValidatesLengthOf, ValidatesNumericalityOf, ValidatesPresenceOf, ValidatesTrueFor, ValidatesUniquenessOf, ValidationBase
Class Method Summary collapse
-
.included(klass) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#errors ⇒ Object
call-seq: errors.
-
#increment_times_validated_for(validation) ⇒ Object
:nodoc:.
-
#times_validated(key) ⇒ Object
:nodoc:.
-
#valid? ⇒ Boolean
call-seq: valid?.
-
#valid_for_group?(group) ⇒ Boolean
:nodoc:.
-
#validate_only(key) ⇒ Object
call-seq: validate_only(key).
Class Method Details
.included(klass) ⇒ Object
:nodoc:
2 3 4 5 6 7 8 9 |
# File 'lib/validatable_instance_methods.rb', line 2 def self.included(klass) #:nodoc: klass.extend Validatable::ClassMethods klass.extend Validatable::Macros klass.class_eval do include ActiveSupport::Callbacks define_callbacks :before_validation, :after_validation, :validate end end |
Instance Method Details
#errors ⇒ Object
call-seq: errors
Returns the Errors object that holds all information about attribute error messages.
21 22 23 |
# File 'lib/validatable_instance_methods.rb', line 21 def errors @errors ||= Validatable::Errors.new end |
#increment_times_validated_for(validation) ⇒ Object
:nodoc:
40 41 42 43 44 45 46 47 48 |
# File 'lib/validatable_instance_methods.rb', line 40 def increment_times_validated_for(validation) #:nodoc: if validation.key != nil if times_validated_hash[validation.key].nil? times_validated_hash[validation.key] = 1 else times_validated_hash[validation.key] += 1 end end end |
#times_validated(key) ⇒ Object
:nodoc:
36 37 38 |
# File 'lib/validatable_instance_methods.rb', line 36 def times_validated(key) #:nodoc: times_validated_hash[key] || 0 end |
#valid? ⇒ Boolean
call-seq: valid?
Returns true if no errors were added otherwise false. Only executes validations that have no :groups option specified
14 15 16 |
# File 'lib/validatable_instance_methods.rb', line 14 def valid? valid_for_group?(nil) end |
#valid_for_group?(group) ⇒ Boolean
:nodoc:
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/validatable_instance_methods.rb', line 25 def valid_for_group?(group) #:nodoc: run_before_validations errors.clear run_callbacks(:before_validation) run_callbacks(:validate) self.class.validate_children(self, group) self.validate_group(group) run_callbacks(:after_validation) errors.empty? end |
#validate_only(key) ⇒ Object
call-seq: validate_only(key)
Only executes a specified validation. The argument should follow a pattern based on the key of the validation.
Examples:
* validates_presence_of :name can be run with obj.validate_only("presence_of/name")
* validates_presence_of :birthday, :key => "a key" can be run with obj.validate_only("presence_of/a key")
56 57 58 59 60 61 62 63 64 |
# File 'lib/validatable_instance_methods.rb', line 56 def validate_only(key) validation_name, attribute_name = key.split("/") validation_name = validation_name.split("_").collect{|word| word.capitalize}.join validation_key = "#{self.class.name}/Validatable::Validates#{validation_name}/#{attribute_name}" validation = self.class.all_validations.find { |validation| validation.key == validation_key } raise ArgumentError.new("validation with key #{validation_key} could not be found") if validation.nil? errors.clear run_validation(validation) end |