Class: Reformed::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::OutputSafetyHelper
Defined in:
lib/reformed/form_builder.rb

Constant Summary collapse

@@html5 =
true
@@input_wrapper =
lambda { |controls, options|
  "<div class=\"input-control\">#{controls[:label]} #{controls[:input]} #{controls[:error]} #{controls[:hint]}</div>"
}
@@label_wrapper =
lambda { |controls, options|
  "<span>#{controls[:label]}</span>"
}
@@hint_wrapper =
lambda { |message, options|
  "<span class=\"hint\">#{message}</span>"
}
@@error_wrapper =
lambda { |message, options|
  "<span class=\"error\">#{message}</span>"
}

Instance Method Summary collapse

Instance Method Details

#error_wrap(str, options, &block) ⇒ Object



80
81
82
# File 'lib/reformed/form_builder.rb', line 80

def error_wrap(str, options, &block)
  @@error_wrapper.call(str, options)
end

#hint_wrap(str, options, &block) ⇒ Object



76
77
78
# File 'lib/reformed/form_builder.rb', line 76

def hint_wrap(str, options, &block)
  @@hint_wrapper.call(str, options)
end

#html5?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/reformed/form_builder.rb', line 90

def html5?
  @@html5
end

#input(method, options = {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/reformed/form_builder.rb', line 25

def input(method, options = {}, &block)
  controls = {}

  if options[:hint]
    controls[:hint] = hint_wrap(options.delete(:hint), control_options(options))
  end

  if options[:error]
    controls[:error] = error_wrap(options.delete(:error), control_options(options))
  end

  controls[:label] = label_wrap(method, control_options(options.merge(method: method), :label))

  controls[:input] = input_wrap(method, control_options(options))

  raw @@input_wrapper.call(controls, options)
end

#input_wrap(method, options, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/reformed/form_builder.rb', line 43

def input_wrap(method, options, &block)
  whatami = options[:as] || as(method)
  options.delete(:as)

  case whatami
    when :string   then text_field      method, options
    when :text     then text_area       method, options
    when :file     then file_field      method, options
    when :string   then text_field      method, options
    when :password then password_field  method, options
    when :radio    then radio_button    method, options[:choices], options
    when :boolean  then check_box       method, options
    when :url      then url_field       method, options
    when :email    then email_field     method, options
    when :phone    then phone_field     method, options
    when :number   then number_field    method, options
    when :date     then date_select     method, options, options.delete(:html) || {}
    when :time     then time_select     method, options, options.delete(:html) || {}
    when :datetime then datetime_select method, options, options.delete(:html) || {}
    when :select   then select          method, options[:choices], options, options.delete(:html) || {}
    when :hidden   then hidden_field    method, options
  end
end

#label_wrap(method, options = {}, &block) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/reformed/form_builder.rb', line 67

def label_wrap(method, options = {}, &block)
  label_text = options.delete(:label) if options[:label]
  if label_text
    @@label_wrapper.call({label: label(method, label_text, options, &block)}, options)
  else
    @@label_wrapper.call({label: label(method, options[:label], &block)}, options)
  end
end

#reform_fields_for(record_or_name_or_array, *args, &block) ⇒ Object



84
85
86
87
88
# File 'lib/reformed/form_builder.rb', line 84

def reform_fields_for(record_or_name_or_array, *args, &block)
  options = args.extract_options!
  options[:builder] ||= self.class
  fields_for(record_or_name_or_array, *(args << options), &block)
end