forked from publify/publify
/
sidebar.rb
269 lines (220 loc) · 5.59 KB
/
sidebar.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
class Sidebar < ActiveRecord::Base
serialize :config
class Field
attr_accessor :key
attr_accessor :options
attr_accessor :default
include ApplicationHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
def initialize(key, default, options = { })
@key, @default, @options = key.to_s, default, options
end
def label
options[:label] || key.humanize.gsub(/url/i, 'URL')
end
def label_html(sidebar)
content_tag('label', label)
end
def input_html(sidebar)
text_field_tag(input_name(sidebar), sidebar.config[key], options)
end
def line_html(sidebar)
label_html(sidebar) + "<br />" + input_html(sidebar) + "<br />"
end
def input_name(sidebar)
"configure[#{sidebar.id}][#{key}]"
end
def canonicalize(value)
value
end
class SelectField < self
def input_html(sidebar)
select_tag(input_name(sidebar),
options_for_select(options[:choices], sidebar.config[key]),
options)
end
end
class TextAreaField < self
def input_html(sidebar)
html_options = { "rows" => "10", "cols" => "30", "style" => "width:255px"}.update(options.stringify_keys)
text_area_tag(input_name(sidebar), h(sidebar.config[key]), html_options)
end
end
class RadioField < self
def input_html(sidebar)
options[:choices].collect do |choice|
value = value_for(choice)
radio_button_tag(input_name(sidebar), value,
value == sidebar.config[key], options) +
content_tag('label', label_for(choice))
end.join("<br />")
end
def label_for(choice)
choice.is_a?(Array) ? choice.last : choice.to_s.humanize
end
def value_for(choice)
choice.is_a?(Array) ? choice.first : choice
end
end
class CheckBoxField < self
def input_html(sidebar)
hidden_field_tag(input_name(sidebar),0)+
check_box_tag(input_name(sidebar), 1, sidebar.config[key], options)
end
def line_html(sidebar)
input_html(sidebar) + ' ' + label_html(sidebar) + '<br >'
end
def canonicalize(value)
case value
when "0"
false
else
true
end
end
end
def self.build(key, default, options)
field = class_for(options).new(key, default, options)
end
def self.class_for(options)
case options[:input_type]
when :text_area
TextAreaField
when :textarea
TextAreaField
when :radio
RadioField
when :checkbox
CheckBoxField
when :select
SelectField
else
if options[:choices]
SelectField
else
self
end
end
end
end
class << self
attr_accessor :view_root
def find_all_visible
find :all, :conditions => 'active_position is not null', :order => 'active_position'
end
def find_all_staged
find :all, :conditions => 'staged_position is not null', :order => 'staged_position'
end
def purge
delete_all('active_position is null and staged_position is null')
end
def setting(key, default=nil, options = { })
return if instance_methods.include?(key.to_s)
fields << Field.build(key.to_s, default, options)
fieldmap.update(key.to_s => fields.last)
self.send(:define_method, key) do
self.config[key.to_s]
end
self.send(:define_method, "#{key}=") do |newval|
self.config[key.to_s] = newval
end
end
def fieldmap
@fieldmap ||= {}
end
def fields
@fields ||= []
end
def fields=(newval)
@fields = newval
end
def description(desc = nil)
if desc
@description = desc
else
@description
end
end
def lifetime(timeout = nil)
if timeout
@lifetime = timeout
else
@lifetime
end
end
def short_name
self.to_s.underscore.split(%r{_}).first
end
def display_name(new_dn = nil)
@display_name = new_dn if new_dn
@display_name || short_name.humanize
end
def available_sidebars
Sidebar.subclasses.sort_by { |klass| klass.to_s }
end
end
def blog
Blog.default
end
def initialize(*args)
if block_given?
super(*args) { |instance| yield instance }
else
super(*args)
end
self.class.fields.each do |field|
unless config.has_key?(field.key)
config[field.key] = field.default
end
end
end
def publish
self.active_position=self.staged_position
end
def config
self[:config] ||= { }
end
def sidebar_controller
@sidebar_controller ||= SidebarController.available_sidebars.find { |s| s.short_name == self.controller }
end
def html_id
short_name + '-' + id.to_s
end
def parse_request(contents, params)
end
def fields
self.class.fields
end
def fieldmap(field = nil)
if field
self.class.fieldmap[field.to_s]
else
self.class.fieldmap
end
end
def description
self.class.description
end
def short_name
self.class.short_name
end
def display_name
self.class.display_name
end
def content_partial
"/sidebars/#{short_name}/content"
end
def to_locals_hash
fields.inject({ :sidebar => self }) do |hash, field|
hash.merge(field.key => config[field.key])
end
end
def lifetime
self.class.lifetime
end
def view_root
self.class.view_root
end
end