Class: JQueryValidatingFormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- JQueryValidatingFormBuilder
- Defined in:
- lib/casey_jones/validation/j_query_validating_form_builder.rb
Instance Attribute Summary collapse
-
#form_validations ⇒ Object
Returns the value of attribute form_validations.
Class Method Summary collapse
Instance Method Summary collapse
- #div_tag(contents, html_options = {}) ⇒ Object
-
#form_fields(*args) ⇒ Object
Renders form fields for the named arguments, complete with divs for styling and labels for each field.
- #hidden_field(method, options = {}) ⇒ Object
-
#initialize(object_name, object, template, options, proc) ⇒ JQueryValidatingFormBuilder
constructor
A new instance of JQueryValidatingFormBuilder.
-
#label(method, text = nil, options = {}) ⇒ Object
Adds error message directly inline to a form label Accepts all the options normall passed to form.label as well as: :hide_errors - true if you don’t want errors displayed on this label :additional_text - Will add additional text after the error message or after the label if no errors.
- #password_field(method, options = {}) ⇒ Object
-
#set_validations_for(method, options = {}) ⇒ Object
make a hash of the class name and validator class for each validator that governs this form field, but only the ones that are represented in our local validation_methods hash.
- #text_area(method, options = {}) ⇒ Object
- #text_field(method, options = {}) ⇒ Object
Constructor Details
#initialize(object_name, object, template, options, proc) ⇒ JQueryValidatingFormBuilder
Returns a new instance of JQueryValidatingFormBuilder.
2 3 4 5 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 2 def initialize(object_name, object, template, , proc) self.form_validations = (object._validators || {}) super(object_name, object, template, , proc) end |
Instance Attribute Details
#form_validations ⇒ Object
Returns the value of attribute form_validations.
89 90 91 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 89 def form_validations @form_validations end |
Class Method Details
.add_validator(name, proc) ⇒ Object
171 172 173 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 171 def self.add_validator(name, proc) self.validation_methods[name] = proc end |
Instance Method Details
#div_tag(contents, html_options = {}) ⇒ Object
92 93 94 95 96 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 92 def div_tag(contents, ={}) capture do content_tag('div', contents, ) end end |
#form_fields(*args) ⇒ Object
Renders form fields for the named arguments, complete with divs for styling
and labels for each field.
= f.form_fields :name, :address
Will output:
<div class="field">
<div class="fieldName">
<label>Field Name</label>
</div>
<div class="fieldValue">
<input type=... />
</div>
</div>
= f.form_fields({"Edit your profile" => [:name, :address]})
Will output:
<div class="fieldGroup">
<div class="fieldGroupTitle">Edit your profile</div>
<div class="field">
<div class="fieldName">
<label>Field Name</label>
</div>
<div class="fieldValue">
<input type=... />
</div>
</div>
</div>
= f.form_fields({"Edit your profile" => [{:name=>{:label=>"What is your name", :type=>:text_area}}, :address]})
Will output:
<div class="fieldGroup">
<div class="fieldGroupTitle">Edit your profile</div>
<div class="field">
<div class="fieldName">
<label>What is your name</label>
</div>
<div class="fieldValue">
<textarea type=... />
</div>
</div>
</div>
= f.form_fields({"Edit your profile" => :profile_attributes})
Will output:
<div class="fieldGroup">
<div class="fieldGroupTitle">Edit your profile</div>
<div class="field">
<div class="fieldName">
<label>What is your name</label>
</div>
<div class="fieldValue">
<textarea type=... />
</div>
</div>
</div>
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 72 def form_fields(*args) concat capture do args.each do |arg| if arg.is_a? Hash arg.each do |key, value| end elsif arg.is_a? Array else end end end end |
#hidden_field(method, options = {}) ⇒ Object
145 146 147 148 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 145 def hidden_field(method, ={}) = set_validations_for(method, ) super(method, ) end |
#label(method, text = nil, options = {}) ⇒ Object
Adds error message directly inline to a form label Accepts all the options normall passed to form.label as well as:
:hide_errors - true if you don't want errors displayed on this label
:additional_text - Will add additional text after the error message or after the label if no errors
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 103 def label(method, text = nil, = {}) #Check to see if text for this label has been supplied and humanize the field name if not. text = text || method.to_s.humanize #Get a reference to the model object object_name = @object_name.to_s if object_name.include? '[' ivar_name = '@' + @object_name[0..object_name.index('[')-1] object = @template.instance_variable_get(ivar_name) object_name.scan(/\[([\w_-]+)\]/) do |m| object = object.send(m[0].gsub(/_attributes/, '')) end else object = @template.instance_variable_get("@#{@object_name}") end #Make sure we have an object and we're not told to hide errors for this label unless object.nil? || [:hide_errors] #Check if there are any errors for this field in the model errors = object.errors.on(method.to_sym) if errors #Generate the label using the text as well as the error message wrapped in a span with error class text += " <span class=\"error\">#{errors.is_a?(Array) ? errors.first : errors}</span>" end end #Add any additional text that might be needed on the label text += " #{[:additional_text]}" if [:additional_text] #Finally hand off to super to deal with the display of the label super(method, text, ) end |
#password_field(method, options = {}) ⇒ Object
140 141 142 143 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 140 def password_field(method, ={}) = set_validations_for(method, ) super(method, ) end |
#set_validations_for(method, options = {}) ⇒ Object
make a hash of the class name and validator class for each validator that governs this form field, but only the ones that are represented in our local validation_methods hash.
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 158 def set_validations_for(method, ={}) .extend(OptionsAddClass) my_validations = self.form_validations[method]. map_hash{|t|{t.class.name.underscore.split('/').last=>t}}. symbolize_keys. slice(*self.validation_methods.keys) my_validations.each do |class_name, validator_class| = validation_methods[class_name].call(validator_class, ) end return end |
#text_area(method, options = {}) ⇒ Object
150 151 152 153 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 150 def text_area(method, ={}) = set_validations_for(method, ) super(method, ) end |
#text_field(method, options = {}) ⇒ Object
134 135 136 137 138 |
# File 'lib/casey_jones/validation/j_query_validating_form_builder.rb', line 134 def text_field(method, = {}) = set_validations_for(method, ) puts .inspect super(method, ) end |