Class: Merb::Helpers::Form::Builder::Base

Inherits:
Object
  • Object
show all
Includes:
Tag
Defined in:
lib/merb-helpers/form/builder.rb

Direct Known Subclasses

Form

Instance Method Summary collapse

Methods included from Tag

#close_tag, #open_tag, #self_closing_tag, #tag

Constructor Details

#initialize(obj, name, origin) ⇒ Base

Returns a new instance of Base.



10
11
12
13
# File 'lib/merb-helpers/form/builder.rb', line 10

def initialize(obj, name, origin)
  @obj, @origin = obj, origin
  @name = name || @obj.class.name.snake_case.split("/").last
end

Instance Method Details

#bound_check_box(method, attrs = {}) ⇒ Object



45
46
47
48
49
# File 'lib/merb-helpers/form/builder.rb', line 45

def bound_check_box(method, attrs = {})
  name = control_name(method)
  update_bound_controls(method, attrs, "checkbox")
  unbound_check_box({:name => name}.merge(attrs))
end

#bound_radio_button(method, attrs = {}) ⇒ Object



62
63
64
65
66
# File 'lib/merb-helpers/form/builder.rb', line 62

def bound_radio_button(method, attrs = {})
  name = control_name(method)
  update_bound_controls(method, attrs, "radio")
  unbound_radio_button({:name => name, :value => control_value(method)}.merge(attrs))
end

#bound_radio_group(method, arr) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/merb-helpers/form/builder.rb', line 73

def bound_radio_group(method, arr)
  val = control_value(method)
  arr.map do |attrs|
    attrs = {:value => attrs} unless attrs.is_a?(Hash)
    attrs[:checked] = true if (val == attrs[:value])
    radio_group_item(method, attrs)
  end.join
end

#bound_select(method, attrs = {}) ⇒ Object



91
92
93
94
95
# File 'lib/merb-helpers/form/builder.rb', line 91

def bound_select(method, attrs = {})
  name = control_name(method)
  update_bound_controls(method, attrs, "select")
  unbound_select({:name => name}.merge(attrs))
end

#bound_text_area(method, attrs = {}) ⇒ Object



103
104
105
106
107
# File 'lib/merb-helpers/form/builder.rb', line 103

def bound_text_area(method, attrs = {})
  name = "#{@name}[#{method}]"
  update_bound_controls(method, attrs, "text_area")
  unbound_text_area(control_value(method), {:name => name}.merge(attrs))
end

#button(contents, attrs) ⇒ Object



114
115
116
117
# File 'lib/merb-helpers/form/builder.rb', line 114

def button(contents, attrs)
  update_unbound_controls(attrs, "button")
  tag(:button, contents, attrs)
end

#fieldset(attrs, &blk) ⇒ Object



21
22
23
24
25
# File 'lib/merb-helpers/form/builder.rb', line 21

def fieldset(attrs, &blk)
  legend = (l_attr = attrs.delete(:legend)) ? tag(:legend, l_attr) : ""
  tag(:fieldset, legend + @origin.capture(&blk), attrs)
  # @origin.concat(contents, blk.binding)
end

#form(attrs = {}, &blk) ⇒ Object



15
16
17
18
19
# File 'lib/merb-helpers/form/builder.rb', line 15

def form(attrs = {}, &blk)
  captured = @origin.capture(&blk)
  fake_method_tag = process_form_attrs(attrs)
  tag(:form, fake_method_tag + captured, attrs)
end

#submit(value, attrs) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/merb-helpers/form/builder.rb', line 119

def submit(value, attrs)
  attrs[:type]  ||= "submit"
  attrs[:name]  ||= "form_submit"
  attrs[:value] ||= value
  update_unbound_controls(attrs, "submit")
  self_closing_tag(:input, attrs)
end

#unbound_check_box(attrs) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/merb-helpers/form/builder.rb', line 51

def unbound_check_box(attrs)
  update_unbound_controls(attrs, "checkbox")
  if attrs.delete(:boolean)
    on, off = attrs.delete(:on), attrs.delete(:off)
    unbound_hidden_field(:name => attrs[:name], :value => off) <<
      self_closing_tag(:input, {:type => "checkbox", :value => on}.merge(attrs))
  else
    self_closing_tag(:input, {:type => "checkbox"}.merge(attrs))
  end
end

#unbound_radio_button(attrs) ⇒ Object



68
69
70
71
# File 'lib/merb-helpers/form/builder.rb', line 68

def unbound_radio_button(attrs)
  update_unbound_controls(attrs, "radio")
  self_closing_tag(:input, {:type => "radio"}.merge(attrs))
end

#unbound_radio_group(arr, attrs = {}) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/merb-helpers/form/builder.rb', line 82

def unbound_radio_group(arr, attrs = {})
  arr.map do |ind_attrs|
    ind_attrs = {:value => ind_attrs} unless ind_attrs.is_a?(Hash)
    joined = attrs.merge(ind_attrs)
    joined.merge!(:label => joined[:label] || joined[:value])
    unbound_radio_button(joined)
  end.join
end

#unbound_select(attrs = {}) ⇒ Object



97
98
99
100
101
# File 'lib/merb-helpers/form/builder.rb', line 97

def unbound_select(attrs = {})
  update_unbound_controls(attrs, "select")
  attrs[:name] << "[]" if attrs[:multiple] && !(attrs[:name] =~ /\[\]$/)
  tag(:select, options_for(attrs), attrs)
end

#unbound_text_area(contents, attrs) ⇒ Object



109
110
111
112
# File 'lib/merb-helpers/form/builder.rb', line 109

def unbound_text_area(contents, attrs)
  update_unbound_controls(attrs, "text_area")
  tag(:textarea, contents, attrs)
end