Class: Caisson::Helpers::Form::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/caisson/helpers/form/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(core) ⇒ Base

************************************************************************************* CONSTRUCTOR *************************************************************************************



9
10
11
# File 'lib/caisson/helpers/form/base.rb', line 9

def initialize(core)
  @core = core
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



193
# File 'lib/caisson/helpers/form/base.rb', line 193

def method_missing(*args, &block) return @core.send(*args, &block) end

Instance Method Details

#button(text, options = {}) ⇒ Object

************************************************************************************* PUBLIC INSTANCE METHODS *************************************************************************************



17
18
19
# File 'lib/caisson/helpers/form/base.rb', line 17

def button(text, options={})
  Caisson::Helpers::Form::Button.new(@core).build(text, options)
end

#button_to(text, options = {}, html_options = {}) ⇒ Object



21
22
23
# File 'lib/caisson/helpers/form/base.rb', line 21

def button_to(text, options={}, html_options={})
  Caisson::Helpers::Form::Button.new(@core).build_button_to(text, options, html_options)
end

#buttons(&block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/caisson/helpers/form/base.rb', line 25

def buttons(&block)
  content = '<div class="buttons row"><div class="large-12 columns">'
  content << @core.capture(self, &block)
  content << '</div></div>'

  return content.html_safe
end

#checkbox(name, selected = false, options = {}) ⇒ Object



74
75
76
# File 'lib/caisson/helpers/form/base.rb', line 74

def checkbox(name, selected=false, options={})
  new_field('checkbox', name, selected, options)
end

#label(text, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/caisson/helpers/form/base.rb', line 33

def label(text, options={})
  options.reverse_merge!(content: '', placeholder: nil, separator: ' : ')

  label_for = options[:content].scan(/<[input|textarea|select|span|div].+id="(.+?)"./).flatten.first

  attributes = {}
  attributes[:for] = label_for if label_for
  attributes[:class] = 'placeholder' if options[:placeholder]

  text = '&nbsp;'.html_safe if text.empty?
  text += options[:separator].to_s if not options[:placeholder]

  return (:label, text, attributes)
end

#line(label_str, content, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/caisson/helpers/form/base.rb', line 48

def line(label_str, content, options={})
  options.reverse_merge!(class: nil, id: nil, label_separator: ' : ', placeholder: false)

  css_class = ['row', 'form-line', options[:class]].compact.join(' ')

  line_content = '<div class="' + css_class + '"><div class="large-12 columns field">'
  line_content << label(label_str, content: content, separator: options[:label_separator], placeholder: options[:placeholder]) if not label_str.nil?
  line_content << content
  line_content << '</div></div>'

  return line_content.html_safe
end

#line_checkbox(label_str, field_name, selected = false, options = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/caisson/helpers/form/base.rb', line 82

def line_checkbox(label_str, field_name, selected=false, options={})
  options.reverse_merge!(label_separator: nil, wrap_field: false)

  options[:class] = [options[:class], 'line-checkbox'].compact.join(' ')

  return line(label_str, checkbox(field_name, selected), options)
end

#line_password(label_str, field_name, value = '', options = {}) ⇒ Object



90
91
92
93
94
# File 'lib/caisson/helpers/form/base.rb', line 90

def line_password(label_str, field_name, value='', options={})
  field_options = get_field_options(label_str, options)

  line(label_str, password(field_name, value, field_options), options)
end

#line_select(label_str, field_name, choices, selection, options = {}) ⇒ Object



96
97
98
# File 'lib/caisson/helpers/form/base.rb', line 96

def line_select(label_str, field_name, choices, selection, options={})
  line(label_str, select(field_name, choices, selection), options)
end

#line_static(label_str, value, options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/caisson/helpers/form/base.rb', line 100

def line_static(label_str, value, options={})
  options.reverse_merge!(wrap_field: false)

  options[:class] = [options[:class], 'line-static'].compact.join(' ')

  field_options = {}
  field_options[:date_format] = options.delete(:date_format) if options[:date_format]

  line(label_str, static(value, field_options), options)
end

#line_switch(label_str, field_name, choices, selection, options = {}) ⇒ Object



111
112
113
# File 'lib/caisson/helpers/form/base.rb', line 111

def line_switch(label_str, field_name, choices, selection, options={})
  line(label_str, switch(field_name, choices, selection), options)
end

#line_text(label_str, field_name, value = '', options = {}) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/caisson/helpers/form/base.rb', line 115

def line_text(label_str, field_name, value='', options={})
  options.reverse_merge!(wrap_field: false)

  field_options = get_field_options(label_str, options)

  line(label_str, text(field_name, value, field_options), options)
end

#line_time(label_str, field_name, value = '', options = {}) ⇒ Object



123
124
125
# File 'lib/caisson/helpers/form/base.rb', line 123

def line_time(label_str, field_name, value='', options={})
  line(label_str, time(field_name, value), options)
end

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



127
128
129
# File 'lib/caisson/helpers/form/base.rb', line 127

def password(name, value='', options={})
  new_field('password', name, value, options)
end

#select(name, choices, selection, options = {}) ⇒ Object



131
132
133
134
135
# File 'lib/caisson/helpers/form/base.rb', line 131

def select(name, choices, selection, options={})
  options.reverse_merge!(submit: false, translate: false)

  new_field('select', name, choices, selection, options)
end

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



137
138
139
# File 'lib/caisson/helpers/form/base.rb', line 137

def static(value, options={})
  new_field('static', value, options)
end

#switch(name, choices, selection, options = {}) ⇒ Object



141
142
143
144
145
# File 'lib/caisson/helpers/form/base.rb', line 141

def switch(name, choices, selection, options={})
  options.reverse_merge!(submit: false, translate: false)

  new_field('switch', name, choices, selection, options)
end

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



147
148
149
# File 'lib/caisson/helpers/form/base.rb', line 147

def text(name, value='', options={})
  new_field('text', name, value, options)
end

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



151
152
153
# File 'lib/caisson/helpers/form/base.rb', line 151

def time(name, value, options={})
  new_field('time_field', name, value, options)
end