Class: Microframe::FormHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/microframe/controller/form_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, target_link) ⇒ FormHelper

Returns a new instance of FormHelper.



5
6
7
8
9
10
# File 'lib/microframe/controller/form_helper.rb', line 5

def initialize (target, target_link)
  @target = target
  @target_name = target.class.to_s.downcase
  @link = target_link
  @form_started = false
end

Instance Attribute Details

#form_startedObject (readonly)

Returns the value of attribute form_started.



3
4
5
# File 'lib/microframe/controller/form_helper.rb', line 3

def form_started
  @form_started
end

#targetObject (readonly)

Returns the value of attribute target.



3
4
5
# File 'lib/microframe/controller/form_helper.rb', line 3

def target
  @target
end

#target_idObject (readonly)

Returns the value of attribute target_id.



3
4
5
# File 'lib/microframe/controller/form_helper.rb', line 3

def target_id
  @target_id
end

#target_nameObject (readonly)

Returns the value of attribute target_name.



3
4
5
# File 'lib/microframe/controller/form_helper.rb', line 3

def target_name
  @target_name
end

Instance Method Details

#check_box(name, val = nil) ⇒ Object



39
40
41
42
# File 'lib/microframe/controller/form_helper.rb', line 39

def check_box(name, val=nil)
  val ||= target.send(name)
  gatekeeper "<input type = 'checkbox' name = '#{target_name}[#{name}]' checked = '#{val}' value = 'true'/>"
end

#gatekeeper(output) ⇒ Object



50
51
52
# File 'lib/microframe/controller/form_helper.rb', line 50

def gatekeeper(output)
  form_started ? output : start_form + output
end

#hidden(val) ⇒ Object



44
45
46
47
48
# File 'lib/microframe/controller/form_helper.rb', line 44

def hidden(val)
  name = val.class.to_s.downcase
  val = val.id
  gatekeeper "<input type = 'hidden' name = '#{name}_id' value = '#{val}'/>"
end

#label(name) ⇒ Object



19
20
21
# File 'lib/microframe/controller/form_helper.rb', line 19

def label(name)
  gatekeeper "<label>#{name}</label>"
end

#start_formObject



12
13
14
15
16
17
# File 'lib/microframe/controller/form_helper.rb', line 12

def start_form
  @form_started = true
  @target_id = target.id ? target.id : nil
  @link ||= "/#{target_name}s/#{target_id || ""}"
  "<form action='#{@link}' method='post'>"
end

#submitObject



31
32
33
34
35
36
37
# File 'lib/microframe/controller/form_helper.rb', line 31

def submit
  output = ""
  output += "<input type = 'hidden' name = '_method' value = 'put'/>" if target_id
  output += "<input type = 'submit' value = 'save' />"
  output += "</form>"
  gatekeeper output
end

#text_area(name) ⇒ Object



23
24
25
# File 'lib/microframe/controller/form_helper.rb', line 23

def text_area(name)
  gatekeeper "<textarea name = '#{target_name}[#{name}]'>#{target.send(name)}</textarea>"
end

#text_field(name) ⇒ Object



27
28
29
# File 'lib/microframe/controller/form_helper.rb', line 27

def text_field(name)
  gatekeeper "<input type = 'text' name = '#{target_name}[#{name}]' value = '#{target.send(name)}'/>"
end