OSDN Git Service

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