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

#attribute_required?, #content_tag, #errors_on_attribute?, #fields_for, #options_require_validation?, #render_field_as_custom, #render_field_with_errors, #required_by_option?, #validates_inclusion?, #validates_presence?, #validates_uniqueness?, #validators_for, #validators_for?

Instance Method Details

- (Object) button(value = nil, options = {})

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

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

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

- (Array) options_for_state_select(abbreviate = false, incl_international = false) (protected)

Returns a list of US states as an array

Parameters:

  • abbreviate (Boolean) (defaults to: false)

    Abbreviate the value

  • incl_international (Boolean, String) (defaults to: false)

    Include an additional state for "International"

Returns:

  • (Array)

    An array of states



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/motr/forms/builder.rb', line 93

def options_for_state_select(abbreviate = false, incl_international = false)
  incl_international ||= false
  state_list = [
      ['Alabama', "AL"],['Alaska', "AK"],['Arizona', "AZ"],['Arkansas', "AR"],['California', "CA"],['Colorado', "CO"],
    	['Connecticut', "CT"],['District of Columbia', "DC"],['Delaware', "DE"],['Florida', "FL"],['Georgia', "GA"],
    	['Hawaii', "HI"],['Idaho', "ID"],['Illinois', "IL"],['Indiana', "IN"],['Iowa', "IA"],['Kansas', "KS"],['Kentucky', "KY"],
    	['Louisiana', "LA"],['Maine', "ME"],['Maryland', "MD"],['Massachusetts', "MA"],['Michigan', "MI"],['Minnesota', "MN"],
    	['Mississippi', "MS"],['Missouri', "MO"],['Montana', "MT"],['Nebraska', "NE"],['Nevada', "NV"],['New Hampshire', "NH"],
    	['New Jersey', "NJ"],['New Mexico', "NM"],['New York', "NY"],['North Carolina', "NC"],['North Dakota', "ND"],
    	['Ohio', "OH"],['Oklahoma', "OK"],['Oregon', "OR"],['Pennsylvania', "PA"],['Rhode Island', "RI"],['South Carolina', "SC"],
    	['South Dakota', "SD"],['Tennessee', "TN"],['Texas', "TX"],['Utah', "UT"],['Vermont', "VT"],['Virginia', "VA"],['Washington', "WA"],
    	['West Virginia', "WV"],['Wisconsin', "WI"],['Wyoming', "WY"]
    	
  ].map do |state|
    (abbreviate ? state : [state.first, state.first])
  end
  state_list << ['International', incl_international] unless incl_international === false
  state_list
end

- (String) state_select(method, options = {}, html_options = {})

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