forked from kengos/comfortable-mexican-sofa
/
form_builder.rb
148 lines (124 loc) · 4.82 KB
/
form_builder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder
helpers = field_helpers -
%w(hidden_field fields_for) +
%w(select)
helpers.each do |name|
class_eval %Q^
def #{name}(field, *args)
options = args.extract_options!
args << options
return super if options.delete(:disable_builder)
default_field('#{name}', field, options){ super }
end
^
end
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
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
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
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
# -- Tag Field Fields -----------------------------------------------------
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
def field_date_time(tag, index)
default_tag_field(tag, index)
end
def field_integer(tag, index)
default_tag_field(tag, index, :method => :number_field_tag)
end
def field_string(tag, index)
default_tag_field(tag, index)
end
def field_text(tag, index)
default_tag_field(tag, index, :method => :text_area_tag)
end
def page_date_time(tag, index)
default_tag_field(tag, index)
end
def page_integer(tag, index)
default_tag_field(tag, index, :method => :number_field_tag)
end
def page_string(tag, index)
default_tag_field(tag, index)
end
def page_text(tag, index)
default_tag_field(tag, index, :method => :text_area_tag)
end
def page_rich_text(tag, index)
default_tag_field(tag, index, :method => :text_area_tag)
end
def page_file(tag, index)
default_tag_field(tag, index, :method => :file_field_tag)
end
def page_files(tag, index)
default_tag_field(tag, index, :method => :file_field_tag, :multiple => true)
end
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
end