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

#add_css_class(attrs, new_class) ⇒ Object



49
50
51
# File 'lib/merb_helpers/form/builder.rb', line 49

def add_css_class(attrs, new_class)
  attrs[:class] = attrs[:class] ? "#{attrs[:class]} #{new_class}" : new_class
end

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



107
108
109
110
111
# File 'lib/merb_helpers/form/builder.rb', line 107

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



139
140
141
142
143
# File 'lib/merb_helpers/form/builder.rb', line 139

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

#bound_radio_group(method, arr) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/merb_helpers/form/builder.rb', line 175

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

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



163
164
165
166
167
# File 'lib/merb_helpers/form/builder.rb', line 163

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



198
199
200
201
202
# File 'lib/merb_helpers/form/builder.rb', line 198

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

#button(contents, attrs) ⇒ Object



150
151
152
153
# File 'lib/merb_helpers/form/builder.rb', line 150

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

#concat(attrs, &blk) ⇒ Object



13
14
15
# File 'lib/merb_helpers/form/builder.rb', line 13

def concat(attrs, &blk)
  @origin.concat(@origin.capture(&blk), blk.binding)
end

#fake_out_method(attrs, method) ⇒ Object

This can be overridden to use another method to fake out methods



45
46
47
# File 'lib/merb_helpers/form/builder.rb', line 45

def fake_out_method(attrs, method)
  self_closing_tag(:input, :type => "hidden", :name => "_method", :value => method)
end

#fieldset(attrs, &blk) ⇒ Object



17
18
19
20
21
# File 'lib/merb_helpers/form/builder.rb', line 17

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



23
24
25
26
27
28
# File 'lib/merb_helpers/form/builder.rb', line 23

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

  tag(:form, fake_method_tag + captured, attrs)
end

#process_form_attrs(attrs) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/merb_helpers/form/builder.rb', line 30

def process_form_attrs(attrs)
  method = attrs[:method]

  # Unless the method is :get, fake out the method using :post
  attrs[:method] = :post unless attrs[:method] == :get
  # Use a fake PUT if the object is not new, otherwise use the method
  # passed in. Defaults to :post if no method is set.
  method ||= (@obj.respond_to?(:new_record?) && !@obj.new_record?) || (@obj.respond_to?(:new?) && !@obj.new?) ? :put : :post

  attrs[:enctype] = "multipart/form-data" if attrs.delete(:multipart) || @multipart

  method == :post || method == :get ? "" : fake_out_method(attrs, method)
end

#submit(value, attrs) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/merb_helpers/form/builder.rb', line 155

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



113
114
115
116
117
118
119
120
121
122
# File 'lib/merb_helpers/form/builder.rb', line 113

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



145
146
147
148
# File 'lib/merb_helpers/form/builder.rb', line 145

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



184
185
186
187
188
189
190
191
# File 'lib/merb_helpers/form/builder.rb', line 184

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



169
170
171
172
173
# File 'lib/merb_helpers/form/builder.rb', line 169

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



193
194
195
196
# File 'lib/merb_helpers/form/builder.rb', line 193

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

#update_bound_check_box(method, attrs) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
# File 'lib/merb_helpers/form/builder.rb', line 68

def update_bound_check_box(method, attrs)
  raise ArgumentError, ":value can't be used with a bound_check_box" if attrs.has_key?(:value)

  attrs[:boolean] = attrs.fetch(:boolean, true)

  val = @obj.send(method)
  attrs[:checked] = attrs.key?(:on) ? val == attrs[:on] : considered_true?(val)
end

#update_bound_controls(method, attrs, type) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/merb_helpers/form/builder.rb', line 53

def update_bound_controls(method, attrs, type)
  case type
  when "checkbox"
    update_bound_check_box(method, attrs)
  when "select"
    update_bound_select(method, attrs)
  end
end

#update_bound_select(method, attrs) ⇒ Object



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

def update_bound_select(method, attrs)
  attrs[:value_method] ||= method
  attrs[:text_method] ||= attrs[:value_method] || :to_s
  attrs[:selected] ||= @obj.send(attrs[:value_method])
end

#update_unbound_check_box(attrs) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/merb_helpers/form/builder.rb', line 88

def update_unbound_check_box(attrs)
  boolean = attrs[:boolean] || (attrs[:on] && attrs[:off]) ? true : false

  case
  when attrs.key?(:on) ^ attrs.key?(:off)
    raise ArgumentError, ":on and :off must be specified together"
  when (attrs[:boolean] == false) && (attrs.key?(:on))
    raise ArgumentError, ":boolean => false cannot be used with :on and :off"
  when boolean && attrs.key?(:value)
    raise ArgumentError, ":value can't be used with a boolean checkbox"
  end

  if attrs[:boolean] = boolean
    attrs[:on] ||= "1"; attrs[:off] ||= "0"
  end

  attrs[:checked] = "checked" if attrs.delete(:checked)
end

#update_unbound_controls(attrs, type) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/merb_helpers/form/builder.rb', line 77

def update_unbound_controls(attrs, type)
  case type
  when "checkbox"
    update_unbound_check_box(attrs)
  when "file"
    @multipart = true
  end

  attrs[:disabled] ? attrs[:disabled] = "disabled" : attrs.delete(:disabled)
end