Module: FormtasticValidation

Defined in:
lib/formtastic_validation.rb

Constant Summary collapse

NAMESPACE =
'validation'
@@valid_message =
"Thank you!"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
# File 'lib/formtastic_validation.rb', line 7

def self.included(base)
  base.class_eval {
    alias_method_chain :input, :validation
  }
end

Instance Method Details

#add_namespace(opts_hash) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/formtastic_validation.rb', line 69

def add_namespace(opts_hash)
  namespaced_hash = Hash.new
  opts_hash.each do |key, value|
    namespaced_key = (NAMESPACE + '_' + key.to_s).to_sym
    namespaced_hash[namespaced_key] = value
  end
  namespaced_hash
end

#attribute_sym(attribute) ⇒ Object



28
29
30
# File 'lib/formtastic_validation.rb', line 28

def attribute_sym(attribute)
  attribute.to_s.sub(/_id$/, '').to_sym
end

#has_validations?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/formtastic_validation.rb', line 20

def has_validations?(attribute)
  if @object && @object.class.respond_to?(:reflect_on_validations_for)
    @object.class.reflect_on_validations_for(attribute_sym(attribute)).present?
  else
    false
  end
end

#input_with_validation(method, options = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/formtastic_validation.rb', line 13

def input_with_validation(method, options = {})
  if has_validations?(method)
    options[:input_html] = validation_tags(method, options[:input_html])
  end
  input_without_validation(method, options)
end

#options_tags(tags) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/formtastic_validation.rb', line 60

def options_tags(tags)
  otags_array = tags.map do |tag|
    tag.delete_if { |key, value| key.eql?(:validation) }
  end
  opts_hash = otags_array.inject { |memo, hash| memo.merge(hash) }
  namespaced_hash = add_namespace(opts_hash)
  serialise_options(namespaced_hash)
end

#serialise_options(opts_hash) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/formtastic_validation.rb', line 78

def serialise_options(opts_hash)
  serialised_hash = Hash.new
  opts_hash.each do |key, value|
    serialised_value = case value.class.to_s
                       when "String" then value
                       when "Regexp" then value.inspect
                       else value.to_json
                       end
    serialised_hash[key] = serialised_value
  end
  serialised_hash
end

#stack_tags(tags, input_html) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/formtastic_validation.rb', line 44

def stack_tags(tags, input_html)
  vtags = validates_tags(tags.dup)
  otags = options_tags(tags.dup)
  if input_html
    input_html[:validation] = vtags
    input_html.merge(otags)
  else
    { :validation => vtags }.merge(otags)
  end
end

#validates_tags(tags) ⇒ Object



55
56
57
58
# File 'lib/formtastic_validation.rb', line 55

def validates_tags(tags)
  validates_tags = tags.map { |tag| tag[:validation] }
  validates_tags.join(' ')
end

#validation_tags(attribute, input_html) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/formtastic_validation.rb', line 32

def validation_tags(attribute, input_html)
  tags = []
  @object.class.reflect_on_validations_for(attribute).each do |validation|
    tags << { :validation => validation.macro }.merge(validation.options.merge(validation_valid_message_tag))
  end
  stack_tags(tags, input_html)
end

#validation_valid_message_tagObject



40
41
42
# File 'lib/formtastic_validation.rb', line 40

def validation_valid_message_tag
  { :valid_message => @@valid_message }
end