Module: HandyForm::Helpers

Defined in:
lib/handy_form/helpers.rb

Instance Method Summary collapse

Instance Method Details

#error_tag(options) ⇒ Object



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

def error_tag(options)
  return "" if options.nil?
  case options
  when String
    text = options
    options = {:class => "error-message"}
  when Hash
    text = build_error_string(options.delete(:text))
    merge_option_strings!(options, :class, "error-message")
  end

  (:div, text, options)
end

#handy_field_wrapper(field, field_label, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/handy_form/helpers.rb', line 16

def handy_field_wrapper(field, field_label, options = {})
  options[:wrapper] ||= {}
  merge_option_strings!(options[:wrapper], :class, "row")

  hint = hint_tag(options.delete(:hint))
  error = error_tag(options.delete(:error))

  unless options.delete(:input_first)
    (:div, (
      field_label.html_safe + field.html_safe +
      hint.html_safe + error.html_safe
    ), options[:wrapper]).html_safe
  else
    (:div, (field.html_safe + field_label.html_safe + hint.html_safe + error.html_safe), options[:wrapper]).html_safe
  end
end

#handy_form_for(record_or_name_or_array, *args, &proc) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
# File 'lib/handy_form/helpers.rb', line 3

def handy_form_for(record_or_name_or_array, *args, &proc)
  raise ArgumentError, "Missing block" unless block_given?
  options = args.extract_options!
  merge_builder_options! options
  form_for(record_or_name_or_array, *(args << options), &proc)
end

#hint_tag(options) ⇒ Object



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

def hint_tag(options)
  return "" if options.nil?
  case options
  when String
    text = options
    options = {:class => "hint"}
  when Hash
    text = options.delete(:text)
    merge_option_strings!(options, :class, "error-message")
  end

  (:div, text, options)
end

#merge_builder_options!(options) ⇒ Object



10
11
12
13
14
# File 'lib/handy_form/helpers.rb', line 10

def merge_builder_options!(options)
  options.merge!(:builder => HandyForm::Builder)
  options[:html] ||= {}
  options[:html][:class] = "handy_form #{options[:html][:class]}".strip
end