Module: Cuba::FormHelpers

Includes:
Prelude
Defined in:
lib/cuba/contrib/form_helpers.rb

Instance Method Summary collapse

Methods included from Prelude

#escape_html, #urlencode

Instance Method Details

#cuba_contrib_form(record) ⇒ Object



13
14
15
# File 'lib/cuba/contrib/form_helpers.rb', line 13

def cuba_contrib_form(record)
  view(cuba_contrib_path("form"), model: record)
end

#cuba_contrib_partial(template, locals = {}) ⇒ Object



5
6
7
# File 'lib/cuba/contrib/form_helpers.rb', line 5

def cuba_contrib_partial(template, locals = {})
  partial(cuba_contrib_path("%s.mote" % template), locals)
end

#cuba_contrib_path(*args) ⇒ Object



9
10
11
# File 'lib/cuba/contrib/form_helpers.rb', line 9

def cuba_contrib_path(*args)
  File.join(Cuba::CONTRIB_ROOT, "views", *args)
end

#datefield(model, field, hint = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/cuba/contrib/form_helpers.rb', line 31

def datefield(model, field, hint = nil)
  input(model, field, hint) do
    cuba_contrib_partial("textfield",
      name: field_name(model, field),
      value: model.send(field),
      class: "date"
    )
  end
end


75
76
77
78
79
80
81
82
# File 'lib/cuba/contrib/form_helpers.rb', line 75

def dropdown(model, field, options, hint = nil)
  input(model, field, hint) do
    cuba_contrib_partial("select",
      name: field_name(model, field),
      options: select_options(options, model.send(field))
    )
  end
end

#field_name(model, field) ⇒ Object



103
104
105
# File 'lib/cuba/contrib/form_helpers.rb', line 103

def field_name(model, field)
  "#{underscore(model.class.name)}[#{field}]"
end

#filefield(model, field, hint = nil) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/cuba/contrib/form_helpers.rb', line 58

def filefield(model, field, hint = nil)
  input(model, field, hint) do
    cuba_contrib_partial("file",
      name: field_name(model, field)
    )
  end
end

#input(model, field, hint) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/cuba/contrib/form_helpers.rb', line 84

def input(model, field, hint)
  cuba_contrib_partial("field",
    label: humanize(field),
    input: yield,
    hint: hint,
    error: localize_errors(model, field).join(", ")
  )
end

#localize_errors(model, field) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/cuba/contrib/form_helpers.rb', line 93

def localize_errors(model, field)
  model.errors[field].map do |err|
    unless settings.localized_errors[err]
      raise "No localized error defined for: #{err}"
    end

    settings.localized_errors[err] % { field: humanize(field) }
  end
end

#option_tag(label, value, selected = nil) ⇒ Object



27
28
29
# File 'lib/cuba/contrib/form_helpers.rb', line 27

def option_tag(label, value, selected = nil)
  cuba_contrib_partial("option", selected: selected, value: value, label: label)
end

#password_field(model, field, hint = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/cuba/contrib/form_helpers.rb', line 50

def password_field(model, field, hint = nil)
  input(model, field, hint) do
    cuba_contrib_partial("password",
      name: field_name(model, field)
    )
  end
end

#select_options(pairs, selected = nil, prompt = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/cuba/contrib/form_helpers.rb', line 17

def select_options(pairs, selected = nil, prompt = nil)
  "".tap do |ret|
    ret << option_tag(prompt, "") if prompt

    pairs.each do |label, value|
      ret << option_tag(label, value, selected)
    end
  end
end

#textarea(model, field, hint = nil) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/cuba/contrib/form_helpers.rb', line 66

def textarea(model, field, hint = nil)
  input(model, field, hint) do
    cuba_contrib_partial("textarea",
      name: field_name(model, field),
      value: model.send(field)
    )
  end
end

#textfield(model, field, hint = nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/cuba/contrib/form_helpers.rb', line 41

def textfield(model, field, hint = nil)
  input(model, field, hint) do
    cuba_contrib_partial("textfield",
      name: field_name(model, field),
      value: model.send(field)
    )
  end
end