class Megatron::Form

Attributes

style[RW]

Public Class Methods

new(object_name, object, template, options) click to toggle source
Calls superclass method
# File lib/megatron/form.rb, line 6
def initialize(object_name, object, template, options)
  super
  @style = options.delete(:style) || "table"
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
# File lib/megatron/form.rb, line 65
def method_missing(method, *args, &block)
  @template.send(method, *args, &block)
end
password_field(method, options = {}) click to toggle source
Calls superclass method
# File lib/megatron/form.rb, line 28
def password_field(method, options = {})
  if errors_on?(method)
    options[:class] ||= ""
    options[:class] += " error"
  end

  wrapper method, options.delete(:label) || method.to_s.humanize, super(method, options)
end
select(method, choices = nil, options = {}, html_options = {}, &block) click to toggle source
Calls superclass method
# File lib/megatron/form.rb, line 46
def select(method, choices = nil, options = {}, html_options = {}, &block)
  if errors_on?(method)
    html_options[:class] ||= ""
    html_options[:class] += " error"
  end

  field = content_tag(:div, class: 'select_box') { super(method, choices, options, html_options, &block) }

  wrapper method, options.delete(:label) || method.to_s.humanize, field
end
stacked_form(&block) click to toggle source
# File lib/megatron/form.rb, line 11
def stacked_form(&block)
  content_tag :div, class: 'stacked-form', &block
end
submit(btn_text, args = {}) click to toggle source
# File lib/megatron/form.rb, line 57
def submit(btn_text, args = {})
  text = "<footer class='form-footer'>".html_safe
  text << button(btn_text, {class: "primary-btn"}.merge(args))
  text << "</footer>".html_safe

  text
end
table_form(&block) click to toggle source
# File lib/megatron/form.rb, line 15
def table_form(&block)
  content_tag :div, class: ['table', 'table-form'], &block
end
text_area(method, options = {}) click to toggle source
Calls superclass method
# File lib/megatron/form.rb, line 37
def text_area(method, options = {})
  if errors_on?(method)
    options[:class] ||= ""
    options[:class] += " error"
  end

  wrapper method, options.delete(:label) || method.to_s.humanize, super(method, options)
end
text_field(method, options = {}) click to toggle source
Calls superclass method
# File lib/megatron/form.rb, line 19
def text_field(method, options = {})
  if errors_on?(method)
    options[:class] ||= ""
    options[:class] += " error"
  end

  wrapper method, options.delete(:label) || method.to_s.humanize, super(method, options)
end

Private Instance Methods

errors_on?(method) click to toggle source
# File lib/megatron/form.rb, line 106
def errors_on?(method)
  @object.present? && @object.respond_to?(:errors) && @object.errors[method].present?
end
stacked_row(&block) click to toggle source
# File lib/megatron/form.rb, line 110
def stacked_row(&block)
  content_tag :div, class: 'form-row', &block
end
stacked_wrapper(method, label, field) click to toggle source
# File lib/megatron/form.rb, line 114
def stacked_wrapper(method, label, field)
  concat(stacked_row {
    concat label(method, label, class: [('red-text' if errors_on?(method))])
    concat field
  })

  if errors_on?(method)
    concat(stacked_row {
      content_tag :span, class: ['red-text', 'small-text'] do
        @object.errors[method].to_sentence
      end
    })
  end
end
table_cell(&block) click to toggle source
# File lib/megatron/form.rb, line 82
def table_cell(&block)
  content_tag(:div, class: 'table-cell', &block)
end
table_row(&block) click to toggle source
# File lib/megatron/form.rb, line 78
def table_row(&block)
  content_tag(:div, class: 'table-row', &block)
end
table_wrapper(method, label, field) click to toggle source
# File lib/megatron/form.rb, line 86
def table_wrapper(method, label, field)
  concat(table_row {
    concat(table_cell {
      label(method, label, class: [('red-text' if errors_on?(method))])
    })
    concat(table_cell { field })
  })

  if errors_on?(method)
    concat(table_row {
      concat(table_cell { ' ' })
      concat(table_cell {
        content_tag :p, class: 'red-text' do
          @object.errors[method].to_sentence
        end
      })
    })
  end
end
wrapper(*args) click to toggle source
# File lib/megatron/form.rb, line 71
def wrapper(*args)
  send("#{@style}_wrapper".to_sym, *args)
rescue NoMethodError
  Bugsnag.notify($!)
  raise "Could not find Megatron::Form form style '#{@style}'."
end