OSDN Git Service

fix has one manifest
[pettanr/pettanr.git] / lib / peta / element.rb
1 module Peta
2   module Element
3     def self.included(base)
4       base.extend(ClassMethods)
5       base.__send__ :include, InstanceMethods
6     end
7     
8     module ClassMethods
9       def colum_structures
10         raise
11       end
12       
13       def list_opt_for_panel
14         {}
15       end
16       
17       def show_opt_for_panel
18         {}
19       end
20       
21       def json_opt_for_panel
22         {}
23       end
24       
25       def path_name with_engine = false
26         self.to_s.tableize
27       end
28       
29     end
30     
31     module InstanceMethods
32       private
33       
34       public
35       
36       def has_picture?
37         false
38       end
39       
40       def has_part?
41         false
42       end
43       
44       def parts
45         @parts ||= []
46       end
47       
48       def has_helper? column_name
49         self.class.colum_structures[column_name] and self.class.colum_structures[column_name][:helper]
50       end
51       
52       def element_name
53         self.class.to_s.underscore
54       end
55       
56       def extend_column
57         nil
58       end
59       
60       def extend_element_name
61         self.extend_column ? self.attributes[extend_column] : self.element_name
62       end
63       
64       def find_configurations hash, key
65          if hash[key].is_a? String
66            self.find_configurations hash, hash[key]
67          else
68            return hash[key]
69          end
70       end
71       
72       def element_index
73         @element_index ||= self.t
74       end
75       
76       def parent
77         @parent ||= self.panel
78       end
79       
80       def parent= content
81         @parent = content
82       end
83       
84       def new_index
85         @new_index
86       end
87       
88       def new_index= v
89         @new_index = v
90       end
91       
92       def new_panel
93         @new_panel
94       end
95       
96       def new_panel= v
97         @new_panel = v
98       end
99       
100       def get_panel
101         self.panel || @new_panel
102       end
103       
104       def tag_id c = nil
105         'panel' + self.tag_panel_id + self.tag_element_type + self.tag_element_id + c.to_s
106       end
107       
108       def field_tag_id f
109         self.tag_id + f.to_s
110       end
111       
112       def tag_panel_id
113         self.get_panel.new_record? ? '0' : self.get_panel.id.to_s
114       end
115       
116       def tag_element_id
117         self.new_record? ? '0' : self.id.to_s
118       end
119       
120       def tag_element_type
121         raise
122       end
123       
124       def tag_new_index
125         self.new_index.to_s
126       end
127       
128       def path_name with_engine = false
129         self.class.path_name(with_engine)
130       end
131       
132       def form_template with_engine = false
133         self.path_name(with_engine) + '/form'
134       end
135       
136       def scenario_template with_engine = false
137         self.path_name(with_engine) + '/scenario'
138       end
139       
140       def element_face_template with_engine = false
141         self.path_name(with_engine) + '/element_face'
142       end
143       
144       def form_helper_template(colum_name)
145         self.class.colum_structures[colum_name][:helper]
146       end
147       
148       def tag_attributes column = nil, opt = {}
149         {
150           :id => self.field_tag_id(column), :panel_id => self.tag_panel_id, 
151           :element_id => self.tag_element_id, :element_type => self.tag_element_type
152         }.merge(opt)
153       end
154       
155       def field_tag_attributes column, no_attr, opt = {}
156         self.tag_attributes(column).merge(
157           {:column => column, :new_index => self.tag_new_index, :no_attr => no_attr}
158         ).merge(opt)
159       end
160       
161       #render element by body
162       def any_tag_attributes name = nil, opt = {}
163         r = self.tag_attributes(name)
164         r.merge!(
165           {:new_index => self.tag_new_index}
166         ) if self.new_index
167         r.merge(opt)
168       end
169       
170       def select_tag_attributes(selected, column, no_attr)
171         [
172           :last, :first, 
173           {:html => {:selected => selected}}, 
174           self.field_tag_attributes(column, no_attr)
175         ]
176       end
177       
178       def tag_attr column = nil, opt = {}
179         self.tag_attributes(column, opt).to_attr
180       end
181       
182       def field_tag_attr column, no_attr, opt = {}
183         self.field_tag_attributes(column, no_attr, opt).to_attr
184       end
185       
186       def any_tag_attr name = nil, opt = {}
187         self.any_tag_attributes(name, opt).to_attr
188       end
189       
190     end
191   end
192 end
193