Class: Fluffery::Forms::Builder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
Utilities
Defined in:
lib/fluffery/forms/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#confirm_or_disable, #content_tag, #error_console, #option_exists?, #render_with_fluff, #validator, #without_error_proc

Constructor Details

#initialize(object_name, object, template, options, proc) ⇒ Builder

Sets up options custom to our form builder. It also overrides the default error proc so we can use something more custom.



15
16
17
18
19
20
# File 'lib/fluffery/forms/builder.rb', line 15

def initialize(object_name, object, template, options, proc)
  @field_order ||= []
  without_error_proc do
    super(object_name, object, template, options, proc)          
  end        
end

Instance Attribute Details

#field_orderObject

Access the template object, and create an accessor which will track the order in which our fields are used in the form. This way we have the ability to capture that state for re-use in emails etc.



10
11
12
# File 'lib/fluffery/forms/builder.rb', line 10

def field_order
  @field_order
end

#templateObject

Access the template object, and create an accessor which will track the order in which our fields are used in the form. This way we have the ability to capture that state for re-use in emails etc.



10
11
12
# File 'lib/fluffery/forms/builder.rb', line 10

def template
  @template
end

Instance Method Details

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

Creates a html button tag for use in forms instead of the default input submit. Uses a span to wrap the content so it can be styled with css.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fluffery/forms/builder.rb', line 25

def button(value = nil, options = {})
  value, options = nil, value if value.is_a?(Hash)
  value ||= submit_default_value
  
  options = confirm_or_disable(options)        
  options = Fluffery::Utils::Internal.merge_html_classes(options, 'button')

  (:button, options.reverse_merge!({ "type" => "submit", "name" => "commit" })) do
    (:span, value.to_s)
  end

end

#date_select(method, options = {}, html_options = {}) ⇒ Object

Renders the default date_select but with an error wrapping if the method in question has errors



66
67
68
69
70
# File 'lib/fluffery/forms/builder.rb', line 66

def date_select(method, options = {}, html_options = {})
  render_with_fluff(method, options, html_options) do
    super(method, options, html_options)
  end        
end

#email_field(method, options = {}) ⇒ Object



57
58
59
60
61
# File 'lib/fluffery/forms/builder.rb', line 57

def email_field(method, options = {})
  render_with_fluff(method, options) do
    super(method, options.merge!('type' => 'email'))
  end
end

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

Custom label tag including an asterisk if the field is required.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fluffery/forms/builder.rb', line 40

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

  options, text = text, nil if text.is_a?(Hash)
  text ||= method.to_s.humanize
  options.stringify_keys!
  
  options.reverse_merge!('transform' => :titleize) unless option_exists?(options['transform'])
  text.send(options['transform'].to_sym)
  options.delete('transform')
  
  # Check to see if :required is set on a label, or if the attribute/method has a validator that require it exists. 
  # If so, add a * to the label.
  text = "#{text} <abbr title='Required'>*</abbr>".html_safe if validator.attribute_required?(method, options)
  super(method, text, options, &block)

end

#number_field(method, range, options = {}) ⇒ Object

HTML5 Number field, again, falls back to text in unsupportive browsers.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fluffery/forms/builder.rb', line 74

def number_field(method, range, options = {})
  options, range = range, nil if range.is_a?(Hash)
  options.stringify_keys!
  unless range.nil?
    range = (range.is_a?(Range)) ? range.to_a : range
    options.merge!('min' => range.first, 'max' => range.last)
  end
  
  render_with_fluff(method, options) do
    super(method, options.merge!('type' => 'number'))
  end
  
end

#password_field(method, options = {}) ⇒ Object



88
89
90
91
92
# File 'lib/fluffery/forms/builder.rb', line 88

def password_field(method, options = {})
  render_with_fluff(method, options) do
    super(method, options)
  end
end

#select(method, choices, options = {}, html_options = {}) ⇒ Object



94
95
96
97
98
# File 'lib/fluffery/forms/builder.rb', line 94

def select(method, choices, options = {}, html_options = {})
  render_with_fluff(method, options, html_options) do
    @template.select(@object_name, method, choices, options, html_options)
  end
end

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fluffery/forms/builder.rb', line 100

def state_select(method, options = {}, html_options = {})
  
  state_options = [['Please Select',nil]]
  state_options << ['International', (int || 'International')] if int = options.delete(:international)
  [
      ['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"]
  ].each do |state|
    should_abbr = options.delete(:abbreviate)
    state_options << (should_abbr ? state : [state.first, state.first])
  end
  
  select(method, @template.options_for_select(state_options, @object.try(:state)), options, html_options)
  
end

#text_area(method, options = {}) ⇒ Object



129
130
131
132
133
# File 'lib/fluffery/forms/builder.rb', line 129

def text_area(method, options = {})
  render_with_fluff(method, options) do
    super(method, options)
  end
end

#text_field(method, options = {}) ⇒ Object



123
124
125
126
127
# File 'lib/fluffery/forms/builder.rb', line 123

def text_field(method, options = {})
  render_with_fluff(method, options) do
    super(method, options)
  end
end

#url_field(method, options = {}) ⇒ Object



135
136
137
138
139
# File 'lib/fluffery/forms/builder.rb', line 135

def url_field(method, options = {})
  render_with_fluff(method, options) do
    super(method, options.merge!('type' => 'url'))
  end
end