Class: Parlez::FormBuilder

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

Instance Method Summary collapse

Instance Method Details

#errors_on(method) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/parlez/form_builder.rb', line 48

def errors_on(method)
  if object and object.respond_to?(:errors)
    if object.errors[method].present?
      errors = object.errors[method]
      "<div class='error_message'>#{errors.is_a?(Array) ? errors.first : errors}</div>"
    end
  end
end

#label_for(field, options) ⇒ Object



33
34
35
36
# File 'lib/parlez/form_builder.rb', line 33

def label_for(field, options)
  label = options.delete(:label) || field.to_s.titleize
  "<label for=\"#{object_name}_#{field}\">#{label}</label>".html_safe
end

#rich_area(field, options = {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/parlez/form_builder.rb', line 18

def rich_area(field, options = {})
  options.merge!({:class => 'rich_text'})
  structured_field 'text_area', field, options do
    text_area(field, options)
  end
end

#structured_field(type, field, options = {}, &block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/parlez/form_builder.rb', line 25

def structured_field(type, field, options = {}, &block)
  out = "<div class='form_field #{type}'>"
  out << "<div class='label'>#{label_for(field, options)}</div>" if !options[:omit_label]
  out << "<div class='field'>#{errors_on(field)}#{yield}</div>"
  out << "</div>"
  out.html_safe
end

#submit(value, options = {}, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/parlez/form_builder.rb', line 38

def submit(value, options = {}, &block)
  extra_content = @template.capture(&block) if block_given?
  cancel_link ||= options[:cancel_url] ? ' or ' + options.delete(:cancel_url) : ''
  %(
    <div class='form_element submit_element'>
      #{super(value, options)} #{extra_content} #{cancel_link}
    </div>
  ).html_safe
end