Class: TblForm::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/tbl_form/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#back(method = '', options = {}) ⇒ Object



126
127
128
# File 'lib/tbl_form/form_builder.rb', line 126

def back method = '', options = {}
  "</td></tr>".html_safe
end

#button(type, *args, &block) ⇒ Object

Creates a button:

form_for @user do |f|
  f.button :submit
end

It just acts as a proxy to method name given.



14
15
16
17
18
19
20
# File 'lib/tbl_form/form_builder.rb', line 14

def button(type, *args, &block)
  if respond_to?(:"#{type}_button")
    send(:"#{type}_button", *args, &block)
  else
    send(type, *args, &block)
  end
end

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



101
102
103
104
105
106
# File 'lib/tbl_form/form_builder.rb', line 101

def calendar_field method, options = {}
  options.to_options!
  expired = options.delete(:expired) || false
  if not expired; options.merge!(:class => 'calendar'); else; options.merge!(:disabled => true); end
  text_field method, options
end

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



88
89
90
91
# File 'lib/tbl_form/form_builder.rb', line 88

def check_box method, options = {}
  options.to_options!
  front(method, options) + super + back(method, options)
end

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



108
109
110
111
# File 'lib/tbl_form/form_builder.rb', line 108

def date_select method, options = {}
  options.to_options!
  front(method, options) + super + back(method, options)
end

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



53
54
55
56
# File 'lib/tbl_form/form_builder.rb', line 53

def display(method, options = {})
  options.to_options!
  front(method, options) + ("<span class=\"ms_field_content\">#{method.is_a?(String) ? method.to_s : @object[method.to_s]}</span>").html_safe + back(method, options)
end

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



78
79
80
81
# File 'lib/tbl_form/form_builder.rb', line 78

def file_field method, options = {}
  options.to_options!
  front(method, options) + super + back(method, options)
end

#front(method = '', options = {}) ⇒ Object



119
120
121
122
123
124
# File 'lib/tbl_form/form_builder.rb', line 119

def front method = '', options = {}
  fieldstyle = options.delete(:fieldstyle) || ""
  fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
  fieldextra = options.delete(:fieldextra) || ""
  "<tr class=\"tbl_field\", style=\"#{fieldstyle}\"><th class=\"tbl_field_label\">#{fieldextra}#{label(method, options)}</th><td>".html_safe
end

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

Generates a label

If options includes :for, that is used as the :for of the label. Otherwise, “#form’s object name_#method” is used.

If options includes :label, that value is used for the text of the label. Otherwise, “#titleized: ” is used.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tbl_form/form_builder.rb', line 31

def label method, options = {}
  text = options.delete(:label) ||  "#{method.to_s.titleize}: "
  hint = options.delete(:hint) || ""
  text = text + " <span class=\"ms_hint\">#{hint}</span>" unless hint.blank?
  if options[:for]
    "<label class=\"ms_label\" for='#{options.delete(:for)}'>#{text}</label>"
  else
    #need to use InstanceTag to build the correct ID for :for
    ActionView::Helpers::InstanceTag.new(@object_name, method, self, @object).to_label_tag(text, {:class=>"ms_label"})
  end
end

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



73
74
75
76
# File 'lib/tbl_form/form_builder.rb', line 73

def password_field method, options = {}
  options.to_options!
  front(method, options) + super + back(method, options)
end

#radio_button(method, tag_value, options = {}) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/tbl_form/form_builder.rb', line 93

def radio_button method, tag_value, options = {}
  options.to_options!
  fieldstyle = options.delete(:fieldstyle) || ""
  fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
  fieldextra = options.delete(:fieldextra) || ""
  front(method, options) + super + back(method, options)
end

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



58
59
60
61
# File 'lib/tbl_form/form_builder.rb', line 58

def select method, choices, options = {}, html_options = {}
  options.to_options!
  front(method, options) + super + back(method, options)
end

#submit(value = "Submit", options = {}) ⇒ Object



43
44
45
46
47
# File 'lib/tbl_form/form_builder.rb', line 43

def submit(value = "Submit", options = {})
  text = options.delete(:label) || value
  tag_str = tag( :input, { "type" => "submit", "name" => "commit", "value" => text }.update(options.stringify_keys) )
  output  = "<br /><div class=\"button_bar\">#{ tag_str }</div>".html_safe
end

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



83
84
85
86
# File 'lib/tbl_form/form_builder.rb', line 83

def text_area method, options = {}
  options.to_options!
  front(method, options) + super + back(method, options)
end

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



63
64
65
66
67
68
69
70
71
# File 'lib/tbl_form/form_builder.rb', line 63

def text_field method, options = {}
  options.to_options!
  naked = options.delete(:naked) || false
  if naked
    super
  else
    front(method, options) + super + back(method, options)
  end
end

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



49
50
51
# File 'lib/tbl_form/form_builder.rb', line 49

def value(method, options = {})
  @object[method.to_s]
end