Class: ComfortableMexicanSofa::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/comfortable_mexican_sofa/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/comfortable_mexican_sofa/form_builder.rb', line 18

def default_field(type, field, options = {}, &block)
  errors = if 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

#default_tag_field(tag, options = {}) ⇒ Object

– Tag Field Fields —————————————————–



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 57

def default_tag_field(tag, options = {})
  label     = options[:label] || tag.label.to_s.titleize
  css_class = options[:css_class] || tag.class.to_s.demodulize.underscore
  
  field_css_class = case tag
  when ComfortableMexicanSofa::Tag::PageRichText
    'rich_text'
  when ComfortableMexicanSofa::Tag::PageText, ComfortableMexicanSofa::Tag::FieldText
    'code'
  end
  
  options[:content_field_method] ||= :text_field_tag
  field = 
    options[:field] || 
    @template.send(options[:content_field_method], 'cms_page[blocks_attributes][][content]', tag.content, :id => nil, :class => field_css_class)
  
  %(
    <div class='form_element #{css_class}'>
      <div class='label'>#{label}</div>
      <div class='value'>
        #{field}
        #{@template.hidden_field_tag('cms_page[blocks_attributes][][label]', tag.label, :id => nil)}
      </div>
    </div>
  ).html_safe
end

#field_date_time(tag) ⇒ Object



84
85
86
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 84

def field_date_time(tag)
  default_tag_field(tag, :content_field_method => :datetime_field_tag)
end

#field_integer(tag) ⇒ Object



88
89
90
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 88

def field_integer(tag)
  default_tag_field(tag, :content_field_method => :number_field_tag)
end

#field_string(tag) ⇒ Object



92
93
94
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 92

def field_string(tag)
  default_tag_field(tag)
end

#field_text(tag) ⇒ Object



96
97
98
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 96

def field_text(tag)
  default_tag_field(tag, :content_field_method => :text_area_tag)
end

#label_for(field, options) ⇒ Object



45
46
47
48
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 45

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

#page_date_time(tag) ⇒ Object



100
101
102
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 100

def page_date_time(tag)
  default_tag_field(tag, :content_field_method => :datetime_field_tag)
end

#page_integer(tag) ⇒ Object



104
105
106
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 104

def page_integer(tag)
  default_tag_field(tag, :content_field_method => :number_field_tag)
end

#page_rich_text(tag) ⇒ Object



116
117
118
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 116

def page_rich_text(tag)
  default_tag_field(tag, :content_field_method => :text_area_tag)
end

#page_string(tag) ⇒ Object



108
109
110
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 108

def page_string(tag)
  default_tag_field(tag)
end

#page_text(tag) ⇒ Object



112
113
114
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 112

def page_text(tag)
  default_tag_field(tag, :content_field_method => :text_area_tag)
end

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



35
36
37
38
39
40
41
42
43
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 35

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

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



50
51
52
53
54
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 50

def submit(value, options = {}, &block)
  return super if options.delete(:disable_builder)
  extra_content = @template.capture(&block) if block_given?
  simple_field(nil, "#{super(value, options)} #{extra_content}", :class => 'submit_element')
end