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

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 )
  options = {
    :message => 'contains an invalid element'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, collection|
    next if ( collection.nil? && options[ :allow_nil ]) || ( collection.blank? && options[ :allow_blank ])
    unless collection.all?{ |element| element.valid? }
      object.errors[ attribute ] << options[ :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 )
  options = {
    :message => 'is dependent on :attribute being defined'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, value|
    contingent_on_value = object.send( options[ :on ])
    next if ( value.nil? && options[ :allow_nil ]) || ( value.blank? && options[ :allow_blank ])
    unless value.blank? || !value.blank? && !contingent_on_value.blank? && ( options[ :with_value ].nil? || contingent_on_value == options[ :with_value ])
      object.errors[ attribute ] << options[ :message ].gsub( /:attribute/, options[ :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 )
  options = { 
    :message => 'is not in the required range' 
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, value|
    next if ( value.nil? && options[ :allow_nil ]) || ( value.blank? && options[ :allow_blank ])
    unless options[ :in ].include?( value )
      object.errors[ attribute ] << options[ :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 )
  options = {
    :message => 'contains members that are not valid'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, collection|
    next if ( collection.nil? && options[ :allow_nil ]) || (collection.blank? && options[ :allow_blank ])
    unless collection.all?{ |member| options[ :in ].include?( member )}
      object.errors[ attribute ] << options[ :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 )
  options = {
    :message => 'contains members with incorrect length'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, collection|
    next if ( collection.nil? && options[ :allow_nil ]) || (collection.blank? && options[ :allow_blank ])
    unless options[ :minimum ] && collection.length >= options[ :minimum ]
      object.errors[ attribute ] << options[ :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 )
  options = {
    :message => 'is not a number',
  }.merge!(attributes.extract_options!)

  re = options[:only_integer] ? CAP_INTEGER_REGEX : CAP_NUMBER_REGEX

  validates_each( *attributes ) do |object, attribute, value|
    next if (value.nil? && options[ :allow_nil ]) || (value.blank? && options[ :allow_blank ])
    unless ( value.to_s =~ re ) &&
      ( options[ :greater_than ].nil? || value && value > options[ :greater_than ])
      object.errors[ attribute ] << options[ :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 )
  options = {
    :message => 'does not respond to the given method'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, value|
    next if ( collection.nil? && options[ :allow_nil ]) || ( collection.blank? && options[ :allow_blank ])
    unless options[ :to ].all?{ |method_name| object.respond_to?( method_name )}
      object.errors[ attribute ] << options [ :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 )
  options = {
    :message => 'is not valid'
  }.merge!( attributes.extract_options! )

  validates_each( *attributes ) do |object, attribute, value|
    next if ( value.nil? && options[ :allow_nil ]) || ( value.blank? && options[ :allow_blank ])
    unless value && value.valid?
      object.errors[ attribute ] << options[ :message ]
    end
  end
end