Module: GeoTools::Validations

Defined in:
lib/geo_tools/validations.rb

Constant Summary collapse

ALL_NUMERICALITY_CHECKS =

ActiveRecord::Validations::ClassMethods::ALL_NUMERICALITY_CHECKS

{ :greater_than => '>', :greater_than_or_equal_to => '>=',
:equal_to => '==', :less_than => '<', :less_than_or_equal_to => '<=',
:odd => 'odd?', :even => 'even?' }.freeze

Instance Method Summary collapse

Instance Method Details

#validates_inclusion_of_for(*attr_names) ⇒ Object

Sames as validates_numericality_of but additionally supports :for option which lets you attach an error to a different attribute.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/geo_tools/validations.rb', line 12

def validates_inclusion_of_for(*attr_names)
  configuration = { :on => :save }
  configuration.update(attr_names.extract_options!)

  enum = configuration[:in] || configuration[:within]

  raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?(:include?)

  validates_each(attr_names, configuration) do |record, attr_name, value|
    unless enum.include?(value)
      attr_for = configuration[:for] || attr_name
      record.errors.add(attr_for, :inclusion, :default => configuration[:message], :value => value) 
    end
  end
end

#validates_numericality_of_for(*attr_names) ⇒ Object

Sames as validates_numericality_of but additionally supports :for option which lets you attach an error to a different attribute.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/geo_tools/validations.rb', line 30

def validates_numericality_of_for(*attr_names)
  configuration = { :on => :save, :only_integer => false, :allow_nil => false }
  configuration.update(attr_names.extract_options!)

  numericality_options = ALL_NUMERICALITY_CHECKS.keys & configuration.keys

  (numericality_options - [ :odd, :even ]).each do |option|
    raise ArgumentError, ":#{option} must be a number" unless configuration[option].is_a?(Numeric)
  end

  validates_each(attr_names,configuration) do |record, attr_name, value|
    raw_value = record.send("#{attr_name}_before_type_cast") || value

    next if configuration[:allow_nil] and raw_value.nil?

    attr_for = configuration[:for] || attr_name

    if configuration[:only_integer]
      unless raw_value.to_s =~ /\A[+-]?\d+\Z/
        record.errors.add(attr_for, :not_a_number, :value => raw_value, :default => configuration[:message])
        next
      end
      raw_value = raw_value.to_i
    else
      begin
        raw_value = Kernel.Float(raw_value)
      rescue ArgumentError, TypeError
        record.errors.add(attr_for, :not_a_number, :value => raw_value, :default => configuration[:message])
        next
      end
    end

    numericality_options.each do |option|
      case option
      when :odd, :even
        unless raw_value.to_i.method( ALL_NUMERICALITY_CHECKS[option])[]
          record.errors.add(attr_for, option, :value => raw_value, :default => configuration[:message]) 
        end
      else
        record.errors.add(attr_for, option, :default => configuration[:message], :value => raw_value, :count => configuration[option]) unless raw_value.method( ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
      end
    end
  end
end