Class: SofaGallery::FormBuilder

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

Instance Method Summary collapse

Instance Method Details

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sofa_gallery/form_builder.rb', line 18

def default_field(type, field, options = {}, &block)
  errors = if object.respond_to?(:errors) && object.errors[field].present?
    "<div class='errors'>#{[object.errors[field]].flatten.first}</div>"
  end
  if desc = options.delete(:desc)
    desc = "<div class='desc'>#{desc}</div>"
  end
  %(
    <div class='form_element #{type}_element #{'errors' if errors}'>
      <div class='label'>#{label_for(field, options)}</div>
      <div class='value'>#{yield}</div>
      #{desc}
      #{errors}
    </div>
  ).html_safe
end

#label_for(field, options) ⇒ Object



35
36
37
38
# File 'lib/sofa_gallery/form_builder.rb', line 35

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

#simple_field(label = nil, content = nil, options = {}, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/sofa_gallery/form_builder.rb', line 40

def simple_field(label = nil, content = nil, options = {}, &block)
  content ||= @template.capture(&block) if block_given?
  %(
    <div class='form_element #{options.delete(:class)}'>
      <div class='label'>#{label}</div>
      <div class='value'>#{content}</div>
    </div>
  ).html_safe
end