OSDN Git Service

work
[pettanr/pettanr.git] / lib / manifest / filer.rb
1
2 module Pettanr
3   class FilerManager
4     class Symbol 
5       def initialize symbol_conf
6         @symbol_conf = symbol_conf || {}
7         @link_conf = @symbol_conf['link'] || {'type' => 'action', 'name' => 'show'}
8         @type_conf = @symbol_conf['type'] || 'image'
9         @link_method_name = self.link_method_name
10         @tag_method_name = self.tag_method_name
11       end
12       
13       def link_method_name
14         case @link_conf['type']
15         when 'action'
16           case @link_conf['name']
17           when 'show'
18             :link_action_show
19           else
20             :link_action_else
21           end
22         when 'none'
23           :link_none
24         when 'url_column'
25           :link_url_column
26         else
27           :link_else
28         end
29       end
30       
31       def tag_method_name
32         tag = case @type_conf
33         when 'image'
34           :tag_image
35         when 'picture'
36           :tag_picture
37         when 'template'
38           :tag_template
39         else
40           :tag_else
41         end
42       end
43       
44       def link_action_show view, item, operators
45         view.polymorphic_path(item)
46       end
47       
48       def link_action_else view, item, operators
49         view.polymorphic_path(item, :action => @link_conf['name'])
50       end
51       
52       def link_none view, item, operators
53         ''
54       end
55       
56       def link_url_column view, item, operators
57         item.__send__(@link_conf['name']).to_s
58       end
59       
60       def link_else view, item, operators
61         ''
62       end
63       
64       def tag_image view, item, operators
65         view.tag(:img, :src => @symbol_conf['name'], :width => 64, :height => 64)
66       end
67       
68       def tag_picture view, item, operators
69         view.tag(:img, item.__send__(@symbol_conf['name']))
70       end
71       
72       def tag_template view, item, operators
73         view.render item.path_name + '/' + @symbol_conf['name'], :item => item
74         nil
75       end
76       
77       def tag_else view, item, operators
78         'no.png'
79       end
80       
81       def visible?
82         self.link_method_name != :link_none
83       end
84       
85       def render view, item, operators
86         if @tag_method_name == :tag_template
87           view.render item.path_name + '/' + @symbol_conf['name'], :item => item
88         else
89           link = self.__send__(@link_method_name, view, item, operators)
90           tag = self.__send__(@tag_method_name, view, item, operators)
91           unless tag.blank?
92             view.link_to_unless link.blank?, tag, link
93           end
94         end
95       end
96       
97     end
98     
99     class Caption
100       def initialize caption_conf
101         @caption_conf = caption_conf || {}
102         @link_conf = @caption_conf['link'] || {'type' => 'action', 'name' => 'show'}
103         @type_conf = @caption_conf['type'] || 'column'
104         @link_method_name = self.link_method_name
105         @tag_method_name = self.tag_method_name
106       end
107       
108       def link_method_name
109         case @link_conf['type']
110         when 'action'
111           case @link_conf['name']
112           when 'show'
113             :link_action_show
114           else
115             :link_action_else
116           end
117         when 'none'
118           :link_none
119         when 'url_column'
120           :link_url_column
121         else
122           :link_else
123         end
124       end
125       
126       def tag_method_name
127         tag = case @type_conf
128         when 'column'
129           :tag_column
130         when 'method'
131           :tag_method
132         when 'none'
133           :tag_none
134         else
135           :tag_else
136         end
137       end
138       
139       def link_action_show view, item, operators
140         view.polymorphic_path(item)
141       end
142       
143       def link_action_else view, item, operators
144         view.polymorphic_path(item, :action => @link_conf['name'])
145       end
146       
147       def link_none view, item, operators
148         ''
149       end
150       
151       def link_url_column view, item, operators
152         item.__send__(@link_conf['name']).to_s
153       end
154       
155       def link_else view, item, operators
156         ''
157       end
158       
159       def tag_column view, item, operators
160         item.attributes[@caption_conf['name']]
161       end
162       
163       def tag_method view, item, operators
164         item.__send__(@caption_conf['name']).to_s
165       end
166       
167       def tag_else view, item, operators
168         'no caption'
169       end
170       
171       def visible?
172         @tag_method_name != :tag_none
173       end
174       
175       def render view, item, operators
176         link = self.__send__(@link_method_name, view, item, operators)
177         tag = self.__send__(@tag_method_name, view, item, operators)
178         tag = 'no caption' if tag.blank? 
179         unless tag.blank?
180           view.link_to_unless link.blank?, tag, link
181         end
182       end
183       
184     end
185     
186     class Summary
187       def initialize summary_conf
188         @summary_conf = summary_conf || {}
189         @type_conf = @summary_conf['type'] || 'none'
190         @type_method = self.type_method
191       end
192       
193       def type_method
194         case @type_conf
195         when 'none'
196           :type_none
197         when 'template'
198           :type_template
199         else
200           :type_none
201         end
202       end
203       
204       def type_none view, item, operators
205         ''
206       end
207       
208       def type_template view, item, operators
209         view.render item.path_name + '/' + @summary_conf['name'], :item => item
210       end
211       
212       def visible?
213         self.type_method != :type_none
214       end
215       
216       def render view, item, operators
217         self.__send__(@type_method, view, item, operators)
218       end
219       
220     end
221     
222     class Icon
223       def initialize icon_conf
224         @icon_conf = icon_conf || {}
225       end
226       
227       def visible?
228         true
229       end
230       
231       def render view, item, operators
232         view.link_to view.icon_tag(item.class.to_s, :object => item, :size => 64), 
233           view.polymorphic_path(item, :format => :prof)
234       end
235       
236     end
237     
238     class Date
239       def initialize date_conf
240         @date_conf = date_conf || {}
241       end
242       
243       def visible?
244         true
245       end
246       
247       def render view, item, operators
248         view.distance_of_time_in_words_to_now item.updated_at
249       end
250       
251     end
252     
253     class Edit
254       def initialize edit_conf
255         @edit_conf = edit_conf || {}
256         @type_conf = @edit_conf['type'] || 'default'
257         @type_method = self.type_method
258       end
259       
260       def type_method
261         case @type_conf
262         when 'default'
263           :type_default
264         when 'account'
265           :type_account
266         when 'template'
267           :type_template
268         when 'none'
269           :type_none
270         else
271           :type_none
272         end
273       end
274       
275       def type_default view, item, operators
276         if item.own? operators
277           view.link_to view.tag(:img, :src => view.asset_path('edit.png'), :width => 32, :height => 32), 
278             view.polymorphic_path(item, :action => :edit)
279           view.link_to view.tag(:img, :src => view.asset_path('remove.png'), :width => 32, :height => 32), 
280             item, confirm: 'Are you sure?', :method => :delete
281         end
282       end
283       
284       def type_account view, item, operators
285         if item.own? operators
286           view.link_to view.tag(:img, :src => view.asset_path('edit.png'), :width => 32, :height => 32), 
287             '/home/configure'
288         end
289       end
290       
291       def type_template view, item, operators
292         view.render item.path_name + '/' + @edit_conf['name'], :item => item
293       end
294       
295       def type_none view, item, operators
296         ''
297       end
298       
299       def visible?
300         self.type_method != :type_none
301       end
302       
303       def render view, item, operators
304         self.__send__(@type_method, view, item, operators)
305       end
306       
307     end
308     
309     class Filer
310       class FileItem
311         attr :item, :operators
312         def initialize item, operators, symbol, caption, summary, icon, date, edit, manifest
313           @item = item
314           @manifest = manifest
315           @operators = operators
316           @symbol = symbol
317           @caption = caption
318           @summary = summary
319           @icon = icon
320           @date = date
321           @edit = edit
322         end
323         
324         def symbol view
325           @symbol.render view, @item, @operators
326         end
327         
328         def caption view
329           @caption.render view, @item, @operators
330         end
331         
332         def summary view
333           @summary.render view, @item, @operators
334         end
335         
336         def icon view
337           @icon.render view, @item, @operators
338         end
339         
340         def date view
341           @date.render view, @item, @operators
342         end
343         
344         def edit view
345           @edit.render view, @item, @operators
346         end
347       
348       end
349       
350       attr :items, :file_items, :operators, :paginate, :symbol, :caption, :summary, :icon, :date, :edit
351       def initialize item_name, items, operators, paginate, symbol, caption, summary, icon, date, edit, manifest
352         @item_name = item_name
353         @items = items
354         @operators = operators
355         @paginate = paginate
356         @symbol = symbol
357         @caption = caption
358         @summary = summary
359         @icon = icon
360         @date = date
361         @edit = edit
362         @manifest = manifest
363         begin
364           @file_items = @items.map {|item| 
365             FileItem.new item, operators, symbol, caption, summary, icon, date, edit, manifest
366           }
367         rescue
368           raise "List define err for #{@item_name} in #{@items}\n"
369           
370         end
371       end
372         
373       def model_name
374         @item_name
375       end
376       
377       def model
378         model_name.classify.constantize
379       end
380       
381     end
382     
383     attr :filer_manifest, :item_name, :manifest, :filer_conf, 
384       :symbol, :caption, :summary, :icon, :date, :edit
385     def initialize filer_manifest
386       @filer_manifest = filer_manifest
387       @item_name = @filer_manifest.item_name
388       @manifest = @filer_manifest.manifest
389       @filer_conf = @filer_manifest.conf
390       @symbol = Symbol.new @filer_manifest.symbol
391       @caption = Caption.new @filer_manifest.caption
392       @summary = Summary.new @filer_manifest.summary
393       @icon = Icon.new @filer_manifest.icon
394       @date = Date.new @filer_manifest.date
395       @edit = Edit.new @filer_manifest.edit
396     end
397     
398     def open item_name, items, operators, paginate
399       Filer.new item_name, items, operators, paginate, @symbol, @caption, @summary, @icon, @date, @edit, @manifest
400     end
401     
402   end
403 end
404