Class: Megatron::Form

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/megatron/form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options) ⇒ Form

Returns a new instance of Form.



6
7
8
9
# File 'lib/megatron/form.rb', line 6

def initialize(object_name, object, template, options)
  super
  @style = options.delete(:style) || "table"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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

def method_missing(method, *args, &block)
  @template.send(method, *args, &block)
end

Instance Attribute Details

#styleObject

Returns the value of attribute style.



4
5
6
# File 'lib/megatron/form.rb', line 4

def style
  @style
end

Instance Method Details

#password_field(method, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# 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 = (: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) ⇒ Object



11
12
13
# File 'lib/megatron/form.rb', line 11

def stacked_form(&block)
   :div, class: 'stacked-form', &block
end

#submit(btn_text, args = {}) ⇒ Object



57
58
59
60
61
62
63
# 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) ⇒ Object



15
16
17
# File 'lib/megatron/form.rb', line 15

def table_form(&block)
   :div, class: ['table', 'table-form'], &block
end

#text_area(method, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
# 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 = {}) ⇒ Object



19
20
21
22
23
24
25
26
# 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