OSDN Git Service

add frm view
[pettanr/pettanr.git] / lib / manifest / local / view / filer / caption.rb
1     class Caption
2       def initialize caption_conf
3         @caption_conf = caption_conf || {}
4         @link_conf = @caption_conf['link'] || {'type' => 'action', 'name' => 'show'}
5         @type_conf = @caption_conf['type'] || 'column'
6         @link_method_name = self.link_method_name
7         @tag_method_name = self.tag_method_name
8       end
9       
10       def link_method_name
11         case @link_conf['type']
12         when 'action'
13           case @link_conf['name']
14           when 'show'
15             :link_action_show
16           else
17             :link_action_else
18           end
19         when 'none'
20           :link_none
21         when 'url_column'
22           :link_url_column
23         else
24           :link_else
25         end
26       end
27       
28       def tag_method_name
29         tag = case @type_conf
30         when 'column'
31           :tag_column
32         when 'method'
33           :tag_method
34         when 'none'
35           :tag_none
36         else
37           :tag_else
38         end
39       end
40       
41       def link_action_show view, item, operators
42         view.polymorphic_path(item)
43       end
44       
45       def link_action_else view, item, operators
46         view.polymorphic_path(item, :action => @link_conf['name'])
47       end
48       
49       def link_none view, item, operators
50         ''
51       end
52       
53       def link_url_column view, item, operators
54         item.__send__(@link_conf['name']).to_s
55       end
56       
57       def link_else view, item, operators
58         ''
59       end
60       
61       def tag_column view, item, operators
62         item.attributes[@caption_conf['name']]
63       end
64       
65       def tag_method view, item, operators
66         item.__send__(@caption_conf['name']).to_s
67       end
68       
69       def tag_else view, item, operators
70         'no caption'
71       end
72       
73       def visible?
74         @tag_method_name != :tag_none
75       end
76       
77       def render view, item, operators
78         link = self.__send__(@link_method_name, view, item, operators)
79         tag = self.__send__(@tag_method_name, view, item, operators)
80         tag = 'no caption' if tag.blank? 
81         unless tag.blank?
82           view.link_to_unless link.blank?, tag, link
83         end
84       end
85       
86     end
87