OSDN Git Service

6417fea95f3d79fd67c6472de9ab73b699779669
[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 render view, item, operators
82         link = self.__send__(@link_method_name, view, item, operators)
83         tag = self.__send__(@tag_method_name, view, item, operators)
84         unless tag.blank?
85           link_to_unless link.blank?, tag, link
86         end
87       end
88       
89       
90     end
91     
92     class Caption
93       def initialize caption_conf
94         @caption_conf = caption_conf || {}
95         @link_conf = @caption_conf['link'] || {'type' => 'action', 'name' => 'show'}
96         @type_conf = @caption_conf['type'] || 'column'
97         @link_method_name = self.link_method_name
98         @tag_method_name = self.tag_method_name
99       end
100       
101       def link_method_name
102         case @link_conf['type']
103         when 'action'
104           case @link_conf['name']
105           when 'show'
106             :link_action_show
107           else
108             :link_action_else
109           end
110         when 'none'
111           :link_none
112         when 'url_column'
113           :link_url_column
114         else
115           :link_else
116         end
117       end
118       
119       def tag_method_name
120         tag = case @type_conf
121         when 'column'
122           :tag_column
123         when 'method'
124           :tag_method
125         else
126           :tag_else
127         end
128       end
129       
130       def link_action_show view, item, operators
131         view.polymorphic_path(item)
132       end
133       
134       def link_action_else view, item, operators
135         view.polymorphic_path(item, :action => @link_conf['name'])
136       end
137       
138       def link_none view, item, operators
139         ''
140       end
141       
142       def link_url_column view, item, operators
143         item.__send__(@link_conf['name']).to_s
144       end
145       
146       def link_else view, item, operators
147         ''
148       end
149       
150       def tag_column view, item, operators
151         item.attributes[caption['name']]
152       end
153       
154       def tag_method view, item, operators
155         item.__send__(caption['name']).to_s
156       end
157       
158       def tag_else view, item, operators
159         'no caption'
160       end
161       
162       def render view, item, operators
163         unless caption_type == 'none'
164           link = self.__send__(@link_method_name, view, item, operators)
165           tag = self.__send__(@tag_method_name, view, item, operators)
166           tag = 'no caption' if tag.blank? 
167           unless tag.blank?
168             link_to_unless link.blank?, tag, link
169           end
170         end
171       end
172       
173     end
174     
175     class Summary
176       def initialize summary_conf
177         @summary_conf = summary_conf || {}
178         @type_conf = @summary_conf['type'] || 'none'
179         @type_method = self.type_method
180       end
181       
182       def type_method
183         case @type_conf
184         when 'none'
185           :type_none
186         when 'template'
187           :type_template
188         else
189           :type_none
190         end
191       end
192       
193       def type_none view, item, operators
194         ''
195       end
196       
197       def type_template view, item, operators
198         view.render item.path_name + '/' + @summary_conf['name'], :item => item
199       end
200       
201       def render view, item, operators
202         self.__send__(@type_method, view, item)
203       end
204       
205     end
206     
207     class Icon
208       def initialize icon_conf
209         @icon_conf = icon_conf || {}
210       end
211       
212       def render view, item, operators
213         view.link_to view.icon_tag(item.class.to_s, :object => item, :size => 64), 
214           view.polymorphic_path(item, :format => :prof)
215       end
216       
217     end
218     
219     class Date
220       def initialize date_conf
221         @date_conf = date_conf || {}
222       end
223       
224       def render view, item, operators
225         view.distance_of_time_in_words_to_now item.updated_at
226       end
227       
228     end
229     
230     class Edit
231       def initialize edit_conf
232         @edit_conf = edit_conf || {}
233         @type_conf = @edit_conf['type'] || 'default'
234         @type_method = self.type_method
235       end
236       
237       def type_method
238         case @type_conf
239         when 'default'
240           :type_default
241         when 'account'
242           :type_account
243         when 'template'
244           :type_template
245         when 'none'
246           :type_none
247         else
248           :type_none
249         end
250       end
251       
252       def type_default view, item, operators
253         if item.own? operators
254           view.link_to tag(:img, :src => view.asset_path('edit.png'), :width => 32, :height => 32), 
255             view.polymorphic_path(item, :action => :edit)
256           view.link_to tag(:img, :src => view.asset_path('remove.png'), :width => 32, :height => 32), 
257             item, confirm: 'Are you sure?', :method => :delete
258         end
259       end
260       
261       def type_account view, item, operators
262         if item.own? operators
263           view.link_to tag(:img, :src => view.asset_path('edit.png'), :width => 32, :height => 32), 
264             '/home/configure'
265         end
266       end
267       
268       def type_template view, item, operators
269         view.render item.path_name + '/' + @edit_conf['name'], :item => item
270       end
271       
272       def type_none view, item, operators
273         ''
274       end
275       
276       def render view, item, operators
277         self.__send__(@type_method, view, item, operators)
278       end
279       
280     end
281     
282     class Filer
283       class FileItem
284         attr :item, :operators
285         def initialize item, operators, symbol, caption, summary, icon, date, edit, manifest
286           @item = item
287           @manifest = manifest
288           @operators = operators
289           @symbol = symbol
290           @caption = caption
291           @summary = summary
292           @icon = icon
293           @date = date
294           @edit = edit
295         end
296         
297         def symbol view
298           @symbol.render view, @item, @operators
299         end
300         
301         def caption view
302           @caption.render view, @item, @operators
303         end
304         
305         def summary view
306           @summary.render view, @item, @operators
307         end
308         
309         def icon view
310           @icon.render view, @item, @operators
311         end
312         
313         def date view
314           @date.render view, @item, @operators
315         end
316         
317         def edit view
318           @edit.render view, @item, @operators
319         end
320       
321       end
322       
323       attr :items, :file_items, :operators
324       def initialize items, operators, symbol, caption, summary, icon, date, edit, manifest
325         @items = items
326         @manifest = manifest
327         @file_items = @items.map {|item| 
328           FileItem.new item, operators, symbol, caption, summary, icon, date, edit, manifest
329         }
330       end
331     end
332     
333     def initialize filer_manifest
334       @filer_manifest = filer_manifest
335       @item_name = @filer_manifest.item_name
336       @manifest = @filer_manifest.manifest
337       @filer_conf = @filer_manifest.conf
338       @symbol = Symbol.new @filer_manifest.symbol
339       @caption = Caption.new @filer_manifest.caption
340       @summary = Summary.new @filer_manifest.summary
341       @icon = Icon.new @filer_manifest.icon
342       @date = Date.new @filer_manifest.date
343       @edit = Edit.new @filer_manifest.edit
344     end
345     
346     def open items, operators
347       Filer.new items, operators, @symbol, @caption, @summary, @icon, @date, @edit, @manifest
348     end
349     
350   end
351 end
352