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
- #add_namespace(opts_hash) ⇒ Object
- #attribute_sym(attribute) ⇒ Object
- #has_validations?(attribute) ⇒ Boolean
- #input_with_validation(method, options = {}) ⇒ Object
- #options_tags(tags) ⇒ Object
- #serialise_options(opts_hash) ⇒ Object
- #stack_tags(tags, input_html) ⇒ Object
- #validates_tags(tags) ⇒ Object
- #validation_tags(attribute, input_html) ⇒ Object
- #validation_valid_message_tag ⇒ Object
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
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, = {}) if has_validations?(method) [:input_html] = (method, [:input_html]) end input_without_validation(method, ) end |
#options_tags(tags) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/formtastic_validation.rb', line 60 def () = .map do |tag| tag.delete_if { |key, value| key.eql?(:validation) } end opts_hash = .inject { |memo, hash| memo.merge(hash) } namespaced_hash = add_namespace(opts_hash) (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 (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 (, input_html) = (.dup) = (.dup) if input_html input_html[:validation] = input_html.merge() else { :validation => }.merge() end end |
#validates_tags(tags) ⇒ Object
55 56 57 58 |
# File 'lib/formtastic_validation.rb', line 55 def () = .map { |tag| tag[:validation] } .join(' ') end |
#validation_tags(attribute, input_html) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/formtastic_validation.rb', line 32 def (attribute, input_html) = [] @object.class.reflect_on_validations_for(attribute).each do |validation| << { :validation => validation.macro }.merge(validation..merge()) end (, input_html) end |
#validation_valid_message_tag ⇒ Object
40 41 42 |
# File 'lib/formtastic_validation.rb', line 40 def { :valid_message => @@valid_message } end |