Method: RPH::FormAssistant::FormBuilder.assist

Defined in:
lib/form_assistant/form_builder.rb

.assist(helper_name) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/form_assistant/form_builder.rb', line 204

def self.assist(helper_name)
  define_method(helper_name) do |field, *args|
    options          = (helper_name == 'check_box' ? args.shift : args.extract_options!) || {}
    label_options    = extract_options_for_label(field, options)
    template_options = extract_options_for_template(helper_name, options)
    extra_locals     = options.delete(:locals) || {}
    
    # build out the label element (if desired)
    label = label_options[:label] === false ? nil : self.label(field, label_options.delete(:text), label_options)

    # grab the tip, if any
    tip = options.delete(:tip)
    
    # is the field required?
    required = !!options.delete(:required)
    
    # ensure that we don't have any custom options pass through
    field_options = options.except(:label, :template, :tip, :required)
    
    # call the original render for the element
    super_args = helper_name == 'check_box' ? args.unshift(field_options) : args.push(field_options)
    element = super(field, *super_args)
    
    return element if template_options[:template] === false
    
    # return the helper with an optional label if templates are not to be used
    return render_element(element, field, helper_name, options, label_options[:label] === false) if self.class.ignore_templates
    
    # render the partial template from the desired template root
    render_partial_for(element, field, label, tip, template_options[:template], helper_name, required, extra_locals, args)
  end
end