clot_engine 1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ $:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
2
+
3
+ Autotest.add_discovery do
4
+ "rspec"
5
+ end
@@ -0,0 +1,22 @@
1
+ module Clot
2
+ module ActiveRecord #:nodoc:
3
+ module Droppable
4
+
5
+ def get_drop_class(class_obj)
6
+ begin
7
+ drop_string = class_obj.to_s + "Drop"
8
+ drop_class = drop_string.constantize
9
+ drop_class
10
+ rescue
11
+ get_drop_class class_obj.superclass
12
+ end
13
+ end
14
+
15
+ def to_liquid
16
+ drop_class = get_drop_class self.class
17
+ drop_class.new self
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,116 @@
1
+ module Clot
2
+ module DropAssociation
3
+ def belongs_to(*args)
4
+ args.each do |sym|
5
+ belongs_to = %{
6
+ def #{sym}
7
+ @source.#{sym}
8
+ end
9
+ def #{sym}_id
10
+ @source.#{sym}_id
11
+ end
12
+ }
13
+ class_eval belongs_to
14
+ end
15
+ end
16
+
17
+ def has_many(*args)
18
+ args.each do |sym|
19
+ has_many = %{
20
+ def #{sym}
21
+ @source.#{sym}
22
+ end
23
+
24
+ def #{sym.to_s.singularize}_ids
25
+ @source.#{sym.to_s.singularize}_ids
26
+ end
27
+ }
28
+ class_eval has_many
29
+ end
30
+
31
+ end
32
+ end
33
+
34
+ class BaseDrop < Liquid::Drop
35
+ extend DropAssociation
36
+
37
+ def self.included
38
+ include Rails.application.routes.url_helpers
39
+ end
40
+
41
+ class_attribute :liquid_attributes
42
+ self.liquid_attributes = [:created_at, :updated_at]
43
+ attr_reader :source
44
+ delegate :hash, :to => :source
45
+
46
+ def initialize(source)
47
+ @source = source
48
+ @liquid = self.liquid_attributes.inject({}) { |h, k| h.update k.to_s => @source.send(k) }
49
+ end
50
+
51
+ def before_method(method)
52
+ @liquid[method.to_s]
53
+ end
54
+
55
+ def eql?(comparison_object)
56
+ self == (comparison_object)
57
+ end
58
+
59
+ def ==(comparison_object)
60
+ self.source == (comparison_object.is_a?(self.class) ? comparison_object.source : comparison_object)
61
+ end
62
+
63
+ # converts an array of records to an array of liquid drops, and assigns the given context to each of them
64
+ def self.liquify(current_context, *records, &block)
65
+ i = -1
66
+ records =
67
+ records.inject [] do |all, r|
68
+ i+=1
69
+ attrs = (block && block.arity == 1) ? [r] : [r, i]
70
+ all << (block ? block.call(*attrs) : r.to_liquid)
71
+ all.last.context = current_context if all.last.is_a?(Liquid::Drop)
72
+ all
73
+ end
74
+ records.compact!
75
+ records
76
+ end
77
+
78
+
79
+ def id
80
+ @source.id
81
+ end
82
+
83
+ def dropped_class
84
+ @source.class
85
+ end
86
+
87
+ def errors
88
+ @source.errors
89
+ end
90
+
91
+ def collection_label
92
+ "label field"
93
+ end
94
+
95
+ def self.method_missing(symbol, *args)
96
+ [symbol]
97
+ end
98
+
99
+
100
+
101
+ protected
102
+
103
+ def url_for(options)
104
+ controller.url_for options
105
+ end
106
+
107
+ def controller
108
+ @context.registers[:controller]
109
+ end
110
+
111
+ def liquify(*records, &block)
112
+ self.class.liquify(@context, *records, &block)
113
+ end
114
+
115
+ end
116
+ end
@@ -0,0 +1,342 @@
1
+ require 'clot/date_tags'
2
+ require 'clot/no_model_form_tags'
3
+
4
+ module Clot
5
+
6
+ class NumberedTag < ClotTag
7
+
8
+ def value_string(val)
9
+ if val < 10
10
+ "0#{val}"
11
+ else
12
+ val
13
+ end
14
+ end
15
+
16
+ def can_show(val)
17
+ true
18
+ end
19
+
20
+ def get_options(from_val,to_val, selected_value = nil)
21
+ options = ""
22
+
23
+ if from_val < to_val
24
+ range = (from_val..to_val)
25
+ else
26
+ range = (to_val..from_val).to_a.reverse
27
+ end
28
+
29
+ range.each do |val|
30
+ if can_show(val)
31
+ if selected_value == val
32
+ options << %{<option selected="selected" value="#{val}">#{value_string(val)}</option>}
33
+ else
34
+ options << %{<option value="#{val}">#{value_string(val)}</option>}
35
+ end
36
+ end
37
+ end
38
+ options
39
+ end
40
+
41
+ def set_primary_attributes(context)
42
+ @value_string = resolve_value(@params.shift,context)
43
+ if @value_string.is_a? Time
44
+ @value_string = @value_string.send(time_method)
45
+ end
46
+ end
47
+
48
+ def personal_attributes(name,value)
49
+ case name
50
+ when "id_string_val" then
51
+ @id_string = %{id="#{value}"}
52
+ when "name_string_val" then
53
+ @name_string = %{name="#{value}"}
54
+ when "field_name" then
55
+ @field_name = value
56
+ when "prefix" then
57
+ @prefix = value
58
+ when "prompt" then
59
+ prompt_text = (value === true) ? default_field_name.pluralize.capitalize : value
60
+ @prompt_val = "<option value=\"\">#{prompt_text}</option>"
61
+ end
62
+ end
63
+
64
+ def default_start
65
+ 0
66
+ end
67
+
68
+ def default_end
69
+ 59
70
+ end
71
+
72
+ def id_string(field_name)
73
+ @id_string || if field_name && ! field_name.blank?
74
+ %{id="#{@prefix || 'date'}_#{field_name}"}
75
+ else
76
+ %{id="#{@prefix || 'date'}"}
77
+ end
78
+ end
79
+
80
+ def name_string(field_name)
81
+ @name_string || if field_name && ! field_name.blank?
82
+ %{name="#{@prefix || 'date'}[#{field_name}]"}
83
+ else
84
+ %{name="#{@prefix || 'date'}"}
85
+ end
86
+ end
87
+
88
+ def render_string
89
+ field_name = @field_name || default_field_name
90
+ %{<select #{id_string(field_name)} #{name_string(field_name)}>#{@prompt_val}} + get_options(default_start, default_end, @value_string) + "</select>"
91
+ end
92
+
93
+ def time_method
94
+ default_field_name
95
+ end
96
+ end
97
+
98
+ class SelectMinute < NumberedTag
99
+ def time_method
100
+ :min
101
+ end
102
+ def default_field_name
103
+ "minute"
104
+ end
105
+
106
+ def can_show(val)
107
+ @minute_step.nil? || (val % @minute_step) == 0
108
+ end
109
+
110
+ def personal_attributes(name,value)
111
+ case name
112
+ when "minute_step" then
113
+ @minute_step = value
114
+ end || super(name, value)
115
+ end
116
+
117
+ end
118
+
119
+ class SelectHour < NumberedTag
120
+ def default_field_name
121
+ "hour"
122
+ end
123
+ end
124
+
125
+ class SelectDay < NumberedTag
126
+ def default_field_name
127
+ "day"
128
+ end
129
+
130
+ def default_start
131
+ 1
132
+ end
133
+
134
+ def default_end
135
+ 31
136
+ end
137
+ end
138
+
139
+ class SelectMonth < NumberedTag
140
+ def default_field_name
141
+ "month"
142
+ end
143
+
144
+ def default_start
145
+ 1
146
+ end
147
+
148
+ def default_end
149
+ 12
150
+ end
151
+
152
+ def personal_attributes(name,value)
153
+ case name
154
+ when "use_month_numbers" then
155
+ @use_month_numbers = value
156
+ when "add_month_numbers" then
157
+ @add_month_numbers = value
158
+ when "use_short_month" then
159
+ @use_short_month = value
160
+ when "use_month_names" then
161
+ @use_month_names = value
162
+ end || super(name, value)
163
+ end
164
+
165
+ def value_string(val)
166
+ if @use_month_numbers
167
+ super(val)
168
+ else
169
+ if @use_month_names
170
+ month_name = @use_month_names[val]
171
+ else
172
+ months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
173
+ month_name = months[val - 1]
174
+ end
175
+ if @add_month_numbers
176
+ "#{val} - #{month_name}"
177
+ elsif @use_short_month
178
+ month_name[0..2]
179
+ else
180
+ month_name
181
+ end
182
+ end
183
+ end
184
+
185
+ end
186
+
187
+
188
+
189
+ class SelectYear < NumberedTag
190
+ def default_field_name
191
+ "year"
192
+ end
193
+
194
+ def default_start
195
+ @start_year || @value_string - 5
196
+ end
197
+
198
+ def default_end
199
+ @end_year || @value_string + 5
200
+ end
201
+
202
+ def personal_attributes(name,value)
203
+ case name
204
+ when "start_year" then
205
+ @start_year = value
206
+ when "end_year" then
207
+ @end_year = value
208
+ end || super(name, value)
209
+ end
210
+
211
+
212
+ end
213
+
214
+
215
+ class SelectSecond < NumberedTag
216
+ def time_method
217
+ :sec
218
+ end
219
+ def default_field_name
220
+ "second"
221
+ end
222
+ end
223
+
224
+ class MultiDateTag < ClotTag
225
+
226
+ def set_primary_attributes(context)
227
+ @time = resolve_value(@params.shift,context) || Time.zone.now
228
+ end
229
+
230
+ def personal_attributes(name,value)
231
+ case name
232
+ when "discard_day" then
233
+ @discard_day = true
234
+ when "include_blank" then
235
+ @include_blank = true
236
+ when "minute_step" then
237
+ @minute_step = "minute_step:#{value},"
238
+ when "order" then
239
+ @order = value
240
+ when "start_year" then
241
+ @start_year = "start_year:#{value},"
242
+ when "prefix" then
243
+ @prefix = ",prefix:'#{value}'"
244
+ when "discard_type" then
245
+ @discard_type = ",field_name:''"
246
+ when "datetime_separator" then
247
+ @datetime_separator = value
248
+ when "date_separator" then
249
+ @date_separator = value
250
+ when "time_separator" then
251
+ @time_separator = value
252
+ when "include_seconds" then
253
+ @include_seconds = true
254
+ when "use_month_numbers" then
255
+ @use_month_numbers = "use_month_numbers:true,"
256
+ when /(.*)_prompt/ then
257
+ value_string = value === true ? 'true' : "'#{value}'"
258
+ instance_variable_set("@#{$1}_prompt".to_sym,",prompt:#{value_string}")
259
+ when "prompt" then
260
+ @prompt = ",prompt:true"
261
+ end || super(name,value)
262
+ end
263
+
264
+ def render(context)
265
+ instance_variables.map(&:to_sym).each do |var|
266
+ unless [:@_params, :@markup, :@tag_name].include? var
267
+ instance_variable_set var, nil #this is because the same parse tag is re-rendered
268
+ end
269
+ end
270
+ @params = @_params.clone
271
+ set_attributes(context)
272
+ render_nested(context)
273
+ end
274
+
275
+ def set_unit(unit)
276
+ prompt = instance_variable_get("@#{unit}_prompt".to_sym)
277
+ line = "#{@time.send(time_unit(unit).to_sym).to_s} #{@discard_type}#{@prefix}#{prompt || @prompt}"
278
+ instance_variable_set "@#{unit}",
279
+ "Clot::Select#{unit.capitalize}".constantize.new(".select_#{unit}", line,[])
280
+ end
281
+
282
+ def time_unit(unit)
283
+ case unit
284
+ when "second" then "sec"
285
+ when "minute" then "min"
286
+ else unit
287
+ end
288
+ end
289
+
290
+ def render_units(units, context, separator = nil)
291
+ data = ""
292
+ not_first = false
293
+ units.each do |unit|
294
+ set_unit unit
295
+ if not_first && separator
296
+ data << separator
297
+ end
298
+
299
+ val = instance_variable_get("@#{unit}".to_sym)
300
+ data << val.render(context)
301
+ not_first = true
302
+ end
303
+ data
304
+ end
305
+
306
+
307
+ end
308
+
309
+ class SelectDate < MultiDateTag
310
+ def render_nested(context)
311
+ order = @order || ['year', 'month', 'day']
312
+ render_units(order, context, @date_separator)
313
+ end
314
+ end
315
+
316
+ class SelectTime < MultiDateTag
317
+ def render_nested(context)
318
+ units = ["hour", "minute"]
319
+ if @include_seconds
320
+ units << "second"
321
+ end
322
+ render_units(units, context, @time_separator)
323
+ end
324
+ end
325
+
326
+ class SelectDatetime < MultiDateTag
327
+ def render_nested(context)
328
+ time_units = ["hour", "minute"]
329
+ if @include_seconds
330
+ time_units << "second"
331
+ end
332
+ time_result = render_units(time_units, context, @time_separator)
333
+
334
+ order = @order || ['year', 'month', 'day']
335
+ date_result = render_units(order, context, @date_separator)
336
+
337
+ date_result + @datetime_separator.to_s + time_result
338
+ end
339
+
340
+ end
341
+
342
+ end