Class: Fluffery::Forms::Validation::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fluffery/forms/validation/base.rb

Direct Known Subclasses

Pattern, Presence

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form_object) ⇒ Base

Returns a new instance of Base.



11
12
13
# File 'lib/fluffery/forms/validation/base.rb', line 11

def initialize(form_object)
  @object = form_object
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



9
10
11
# File 'lib/fluffery/forms/validation/base.rb', line 9

def object
  @object
end

Instance Method Details

#add_html_attributes(attribute, options) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/fluffery/forms/validation/base.rb', line 15

def add_html_attributes(attribute, options)
  options = Presence.create(attribute, options) if attribute_required?(attribute)
  matcher = attribute_format_validator(attribute)
  options = Pattern.create(attribute, options, matcher) unless matcher.nil?
  options.reverse_merge!(default_messages_for(attribute))
  options
end

#attribute_format_validator(attribute) ⇒ Object

Checks to see if a particular attribute contains a Regex format validator



39
40
41
42
43
44
# File 'lib/fluffery/forms/validation/base.rb', line 39

def attribute_format_validator(attribute)          
  format_validator = validators_for(attribute).detect{ |v| v.kind == :format }
  return nil unless !format_validator.nil?
  return nil unless format_validator.options.has_key?(:with) && format_validator.options[:with].is_a?(Regexp)
  matcher = format_validator.options[:with]
end

#attribute_required?(attribute, options = nil) ⇒ Boolean

Checks to see if the particular attribute is required, used primarily on labels.

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fluffery/forms/validation/base.rb', line 25

def attribute_required?(attribute, options = nil)
  options.stringify_keys! if options.is_a?(Hash)
  unless options.nil?
    return true if options.has_key?('required') and options['required'] === true
  end
  valid_items = validators_for(attribute).find do |validator| 
    ([:presence, :inclusion].include?(validator.kind)) && 
    (validator.options.present? ? options_require_validation?(validator.options) : true)
  end        
  !valid_items.nil?        
end

#default_messages_for(attribute) ⇒ Object

Looks up the default error message so it may be used in our data-message attribute



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fluffery/forms/validation/base.rb', line 48

def default_messages_for(attribute)
  validators   = validators_for(attribute)
  return {} if validators.empty?
  message_data = validators.inject({}) do |hash, validator|
    message = validator.options.has_key?(:message) ? validator.options[:message] : MessageBuilder.message_for(@object, attribute, validator)
    hash.merge!(validator.kind.to_s => message)
  end
  attr_hash = {'data-validation-messages' => CGI::escape(message_data.to_json) }
  
  # Create a list of data-validates-* attrs on the field so we can catch them with javascript
  # Skip presence and format because they have their own valid HTML5 attrs.
  #
  validators.reject{ |v| v.kind.to_s.match(/(presence|format)/i) }.each{ |v| attr_hash.merge!("data-validates-#{v.kind.to_s}" => 'true') }
  
  attr_hash
  
end

#errors_for?(method) ⇒ Boolean

Checks to see if the particular attribute has errors

Returns:

  • (Boolean)


68
69
70
# File 'lib/fluffery/forms/validation/base.rb', line 68

def errors_for?(method)
  !(@object.nil? || @object.errors.empty? || !@object.errors.key?(method.to_sym) || [@object.errors[method.to_sym]].flatten.empty?)
end

#options_require_validation?(options) ⇒ Boolean

Checks to see if the validation is required Courtesy Justin French and Formtastic

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluffery/forms/validation/base.rb', line 75

def options_require_validation?(options)
  allow_blank = options[:allow_blank]
  return !allow_blank unless allow_blank.nil?
  if_condition = !options[:if].nil?
  condition = if_condition ? options[:if] : options[:unless]

  condition = if condition.respond_to?(:call)
    condition.call(@object)
  elsif condition.is_a?(::Symbol) && @object.respond_to?(condition)
    @object.send(condition)
  else
    condition
  end

  if_condition ? !!condition : !condition
end

#validators_for(attribute) ⇒ Object

Finds all existing validations for the current object and method



94
95
96
97
98
# File 'lib/fluffery/forms/validation/base.rb', line 94

def validators_for(attribute)
  return [] unless !@object.nil? and @object.class.respond_to?(:validators_on)
  attribute  = attribute.to_s.sub(/_id$/, '').to_sym
  validators = @object.class.validators_on(attribute).uniq
end

#validators_for?(method) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/fluffery/forms/validation/base.rb', line 100

def validators_for?(method)
  !validators_for(method).empty?
end