Class: Penknife::EnhancedFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::FormTagHelper, ActionView::Helpers::TagHelper
Defined in:
lib/penknife/enhanced_form_builder.rb

Instance Method Summary collapse

Instance Method Details

#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



15
16
17
18
19
# File 'lib/penknife/enhanced_form_builder.rb', line 15

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  within_form_row(method, 'check_box', options) do
    @template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value)
  end
end

#radio_button(method, tag_value, options = {}) ⇒ Object



27
28
29
# File 'lib/penknife/enhanced_form_builder.rb', line 27

def radio_button(method, tag_value, options = {})
  @template.radio_button(@object_name, method, tag_value, objectify_options(options))
end

#select(method, choices, options = {}, html_options = {}) ⇒ Object



21
22
23
24
25
# File 'lib/penknife/enhanced_form_builder.rb', line 21

def select(method, choices, options = {}, html_options = {})
  within_form_row(method, 'check_box', options) do
    @template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options))
  end
end

#submit(value = "Save changes", options = {}) ⇒ Object



31
32
33
34
35
# File 'lib/penknife/enhanced_form_builder.rb', line 31

def submit(value = "Save changes", options = {})
  within_form_row('&nbsp', nil, options.merge(:title => '')) do
    submit_button(value, options.reverse_merge(:id => "#{object_name}_submit"))
  end
end

#submit_button(value = "Save changes", options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/penknife/enhanced_form_builder.rb', line 37

def submit_button(value = "Save changes", options = {})
  options.stringify_keys!

  if disable_with = options.delete("disable_with")
    disable_with = "this.value='#{disable_with}'"
    disable_with << ";#{options.delete('onclick')}" if options['onclick']
    
    options["onclick"] = [
      "this.setAttribute('originalValue', this.value)",
      "this.disabled=true",
      disable_with,
      "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit())",
      "if (result == false) { this.value = this.getAttribute('originalValue'); this.disabled = false }",
      "return result;",
    ].join(";")
  end

  if confirm = options.delete("confirm")
    options["onclick"] ||= ''
    options["onclick"] += "return #{confirm_javascript_function(confirm)};"
  end

  tag(:button, { "type" => "submit", "name" => "commit" }.update(options.stringify_keys), true) + value + "</button>"
end