Module: Formtastic::Inputs::Base::Validations
- Included in:
- Formtastic::Inputs::Base
- Defined in:
- lib/formtastic/inputs/base/validations.rb
Instance Method Summary collapse
- #column_limit ⇒ Object
- #limit ⇒ Object
- #not_required_through_negated_validation! ⇒ Object
- #not_required_through_negated_validation? ⇒ Boolean
- #optional? ⇒ Boolean
- #required? ⇒ Boolean
- #validation_integer_only? ⇒ Boolean
- #validation_limit ⇒ Object
-
#validation_max ⇒ Object
Prefer :less_than_or_equal_to over :less_than, for no particular reason.
-
#validation_min ⇒ Object
Prefer :greater_than_or_equal_to over :greater_than, for no particular reason.
- #validations ⇒ Object
- #validations? ⇒ Boolean
- #validator_relevant?(validator) ⇒ Boolean
Instance Method Details
#column_limit ⇒ Object
112 113 114 |
# File 'lib/formtastic/inputs/base/validations.rb', line 112 def column_limit column.limit if column? && column.respond_to?(:limit) end |
#limit ⇒ Object
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
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
108 109 110 |
# File 'lib/formtastic/inputs/base/validations.rb', line 108 def optional? !required? end |
#required? ⇒ 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.[:allow_blank] != true }.any? else return false if [:required] == false return true if [:required] == true return !!builder.all_fields_required_by_default end end |
#validation_integer_only? ⇒ 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.[:only_integer] else false end end |
#validation_limit ⇒ Object
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.[:maximum] || (validation.[:within].present? ? validation.[:within].max : nil) else nil end end |
#validation_max ⇒ Object
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.[:less_than_or_equal_to] if validation.[:less_than_or_equal_to] return (validation.[:less_than] - 1) if validation.[:less_than] else nil end end |
#validation_min ⇒ Object
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.[:greater_than_or_equal_to] if validation.[:greater_than_or_equal_to] return (validation.[:greater_than] + 1) if validation.[:greater_than] else nil end end |
#validations ⇒ Object
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
82 83 84 |
# File 'lib/formtastic/inputs/base/validations.rb', line 82 def validations? !validations.empty? end |
#validator_relevant?(validator) ⇒ 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..key?(:if) || validator..key?(:unless) conditional = validator..key?(:if) ? validator.[:if] : validator.[: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..key?(:unless) ? !result : !!result not_required_through_negated_validation! if !result && [:presence, :inclusion, :length].include?(validator.kind) result end |