Module: Merb::Helpers::Form

Included in:
ViewContext
Defined in:
lib/form_helpers.rb

Instance Method Summary collapse

Instance Method Details

#checkbox_control(col, attrs = {}) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/form_helpers.rb', line 79

def checkbox_control(col, attrs = {})
  errorify_field(attrs, col)
  val = @_obj.send(col)
  attrs.merge!(:value => val ? "1" : "0")
  attrs.merge!(:checked => "checked") if val
  checkbox_field(control_name_value(col, attrs))
end

#checkbox_field(attrs = {}) ⇒ Object



87
88
89
90
91
# File 'lib/form_helpers.rb', line 87

def checkbox_field(attrs = {})
  attrs.merge!(:type => :checkbox)
  attrs.add_html_class!("checkbox")
  optional_label(attrs){self_closing_tag("input", attrs)}
end

#control_name(col) ⇒ Object



57
58
59
# File 'lib/form_helpers.rb', line 57

def control_name(col)
  "#{@_object_name}[#{col}]"
end

#control_name_value(col, attrs) ⇒ Object



65
66
67
# File 'lib/form_helpers.rb', line 65

def control_name_value(col, attrs)
  {:name => control_name(col), :value => control_value(col)}.merge(attrs)
end

#control_value(col) ⇒ Object



61
62
63
# File 'lib/form_helpers.rb', line 61

def control_value(col)
  @_obj.send(col)
end

#error_messages_for(obj, error_li = nil, html_class = 'submittal_failed') ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/form_helpers.rb', line 5

def error_messages_for(obj, error_li = nil, html_class='submittal_failed')
  return "" unless obj.errors
  header_message = block_given? ? yield(obj.errors) : "<h2>Form submittal failed because of #{obj.errors.size} problems</h2>"
  ret = %Q{
    <div class='#{html_class}'>
      #{header_message}
      <ul>
  }
  obj.errors.each {|err| ret << (error_li ? error_li.call(err) : "<li>#{err[0]} #{err[1]}</li>") }
  ret << %Q{
      </ul>
    </div>
  }
end

#fields_for(obj, attrs = nil, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/form_helpers.rb', line 46

def fields_for(obj, attrs=nil, &block)
  obj = obj_from_ivar_or_sym(obj)
  old_obj, @_obj = @_obj, obj
  @_object_name = "#{@_obj.class}".snake_case
  old_block, @_block = @_block, block
  
  concat(capture(&block), block.binding)

  @_obj, @_block = old_obj, old_block        
end

#form_for(obj, attrs = {}, &block) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/form_helpers.rb', line 37

def form_for(obj, attrs={}, &block)
  obj = obj_from_ivar_or_sym(obj)
  fake_form_method = set_form_method(attrs, obj)
  concat(open_tag("form", attrs), block.binding)
  concat(generate_fake_form_method(fake_form_method), block.binding) if fake_form_method
  fields_for(obj, attrs, &block)
  concat("</form>", block.binding)
end

#form_tag(attrs = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/form_helpers.rb', line 28

def form_tag(attrs = {}, &block)
  attrs.merge!( :enctype => "multipart/form-data" ) if attrs.delete(:multipart)
  fake_form_method = set_form_method(attrs)
  concat(open_tag("form", attrs), block.binding)
  concat(generate_fake_form_method(fake_form_method), block.binding) if fake_form_method
  concat(capture(&block), block.binding)
  concat("</form>", block.binding)
end

#hidden_control(col, attrs = {}) ⇒ Object



93
94
95
96
97
# File 'lib/form_helpers.rb', line 93

def hidden_control(col, attrs = {})
  attrs.delete(:label)
  errorify_field(attrs, col)
  hidden_field(control_name_value(col, attrs))
end

#hidden_field(attrs = {}) ⇒ Object



99
100
101
102
103
# File 'lib/form_helpers.rb', line 99

def hidden_field(attrs = {})
  attrs.delete(:label)
  attrs.merge!(:type => :hidden)
  self_closing_tag("input", attrs)
end

#label(name, contents = "", attrs = {}) ⇒ Object



143
144
145
# File 'lib/form_helpers.rb', line 143

def label(name, contents = "", attrs = {})
  tag("label", name.to_s + contents, attrs)
end

#obj_from_ivar_or_sym(obj) ⇒ Object



20
21
22
# File 'lib/form_helpers.rb', line 20

def obj_from_ivar_or_sym(obj)
  obj.is_a?(Symbol) ? instance_variable_get("@#{obj}") : obj
end

#radio_field(attrs = {}) ⇒ Object



117
118
119
120
121
# File 'lib/form_helpers.rb', line 117

def radio_field(attrs = {})
  attrs.merge!(:type => "radio")
  attrs.add_html_class!("radio")
  optional_label(attrs){self_closing_tag("input", attrs)}
end

#radio_group_control(col, options = {}, attrs = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/form_helpers.rb', line 105

def radio_group_control(col, options = {}, attrs = {})
  errorify_field(attrs, col)
  val = @_obj.send(col)
  ret = ""
  options.each do |opt|
    hash = {:name => "#{@_object_name}[#{col}]", :value => opt, :label => opt}
    hash.merge!(:selected => "selected") if val.to_s == opt.to_s
    ret << radio_field(hash)
  end
  ret
end

#submit_button(contents, attrs = {}) ⇒ Object



138
139
140
141
# File 'lib/form_helpers.rb', line 138

def submit_button(contents, attrs = {})
  attrs.merge!(:type => "submit")
  tag("button", contents, attrs)
end

#tag(tag_name, contents, attrs = {}) ⇒ Object



24
25
26
# File 'lib/form_helpers.rb', line 24

def tag(tag_name, contents, attrs = {})
  open_tag(tag_name, attrs) + contents + "</#{tag_name}>"
end

#text_area_control(col, attrs = {}) ⇒ Object



123
124
125
126
127
# File 'lib/form_helpers.rb', line 123

def text_area_control(col, attrs = {})
  attrs ||= {}
  errorify_field(attrs, col)
  text_area_field(control_value(col), attrs.merge(:name => control_name(col)))
end

#text_area_field(val, attrs = {}) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/form_helpers.rb', line 129

def text_area_field(val, attrs = {})
  val ||=""
  optional_label(attrs) do
    open_tag("textarea", attrs) +
    val +
    "</textarea>"
  end
end

#text_control(col, attrs = {}) ⇒ Object



69
70
71
72
# File 'lib/form_helpers.rb', line 69

def text_control(col, attrs = {})
  errorify_field(attrs, col)
  text_field(control_name_value(col, attrs))
end

#text_field(attrs = {}) ⇒ Object



74
75
76
77
# File 'lib/form_helpers.rb', line 74

def text_field(attrs = {})
  attrs.merge!(:type => "text")
  optional_label(attrs) { self_closing_tag("input", attrs) }
end