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

#collection(tag, index) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 133

def collection(tag, index)
  options = [["---- Select #{tag.collection_class.titleize} ----", nil]] + 
    tag.collection_objects.collect do |m| 
      [m.send(tag.collection_title), m.send(tag.collection_identifier)]
    end
    
  content = @template.select_tag(
    "page[blocks_attributes][#{index}][content]",
    @template.options_for_select(options, :selected => tag.content),
    :id => nil
  )
  content << @template.hidden_field_tag("page[blocks_attributes][#{index}][identifier]", tag.identifier, :id => nil)
  simple_field(tag.identifier.titleize, content, :class => tag.class.to_s.demodulize.underscore )
end

#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.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

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

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



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
83
84
85
86
87
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 58

def default_tag_field(tag, index, options = {})
  method    = options.delete(:method) || :text_field_tag
  label     = tag.page.class.human_attribute_name(tag.identifier.to_s)
  css_class = tag.class.to_s.demodulize.underscore
  content   = ''
  
  input_class = case tag
  when ComfortableMexicanSofa::Tag::PageDateTime, ComfortableMexicanSofa::Tag::FieldDateTime
    'datetime'
  when ComfortableMexicanSofa::Tag::PageText, ComfortableMexicanSofa::Tag::FieldText
    'code'
  when ComfortableMexicanSofa::Tag::PageRichText
    'rich_text'
  end
  
  case method
  when :file_field_tag
    input_params = {:id => nil, :class => input_class}
    input_params.merge!(:multiple => true) if options[:multiple]
    name = "page[blocks_attributes][#{index}][content]"
    name << '[]' if options[:multiple]
    content << @template.send(method, name, input_params)
    content << @template.render(:partial => 'cms_admin/files/page_form', :object => tag.block)
  else
    content << @template.send(method, "page[blocks_attributes][#{index}][content]", tag.content, :id => nil, :class => input_class)
  end
  content << @template.hidden_field_tag("page[blocks_attributes][#{index}][identifier]", tag.identifier, :id => nil)
  
  simple_field(label, content, :class => css_class)
end

#field_date_time(tag, index) ⇒ Object



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

def field_date_time(tag, index)
  default_tag_field(tag, index)
end

#field_integer(tag, index) ⇒ Object



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

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

#field_string(tag, index) ⇒ Object



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

def field_string(tag, index)
  default_tag_field(tag, index)
end

#field_text(tag, index) ⇒ Object



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

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

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



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

def label_for(field, options={})
  label = options.delete(:label) || object.class.human_attribute_name(field).capitalize
  for_value = options[:id] || "#{object_name}_#{field}"
  %Q{<label for="#{for_value}">#{label}</label>}.html_safe
end

#page_date_time(tag, index) ⇒ Object



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

def page_date_time(tag, index)
  default_tag_field(tag, index)
end

#page_file(tag, index) ⇒ Object



125
126
127
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 125

def page_file(tag, index)
  default_tag_field(tag, index, :method => :file_field_tag)
end

#page_files(tag, index) ⇒ Object



129
130
131
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 129

def page_files(tag, index)
  default_tag_field(tag, index, :method => :file_field_tag, :multiple => true)
end

#page_integer(tag, index) ⇒ Object



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

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

#page_rich_text(tag, index) ⇒ Object



121
122
123
# File 'lib/comfortable_mexican_sofa/form_builder.rb', line 121

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

#page_string(tag, index) ⇒ Object



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

def page_string(tag, index)
  default_tag_field(tag, index)
end

#page_text(tag, index) ⇒ Object



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

def page_text(tag, index)
  default_tag_field(tag, index, :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 simple_field #{options.delete(:class)}'>
      <div class='label'>#{label}</div>
      <div class='value'>#{content}</div>
    </div>
  ).html_safe
end

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



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

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