Module: Formtastic::Inputs::Base::Validations

Included in:
Formtastic::Inputs::Base
Defined in:
lib/formtastic/inputs/base/validations.rb

Instance Method Summary collapse

Instance Method Details

#column_limitObject



112
113
114
# File 'lib/formtastic/inputs/base/validations.rb', line 112

def column_limit
  column.limit if column? && column.respond_to?(:limit)
end

#limitObject



116
117
118
# File 'lib/formtastic/inputs/base/validations.rb', line 116

def limit
  validation_limit || column_limit
end

#not_required_through_negated_validation!Object



104
105
106
# File 'lib/formtastic/inputs/base/validations.rb', line 104

def not_required_through_negated_validation!
  @not_required_through_negated_validation = true
end

#not_required_through_negated_validation?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/formtastic/inputs/base/validations.rb', line 100

def not_required_through_negated_validation?
  @not_required_through_negated_validation
end

#optional?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/formtastic/inputs/base/validations.rb', line 108

def optional?
  !required?
end

#required?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/formtastic/inputs/base/validations.rb', line 86

def required?
  return false if not_required_through_negated_validation?
  if validations?
    validations.select { |validator| 
      [:presence, :inclusion, :length].include?(validator.kind) &&
      validator.options[:allow_blank] != true
    }.any?
  else
    return false if options[:required] == false
    return true if options[:required] == true
    return !!builder.all_fields_required_by_default
  end
end

#validation_integer_only?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
# File 'lib/formtastic/inputs/base/validations.rb', line 71

def validation_integer_only?
  validation = validations? && validations.find do |validation|
    validation.kind == :numericality
  end
  if validation
    validation.options[:only_integer]
  else
    false
  end
end

#validation_limitObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/formtastic/inputs/base/validations.rb', line 34

def validation_limit
  validation = validations? && validations.find do |validation|
    validation.kind == :length
  end
  if validation
    validation.options[:maximum] || (validation.options[:within].present? ? validation.options[:within].max : nil)
  else
    nil
  end
end

#validation_maxObject

Prefer :less_than_or_equal_to over :less_than, for no particular reason.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/formtastic/inputs/base/validations.rb', line 59

def validation_max
  validation = validations? && validations.find do |validation|
    validation.kind == :numericality
  end
  if validation
    return validation.options[:less_than_or_equal_to] if validation.options[:less_than_or_equal_to]
    return (validation.options[:less_than] - 1) if validation.options[:less_than]
  else
    nil
  end
end

#validation_minObject

Prefer :greater_than_or_equal_to over :greater_than, for no particular reason.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/formtastic/inputs/base/validations.rb', line 46

def validation_min
  validation = validations? && validations.find do |validation|
    validation.kind == :numericality
  end
  if validation
    return validation.options[:greater_than_or_equal_to] if validation.options[:greater_than_or_equal_to]
    return (validation.options[:greater_than] + 1) if validation.options[:greater_than]
  else
    nil
  end
end

#validationsObject



6
7
8
9
10
11
12
13
14
# File 'lib/formtastic/inputs/base/validations.rb', line 6

def validations
  @validations ||= if object && object.class.respond_to?(:validators_on) 
    object.class.validators_on(attributized_method_name).select do |validator|
      validator_relevant?(validator)
    end
  else
    []
  end
end

#validations?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/formtastic/inputs/base/validations.rb', line 82

def validations?
  !validations.empty?
end

#validator_relevant?(validator) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/formtastic/inputs/base/validations.rb', line 16

def validator_relevant?(validator)
  return true unless validator.options.key?(:if) || validator.options.key?(:unless)
  conditional = validator.options.key?(:if) ? validator.options[:if] : validator.options[:unless]
  
  result = if conditional.respond_to?(:call)
    conditional.call(object)
  elsif conditional.is_a?(::Symbol) && object.respond_to?(conditional)
    object.send(conditional)
  else
    conditional
  end
  
  result = validator.options.key?(:unless) ? !result : !!result
  not_required_through_negated_validation! if !result && [:presence, :inclusion, :length].include?(validator.kind)

  result
end