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.



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#fieldset(attrs, &blk) ⇒ Object



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

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



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

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



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

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

#unbound_check_box(attrs) ⇒ Object



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

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



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

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



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

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



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

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



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

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