Module: Rad::Html::FormHelper

Defined in:
lib/rad/html/_helpers/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_form_model_helper_and_form_options(model_name, model, options = {}) ⇒ Object



13
14
15
16
# File 'lib/rad/html/_helpers/form_helper.rb', line 13

def build_form_model_helper_and_form_options model_name, model, options = {}
  model_name.must_be.a String, Symbol
  return Rad::Html::ModelHelper.new(self, self, model_name, model, options), options
end

#check_box_tag(name, checked = false, options = {}) ⇒ Object

Form fields



44
45
46
47
48
# File 'lib/rad/html/_helpers/form_helper.rb', line 44

def check_box_tag name, checked = false, options = {}
  options = {type: "checkbox", name: name, value: '1'}.update(options)
  options[:checked] = "checked" if checked
  tag_with_style :input, '', options
end

#error_messages(*errors) ⇒ Object

Errors

delegate :error_messages, :field_with_errors, to: :form_builder



28
29
30
31
32
# File 'lib/rad/html/_helpers/form_helper.rb', line 28

def error_messages *errors
  errors = errors.first if errors.size == 1 and errors.first.is_a? Array
  html = errors.join("<br/>")
  tag :div, html, class: :error_messages
end

#field_set_tag(legend = nil, options = {}, &block) ⇒ Object



50
51
52
53
54
55
# File 'lib/rad/html/_helpers/form_helper.rb', line 50

def field_set_tag legend = nil, options = {}, &block
  content = ""
  content << tag(:legend, legend) unless legend.blank?
  content << capture(&block)
  tag_with_style :fieldset, content, options
end

#field_with_errors(name, errors, options, field_html) ⇒ Object



34
35
36
37
38
# File 'lib/rad/html/_helpers/form_helper.rb', line 34

def field_with_errors name, errors, options, field_html
  html = tag :div, errors.join("<br/>"), class: :field_error_messages
  html << tag(:span, field_html, class: :field_with_errors)
  html
end

#file_field_tag(name, options = {}) ⇒ Object



57
58
59
# File 'lib/rad/html/_helpers/form_helper.rb', line 57

def file_field_tag name, options = {}
  text_field_tag name, nil, options.update(type: "file")
end

#form_for(*args, &block) ⇒ Object

Form



5
6
7
8
9
10
11
# File 'lib/rad/html/_helpers/form_helper.rb', line 5

def form_for *args, &block
  model_helper, options = build_form_model_helper_and_form_options *args

  form_tag options do
    block.call model_helper if block
  end
end

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



18
19
20
21
# File 'lib/rad/html/_helpers/form_helper.rb', line 18

def form_tag options = {}, &block
  options['method'] ||= options[:method] || 'post'
  tag :form, options, &block
end

#hidden_field_tag(name, value = '', options = {}) ⇒ Object



61
62
63
# File 'lib/rad/html/_helpers/form_helper.rb', line 61

def hidden_field_tag name, value = '', options = {}
  text_field_tag(name, value, options.update(type: "hidden"))
end

#password_field_tag(name, value = nil, options = {}) ⇒ Object



65
66
67
# File 'lib/rad/html/_helpers/form_helper.rb', line 65

def password_field_tag name, value = nil, options = {}
  text_field_tag(name, value, options.update(type: "password"))
end

#radio_button_tag(name, value = '1', options = {}) ⇒ Object



69
70
71
72
73
74
# File 'lib/rad/html/_helpers/form_helper.rb', line 69

def radio_button_tag name, value = '1', options = {}
  options = {type: "radio", name: name, value: value, checked: false}.update(options)
  checked = options.delete :checked
  options["checked"] = "checked" if checked
  tag_with_style :input, '', options
end

#select_tag(name, selected, values, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rad/html/_helpers/form_helper.rb', line 76

def select_tag name, selected, values, options = {}
  buff = "\n"
  values.each do |n, v|
    o = {}
    o[:value] = v if v
    o[:selected] = 'selected' if (v || n) == selected
    buff << tag(:option, n, o)
    buff << "\n"
  end

  tag_with_style :select, buff, {name: name}.update(options)
end

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

Other



101
102
103
# File 'lib/rad/html/_helpers/form_helper.rb', line 101

def submit_tag value, options = {}
  tag_with_style :input, '', {type: "submit", value: value}.update(options)
end

#text_area_tag(name, value = '', options = {}) ⇒ Object



93
94
95
# File 'lib/rad/html/_helpers/form_helper.rb', line 93

def text_area_tag name, value = '', options = {}
  tag_with_style :textarea, value, {name: name}.update(options)
end

#text_field_tag(name, value = '', options = {}) ⇒ Object



89
90
91
# File 'lib/rad/html/_helpers/form_helper.rb', line 89

def text_field_tag name, value = '', options = {}
  tag_with_style :input, '', {type: "text", name: name, value: value}.update(options)
end