Class: JQueryValidatingFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/casey_jones/validation/j_query_validating_form_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options, proc)
  self.form_validations = (object._validators || {})
  super(object_name, object, template, options, proc)
end

Instance Attribute Details

#form_validationsObject

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, html_options={})
  capture do
    ('div', contents, html_options)
  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, options={})
  options = set_validations_for(method, options)
  super(method, options)
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, options = {})
  #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? || options[: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 += " #{options[:additional_text]}" if options[:additional_text]
  #Finally hand off to super to deal with the display of the label
  super(method, text, options)
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, options={})
  options = set_validations_for(method, options)
  super(method, options)
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, options={})
  options.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|
    options = validation_methods[class_name].call(validator_class, options)
  end
  return options
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, options={})
  options = set_validations_for(method, options)
  super(method, options)
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, options = {})
  options = set_validations_for(method, options)
  puts options.inspect
  super(method, options)
end