Class: Motr::Forms::Builder

Inherits:
Base
  • Object
show all
Defined in:
lib/motr/forms/builder.rb

Overview

Default Motr form builder. Extends the passed input tags to include additional html attributes, primarily for javascript integration.

Instance Attribute Summary

Attributes inherited from Base

#current_field_type, #field_order, #template

Instance Method Summary collapse

Methods inherited from Base

#fields_for

Instance Method Details

#button(value = nil, options = {}) ⇒ Object

Creates a button tag to be used in a form instead of the default input to help make CSS styling easier

Parameters:

  • value (String) (defaults to: nil)

    The text for the button

  • options (Hash) (defaults to: {})

    HTML options to be passed to the button

  • [String] (Hash)

    a customizable set of options



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/motr/forms/builder.rb', line 51

def button(value = nil, options = {})
  
  value, options = nil, value if value.is_a?(Hash)
  value ||= submit_default_value

  value    = [image_tag(icon, :class => 'icon'), value].join(' ') if icon = options.delete(:icon)
  klasses = (options.delete(:class) || "").split(" ")
  klasses << "button"
  options['class'] = klasses.join(" ")
  (:button, value.to_s.html_safe, options.reverse_merge!({ "type" => "submit", "name" => "commit" }))
  
end

#label(method, text = nil, options = {}, &block) ⇒ Object

Modified label tag to support adding a ‘required’ asterisk to the end of the label. Same params as the original implementation



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/motr/forms/builder.rb', line 27

def label(method, text = nil, options = {}, &block) #:nodoc:
  
  options, text = text, nil if text.is_a?(Hash)
  text ||= method.to_s.humanize
  
  options.stringify_keys!
  klasses = (options.delete(['class']) || "").split(" ")
  klasses << Motr::Forms.error_class if errors_on_attribute?(method)
  options['class'] = klasses.join(" ") unless klasses.compact.empty?
  
  text = "#{text} <abbr title='Required'>*</abbr>".html_safe  if attribute_required?(method) || required_by_option?(options.delete('required'))        
  super(method, text, options, &block)
  
end

#state_select(method, options = {}, html_options = {}) ⇒ String

Generate a select tag with the 50 US states as options

Parameters:

  • method (Symbol)

    The object method/attribute to be used

  • options (Hash) (defaults to: {})

    Same as Rails’ select options hash

  • html_options (Hash) (defaults to: {})

    Same as Rails’ html_options hash

Options Hash (options):

  • :international (Symbol)

    Include an international option

  • :abbreviate (Symbol)

    Use an abbreviated version of the state name for the value

Returns:

  • (String)

    HTML select tag



76
77
78
79
80
# File 'lib/motr/forms/builder.rb', line 76

def state_select(method, options = {}, html_options = {})
  abbr = options.delete(:abbreviate)
  abbr = !(abbr.nil? || abbr === false)
  select(method, @template.options_for_select(options_for_state_select(abbr, options.delete(:international)), @object.try(:state)), options, html_options)        
end