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

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

Defined Under Namespace

Classes: IndeterminableMaximumAttributeError, IndeterminableMinimumAttributeError

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) autofocus?



181
182
183
184
185
# File 'lib/formtastic/inputs/base/validations.rb', line 181

def autofocus?
  opt_autofocus = options[:input_html] && options[:input_html][:autofocus]

  !!opt_autofocus
end

- (Object) column_limit



187
188
189
# File 'lib/formtastic/inputs/base/validations.rb', line 187

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

- (Object) limit



191
192
193
# File 'lib/formtastic/inputs/base/validations.rb', line 191

def limit
  validation_limit || column_limit
end

- (Object) not_required_through_negated_validation!



169
170
171
# File 'lib/formtastic/inputs/base/validations.rb', line 169

def not_required_through_negated_validation!
  @not_required_through_negated_validation = true
end

- (Boolean) not_required_through_negated_validation?



165
166
167
# File 'lib/formtastic/inputs/base/validations.rb', line 165

def not_required_through_negated_validation?
  @not_required_through_negated_validation
end

- (Boolean) optional?



177
178
179
# File 'lib/formtastic/inputs/base/validations.rb', line 177

def optional?
  !required?
end

- (Boolean) required?



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/formtastic/inputs/base/validations.rb', line 134

def required?
  return false if options[:required] == false
  return true if options[:required] == true
  return false if not_required_through_negated_validation?
  if validations?
    validations.select { |validator|
      if validator.options.key?(:on)
        return false if (validator.options[:on] != :save) && ((object.new_record? && validator.options[:on] != :create) || (!object.new_record? && validator.options[:on] != :update))
      end
      case validator.kind
      when :presence
        true
      when :inclusion
        validator.options[:allow_blank] != true
      when :length
        validator.options[:allow_blank] != true &&
        validator.options[:minimum].to_i > 0 ||
        validator.options[:within].try(:first).to_i > 0
      else
        false
      end
    }.any?
  else
    return responds_to_global_required? && !!builder.all_fields_required_by_default
  end
end

- (Boolean) required_attribute?



161
162
163
# File 'lib/formtastic/inputs/base/validations.rb', line 161

def required_attribute?
  required? && builder.use_required_attribute
end

- (Boolean) responds_to_global_required?



173
174
175
# File 'lib/formtastic/inputs/base/validations.rb', line 173

def responds_to_global_required?
  true
end

- (Boolean) validation_integer_only?



119
120
121
122
123
124
125
126
127
128
# File 'lib/formtastic/inputs/base/validations.rb', line 119

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

- (Object) validation_limit



54
55
56
57
58
59
60
61
62
63
# File 'lib/formtastic/inputs/base/validations.rb', line 54

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

- (Object) validation_max

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/formtastic/inputs/base/validations.rb', line 88

def validation_max
  validation = validations? && validations.find do |validation|
    validation.kind == :numericality
  end
  if validation
    # We can't determine an appropriate value for :greater_than with a float/decimal column
    raise IndeterminableMaximumAttributeError if validation.options[:less_than] && column? && [:float, :decimal].include?(column.type)

    if validation.options[:less_than_or_equal_to]
      return (validation.options[:less_than_or_equal_to].call(object)) if validation.options[:less_than_or_equal_to].kind_of?(Proc)
      return (validation.options[:less_than_or_equal_to])
    end

    if validation.options[:less_than]
      return ((validation.options[:less_than].call(object)) - 1) if validation.options[:less_than].kind_of?(Proc)
      return (validation.options[:less_than] - 1)
    end
  end
end

- (Object) validation_min

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/formtastic/inputs/base/validations.rb', line 66

def validation_min
  validation = validations? && validations.find do |validation|
    validation.kind == :numericality
  end

  if validation
    # We can't determine an appropriate value for :greater_than with a float/decimal column
    raise IndeterminableMinimumAttributeError if validation.options[:greater_than] && column? && [:float, :decimal].include?(column.type)

    if validation.options[:greater_than_or_equal_to]
      return (validation.options[:greater_than_or_equal_to].call(object)) if validation.options[:greater_than_or_equal_to].kind_of?(Proc)
      return (validation.options[:greater_than_or_equal_to])
    end

    if validation.options[:greater_than]
      return (validation.options[:greater_than].call(object) + 1) if validation.options[:greater_than].kind_of?(Proc)
      return (validation.options[:greater_than] + 1)
    end
  end
end

- (Object) validation_step



108
109
110
111
112
113
114
115
116
117
# File 'lib/formtastic/inputs/base/validations.rb', line 108

def validation_step
  validation = validations? && validations.find do |validation|
    validation.kind == :numericality
  end
  if validation
    validation.options[:step] || (1 if validation_integer_only?)
  else
    nil
  end
end

- (Object) validations



24
25
26
27
28
29
30
31
32
# File 'lib/formtastic/inputs/base/validations.rb', line 24

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
    nil
  end
end

- (Boolean) validations?



130
131
132
# File 'lib/formtastic/inputs/base/validations.rb', line 130

def validations?
  validations != nil
end

- (Boolean) validator_relevant?(validator)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/formtastic/inputs/base/validations.rb', line 34

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.arity > 0
    conditional.call(object)
  elsif conditional.respond_to?(:call) && conditional.arity == 0
    object.instance_exec(&conditional)
  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