Class: Transit::Builders::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/transit/builders/form_builder.rb

Overview

Base class for creating form builders

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_field_typeObject

Tracks the field currently being “processed”



15
16
17
# File 'lib/transit/builders/form_builder.rb', line 15

def current_field_type
  @current_field_type
end

#field_orderObject

Tracks the order in which fields are used in the form, this allows the easy rebuilding of the submitted form data when submitted since normally the hash isn’t ordered.



13
14
15
# File 'lib/transit/builders/form_builder.rb', line 13

def field_order
  @field_order
end

#templateObject

Access the template object



10
11
12
# File 'lib/transit/builders/form_builder.rb', line 10

def template
  @template
end

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



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/transit/builders/form_builder.rb', line 57

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

#fields_for(record_or_name_or_array, *args, &block) ⇒ Object

Overrides fields_for to make sure the form builder is set properly



72
73
74
75
76
77
# File 'lib/transit/builders/form_builder.rb', line 72

def fields_for(record_or_name_or_array, *args, &block) #:nodoc:
  opts = args.extract_options!
  opts.merge!(:builder => Transit::Builders::FormBuilder)
  args.push(opts)
  super(record_or_name_or_array, *args, &block)
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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/transit/builders/form_builder.rb', line 33

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 << 'field_with_errors' 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



92
93
94
95
96
# File 'lib/transit/builders/form_builder.rb', line 92

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