Module: Validation::ClassMethods
- Defined in:
- lib/generators/rcap/models/templates/modules/validations.rb
Overview
:nodoc:
Constant Summary collapse
- CAP_NUMBER_REGEX =
Regexp.new( '^-{0,1}\d*\.{0,1}\d+$' )
- CAP_INTEGER_REGEX =
Regexp.new( '\-{0,1}A[+-]?\d+\Z' )
Instance Method Summary collapse
- #validates_collection_of(*attributes) ⇒ Object
- #validates_dependency_of(*attributes) ⇒ Object
- #validates_inclusion_of(*attributes) ⇒ Object
- #validates_inclusion_of_members_of(*attributes) ⇒ Object
- #validates_length_of_members_of(*attributes) ⇒ Object
- #validates_numericality_of(*attributes) ⇒ Object
- #validates_responsiveness_of(*attributes) ⇒ Object
- #validates_validity_of(*attributes) ⇒ Object
Instance Method Details
#validates_collection_of(*attributes) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/generators/rcap/models/templates/modules/validations.rb', line 59 def validates_collection_of( *attributes ) = { :message => 'contains an invalid element' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, collection| next if ( collection.nil? && [ :allow_nil ]) || ( collection.blank? && [ :allow_blank ]) unless collection.all?{ |element| element.valid? } object.errors[ attribute ] << [ :message ] end end end |
#validates_dependency_of(*attributes) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/generators/rcap/models/templates/modules/validations.rb', line 72 def validates_dependency_of( *attributes ) = { :message => 'is dependent on :attribute being defined' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, value| contingent_on_value = object.send( [ :on ]) next if ( value.nil? && [ :allow_nil ]) || ( value.blank? && [ :allow_blank ]) unless value.blank? || !value.blank? && !contingent_on_value.blank? && ( [ :with_value ].nil? || contingent_on_value == [ :with_value ]) object.errors[ attribute ] << [ :message ].gsub( /:attribute/, [ :on ].to_s ) end end end |
#validates_inclusion_of(*attributes) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/generators/rcap/models/templates/modules/validations.rb', line 7 def validates_inclusion_of( *attributes ) = { :message => 'is not in the required range' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, value| next if ( value.nil? && [ :allow_nil ]) || ( value.blank? && [ :allow_blank ]) unless [ :in ].include?( value ) object.errors[ attribute ] << [ :message ] end end end |
#validates_inclusion_of_members_of(*attributes) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/generators/rcap/models/templates/modules/validations.rb', line 20 def validates_inclusion_of_members_of( *attributes ) = { :message => 'contains members that are not valid' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, collection| next if ( collection.nil? && [ :allow_nil ]) || (collection.blank? && [ :allow_blank ]) unless collection.all?{ |member| [ :in ].include?( member )} object.errors[ attribute ] << [ :message ] end end end |
#validates_length_of_members_of(*attributes) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/generators/rcap/models/templates/modules/validations.rb', line 33 def validates_length_of_members_of( *attributes ) = { :message => 'contains members with incorrect length' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, collection| next if ( collection.nil? && [ :allow_nil ]) || (collection.blank? && [ :allow_blank ]) unless [ :minimum ] && collection.length >= [ :minimum ] object.errors[ attribute ] << [ :message ] end end end |
#validates_numericality_of(*attributes) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/generators/rcap/models/templates/modules/validations.rb', line 86 def validates_numericality_of( *attributes ) = { :message => 'is not a number', }.merge!(attributes.) re = [:only_integer] ? CAP_INTEGER_REGEX : CAP_NUMBER_REGEX validates_each( *attributes ) do |object, attribute, value| next if (value.nil? && [ :allow_nil ]) || (value.blank? && [ :allow_blank ]) unless ( value.to_s =~ re ) && ( [ :greater_than ].nil? || value && value > [ :greater_than ]) object.errors[ attribute ] << [ :message ] end end end |
#validates_responsiveness_of(*attributes) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/generators/rcap/models/templates/modules/validations.rb', line 103 def validates_responsiveness_of( *attributes ) = { :message => 'does not respond to the given method' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, value| next if ( collection.nil? && [ :allow_nil ]) || ( collection.blank? && [ :allow_blank ]) unless [ :to ].all?{ |method_name| object.respond_to?( method_name )} object.errors[ attribute ] << [ :message ] end end end |
#validates_validity_of(*attributes) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/generators/rcap/models/templates/modules/validations.rb', line 46 def validates_validity_of( *attributes ) = { :message => 'is not valid' }.merge!( attributes. ) validates_each( *attributes ) do |object, attribute, value| next if ( value.nil? && [ :allow_nil ]) || ( value.blank? && [ :allow_blank ]) unless value && value.valid? object.errors[ attribute ] << [ :message ] end end end |