Class: GenericFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
Defined in:
lib/generic_form_builder.rb

Constant Summary collapse

STANDARD_FIELDS =
%w[
  text_field
  password_field
  text_area
  email_field
  file_field
  number_field
  date_field
].freeze

Instance Method Summary collapse

Instance Method Details

#buttons(options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/generic_form_builder.rb', line 69

def buttons(options = {})
  options = {
    cancel_class: ["cancel", "button"],
    wrapper_class: ["actions"]
  }.merge(options)

  buttons  = (:button,
                (:span, options[:submit_text] || 'Save'),
                type: 'submit',
                class: options[:button_class]
              )
  buttons << link_to('Cancel', options[:cancel_link], class: options[:cancel_class]) if options[:cancel_link]

  (:div, buttons, class: options[:wrapper_class])
end

#check_box(field, options = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/generic_form_builder.rb', line 55

def check_box(field, options = {})
  return super if options[:default_builder]
  label_text = options[:label] || field.to_s.humanize
  note       = note_html(options[:note])
  (:p, label(field, super + " #{label_text} #{errors_text(field)}".try(:html_safe) + note, :class => 'checkbox'))
end

#collection_select(field, collection, value_method, name_method, options = {}, html_options = {}) ⇒ Object



48
49
50
51
52
53
# File 'lib/generic_form_builder.rb', line 48

def collection_select(field, collection, value_method, name_method, options = {}, html_options = {})
  return super(field, collection, value_method, name_method) if options[:default_builder]
  label_text = options[:label] || field.to_s.humanize
  note       = note_html(options[:note])
  (:p, label(field, "#{label_text} #{errors_text(field)}".try(:html_safe)) + note + super(field, collection, value_method, name_method))
end

#radio_button(field, value, options = {}) ⇒ Object



62
63
64
65
66
67
# File 'lib/generic_form_builder.rb', line 62

def radio_button(field, value, options = {})
  return super if options[:default_builder]
  label_text = options.delete(:label) || field.to_s.humanize
  tag_type = options.delete(:tag_type) || :li
  (tag_type, label("#{field}_#{value.to_s.downcase.gsub(' ', '_')}", super + " #{label_text} #{errors_text(field)}".try(:html_safe), :class => 'radio', :onclick => ""))
end

#select(field, collection, options = {}, html_options = {}) ⇒ Object



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

def select(field, collection, options = {}, html_options = {})
  return super if options[:default_builder]
  label_text = options[:label] || field.to_s.humanize
  note       = note_html(options[:note])
  p_html_options = {}
  if any_errors?(field)
    p_html_options.merge!('class' => 'errors')
  end
  (:p, label(field, "#{label_text} #{errors_text(field)}".try(:html_safe)) + note + super, p_html_options)
end