Module: Ramaze::Helper::Form

Included in:
CortexReaver::Model::Renderer
Defined in:
lib/cortex_reaver/helper/form.rb

Overview

Helps make forms, especially by escaping.

Instance Method Summary collapse

Instance Method Details

#attr_h(string) ⇒ Object

Escapes quotes for HTML attributes.



123
124
125
# File 'lib/cortex_reaver/helper/form.rb', line 123

def attr_h(string)
  h(string).gsub('"', '"')
end

#errors_list(errors) ⇒ Object

Displays a list of errors.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cortex_reaver/helper/form.rb', line 12

def errors_list(errors)
  unless errors.empty?
    s = "<div class=\"form-errors\">\n<h3>Errors</h3>\n<ul>\n"
    errors.each do |attribute, error|
      if error.kind_of? Array
        error = error.join(', ')
      end
      s << "<li>#{attribute.to_s.titleize} #{error}.</li>\n"
    end
    s << "</ul></div>"
  else
    ''
  end
end

#errors_on(model) ⇒ Object

Displays errors on a record.



7
8
9
# File 'lib/cortex_reaver/helper/form.rb', line 7

def errors_on(model)
  errors_list(model.errors)
end

#form_for(model, action, fields = []) ⇒ Object

Makes a form for a model. Takes a model, form action, and an array of fields, which are passed to form_p.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cortex_reaver/helper/form.rb', line 29

def form_for(model, action, fields = [])
  if model.nil?
    raise ArgumentError.new("needs a model")
  elsif action.nil?
    raise ArgumentError.new("needs an action")
  end

  f = ''

  if model
    f << errors_on(model) 
  end

  f << "<form action=\"#{action}\" method=\"post\" class=\"edit-form\">"

  fields.each do |field|
    case field
    when Array
      f << form_p(field[0], ({:model => model}.merge!(field[1] || {})))
    else
      f << form_p(field, :model => model)
    end
  end

  f << form_submit("Submit #{model.class.to_s.demodulize.titleize}")
end

#form_p(id, params = {}) ⇒ Object

Makes a paragraph for accessing an attribute. Escapes the default value. Parameters:

id => the HTML id/name of the form field.
:type => the type of form field to generate (text, password, checkbox...)
:description => The label for the field (inferred from id by default)
:model => The model to query for fields
:default => The default value for the field (inferred from model and id)
:p_class => Class attached to the paragraph


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cortex_reaver/helper/form.rb', line 64

def form_p(id, params = {})
  type = params[:type].to_s
  p_class = params[:p_class]
  description = params[:description] || id.to_s.titleize
  model = params[:model]
  default = params[:default]
  if model and not default and model.respond_to? id
    default = model.send(id)
  end
  errors = params[:errors]
  if !errors and model.respond_to? :errors
    errors = model.errors
  else
    errors = {}
  end
  error = errors[id]
  
  p_class = "#{p_class} error" if error

  unless type
    case default
    when true
      type = 'checkbox'
  when false
      type = 'checkbox'
    else
      type = 'text'
    end
  end

  f = "<p #{p_class.nil? ? '' : 'class="' + attr_h(p_class) + '"'}>"
  if type == 'checkbox' or type == 'hidden'
  elsif type == 'textarea'
    f << "<label class=\"textarea\" for=\"#{id}\">#{description}</label><br />"
  else
    f << "<label for=\"#{id}\">#{description}</label>"
  end

  case type
  when 'textarea'
    f << "<textarea name=\"#{id}\" id=\"#{id}\">#{Rack::Utils::escape_html default}</textarea>"
  when 'checkbox'
    f << "<input type=\"checkbox\" name=\"#{id}\" id=\"#{id}\" #{default ? 'checked="checked"' : ''} />"
  else
   f << "<input name=\"#{id}\" id=\"#{id}\" type=\"#{type}\" value=\"#{attr_h(default)}\" />"
  end

  if type == 'checkbox'
    f << "<label class=\"checkbox\" for=\"#{id}\">#{description}</label>"
  end

  f << "</p>\n"
end

#form_submit(value = "Submit") ⇒ Object



118
119
120
# File 'lib/cortex_reaver/helper/form.rb', line 118

def form_submit(value = "Submit")
  '<p><input name="submit" type="submit" value="' + attr_h(value) + '" /></p>'
end