OSDN Git Service

fix has one manifest
[pettanr/pettanr.git] / lib / peta / item.rb
1 module Peta
2   class Item < ActiveRecord::Base
3     self.abstract_class = true
4     # ClassMethods
5     # class_name
6     # table_name
7     
8     before_validation :valid_encode
9     
10     def self.singular
11       self.to_s
12     end
13     
14     def self.plural
15       self.singular.pluralize
16     end
17     
18     def self.item_name
19       self.singular.underscore
20     end
21     
22     def self.item?
23       true
24     end
25     
26     def self.content?
27       false
28     end
29     
30     def self.path_name with_engine = false
31       self.plural.underscore
32     end
33     
34     def self.valid_encode_columns
35       []
36     end
37     
38     def self.visible_count_options
39       nil
40     end
41     
42     def self.list_where
43       ''
44     end
45     
46     def self.list_order
47       ''
48     end
49     
50     def self.list_opt
51       {}
52     end
53     
54     def self.list_json_opt
55       {}
56     end
57     
58     def self.show item_id, operators
59       opt = {}
60       opt.merge!(self.show_opt)
61       item = self.find(item_id, opt)
62       raise ActiveRecord::Forbidden unless item.visible?(operators)
63       item
64     end
65     
66     def self.show_opt
67       {}
68     end
69     
70     def self.show_json_opt
71       {}
72     end
73     
74     def self.visible_count
75       self.count self.visible_count_options
76     end
77     
78     def self.visible_count_options
79       []
80     end
81     
82     #InstanceMethods
83     
84     def self.fold_extend_settings params
85       speech_balloon_settings = params[:speech_balloon][:settings]
86       if speech_balloon_settings.is_a?(Hash)
87         params[:speech_balloon][:settings] = speech_balloon_settings.to_json
88       end
89       balloon_settings = params[:speech_balloon][:balloon_attributes][:settings]
90       if balloon_settings.is_a?(Hash)
91         params[:speech_balloon][:balloon_attributes][:settings] = balloon_settings.to_json
92       end
93       speech_settings = params[:speech_balloon][:speech_attributes][:settings]
94       if speech_settings.is_a?(Hash)
95         params[:speech_balloon][:speech_attributes][:settings] = speech_settings.to_json
96       end
97     end
98     
99     def engine
100       Object.const_get self.extend_column
101     end
102     
103     def extend_engine_module
104       self.extend self.engine.extend_module
105       self.elements.each do |element|
106         element.extend_item
107       end
108     end
109     
110     def item_name
111       self.class.item_name
112     end
113     
114     def model_name
115       self.item_name
116     end
117     
118     def table_name
119       self.class.table_name
120     end
121     
122     def extend_column_name
123       nil
124     end
125     
126     def form_name
127       self.extend_column ? self.attributes[self.extend_column] : self.item_name
128     end
129     
130     def valid_encode
131       self.class.valid_encode_columns.each do |a|
132         next if attributes[a] == nil
133         raise Pettanr::BadRequest unless attributes[a].valid_encoding?
134       end
135     end
136     
137     def supply_default
138     end
139     
140     def overwrite 
141     end
142     
143     def visible? operators
144       if Manifest.manifest.magic_numbers['run_mode'] == 0
145         return false unless operators.guest?
146       else
147         return false unless operators.resource_reader?
148       end
149       true
150     end
151     
152     def boost
153     end
154     
155     def dom_id_item 
156       self.new_record? ? '0' : self.id.to_s
157     end
158     
159     def tag_item_id c = nil
160       self.item_name + self.item_id + c.to_s
161     end
162     
163     def dom_id_item_field field_name
164       self.tag_item_id + field_name.to_s
165     end
166     
167     def path_name with_engine = false
168       self.class.path_name(with_engine)
169     end
170     
171     def form_template with_engine = false
172       self.path_name(with_engine) + '/form'
173     end
174     
175     def tag_item_attributes column = nil, opt = {}
176       {
177         :id => self.field_tag_id(column), :panel_id => self.tag_panel_id, 
178         :element_id => self.tag_element_id, :element_type => self.tag_element_type
179       }.merge(opt)
180     end
181     
182       def tag_new_index
183       end
184       
185     def field_tag_attributes column, no_attr, opt = {}
186       self.tag_attributes(column).merge(
187         {:column => column, :new_index => self.tag_new_index, :no_attr => no_attr}
188       ).merge(opt)
189     end
190     
191     #render element by body
192     def any_tag_attributes name = nil, opt = {}
193       r = self.tag_attributes(name)
194       r.merge!(
195         {:new_index => self.tag_new_index}
196       ) if self.new_index
197       r.merge(opt)
198     end
199     
200     def select_tag_attributes(selected, column, no_attr)
201       [
202         :last, :first, 
203         {:html => {:selected => selected}}, 
204         self.field_tag_attributes(column, no_attr)
205       ]
206     end
207     
208     def tag_attr column = nil, opt = {}
209       self.tag_attributes(column, opt).to_attr
210     end
211     
212     def field_tag_attr column, no_attr, opt = {}
213       self.field_tag_attributes(column, no_attr, opt).to_attr
214     end
215     
216     def any_tag_attr name = nil, opt = {}
217       self.any_tag_attributes(name, opt).to_attr
218     end
219     
220   end
221 end