OSDN Git Service

208def0210a4c19eb3067508020c3c51c8997c64
[pettanr/pettanr.git] / app / models / panel.rb
1 #コマ
2 class Panel < ActiveRecord::Base
3   belongs_to :author
4   belongs_to :resource_picture
5   has_many :stories
6   has_many :panel_pictures, :dependent => :destroy
7   has_many :speech_balloons, :dependent => :destroy
8   has_many :ground_pictures, :dependent => :destroy
9   has_many :ground_colors, :dependent => :destroy
10   has_many :panel_colors, :dependent => :destroy
11   accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
12   accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
13   accepts_nested_attributes_for :ground_pictures, :allow_destroy => true
14   accepts_nested_attributes_for :ground_colors, :allow_destroy => true
15   accepts_nested_attributes_for :panel_colors, :allow_destroy => true
16
17   validates :width, :presence => true, :numericality => true, :natural_number => true
18   validates :height, :presence => true, :numericality => true, :natural_number => true
19   validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
20   validates :x, :numericality => {:allow_blank => true}
21   validates :y, :numericality => {:allow_blank => true}
22   validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
23   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
24   validates :publish, :presence => true, :numericality => true
25   
26   def supply_default
27     self.border = 2
28     self.publish = 0
29   end
30   
31   def overwrite au
32     self.author_id = au.id
33   end
34   
35   def own? roles
36     roles = [roles] unless roles.respond_to?(:each)
37     au = Panel.get_author_from_roles roles
38     return false unless au
39     self.author_id == au.id
40   end
41   
42   def visible? roles
43     if MagicNumber['run_mode'] == 0
44       return false unless guest_role_check(roles)
45     else
46       return false unless reader_role_check(roles)
47     end
48     return true if self.own?(roles)
49     self.publish?
50   end
51   
52   def usable? au
53     visible? au
54   end
55   
56   def publish?
57     self.publish > 0
58   end
59   
60   def self.default_page_size
61     25
62   end
63   
64   def self.max_page_size
65     100
66   end
67   
68   def self.page prm = nil
69     page = prm.to_i
70     page = 1 if page < 1
71     page
72   end
73   
74   def self.page_size prm = self.default_page_size
75     page_size = prm.to_i
76     page_size = self.max_page_size if page_size > self.max_page_size
77     page_size = self.default_page_size if page_size < 1
78     page_size
79   end
80   
81   def self.list page = 1, page_size = self.default_page_size
82     opt = {}
83     opt.merge!(self.list_opt)
84     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
85     opt.merge!({:conditions => 'panels.publish > 0', :order => 'panels.updated_at desc'})
86     Panel.find(:all, opt)
87   end
88   
89   def self.list_opt
90     {:include => {
91       :panel_pictures => {
92         :picture => {:artist => {}, :license => {}}
93       }, 
94       :speech_balloons => {:balloons => {}, :speeches => {}},
95       :ground_pictures => {
96         :picture => {:artist => {}, :license => {}}
97       }, 
98       :ground_colors => {
99         :color => {}
100       }, 
101       :panel_colors => {
102       }, 
103       :author => {}
104     }}
105   end
106   
107   def self.mylist au, page = 1, page_size = Author.default_panel_page_size
108     opt = {}
109     opt.merge!(self.list_opt)
110     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
111     opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panels.updated_at desc'})
112     Panel.find(:all, opt)
113   end
114   
115   def self.show rid, roles
116     opt = {}
117     opt.merge!(Panel.show_opt)
118     res = Panel.find(rid, opt)
119     raise ActiveRecord::Forbidden unless res.visible?(roles)
120     res
121   end
122   
123   def self.edit rid, au
124     opt = {}
125     opt.merge!(Panel.show_opt)
126     res = Panel.find(rid, opt)
127     raise ActiveRecord::Forbidden unless res.own?(au)
128     res
129   end
130   
131   def self.show_opt
132     {:include => {
133       :panel_pictures => {
134         :picture => {:artist => {}, :license => {}}
135       }, 
136       :speech_balloons => {:balloons => {}, :speeches => {}},
137       :ground_pictures => {
138         :picture => {:artist => {}, :license => {}}
139       }, 
140       :ground_colors => {
141         :color => {}
142       }, 
143       :panel_colors => {
144       }, 
145       :author => {}
146     }}
147   end
148   
149   def parts_element
150     ((self.panel_pictures || []) + (self.speech_balloons || [])).compact
151   end
152   
153   def parts
154     res = []
155     self.parts_element.each do |e|
156       res[e.t] = e
157     end
158     res
159   end
160   
161   def grounds
162     ((self.ground_colors || []) + (self.ground_pictures || []) + (self.panel_colors || [])).compact
163   end
164   
165   def panel_elements
166     parts + grounds
167   end
168   
169   @@elm_json_opt = {
170     'PanelPicture' => {
171       :picture => {:artist => {}, :license => {}}
172     }, 
173     'SpeechBalloon' => {:balloons => {}, :speeches => {}},
174     'GroundPicture' => {
175       :picture => {:artist => {}, :license => {}}
176     }, 
177     'GroundColor' => {
178       :color => {}
179     }, 
180     'PanelColor' => {
181     } 
182   }
183   
184   def self.elm_json_opt e
185     @@elm_json_opt[e.class.to_s]
186   end
187   
188   def elements
189     self.panel_elements.map {|e|
190       #(-_-;)<... kore wa hidoi
191       JSON.parse e.to_json({:include => Panel.elm_json_opt(e)})
192     }
193   end
194   
195   def panel_elements_as_json
196     self.to_json({:include => {:author => {}}, :methods => :elements})
197   end
198   
199   def self.list_as_json_text ary
200     '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
201   end
202   
203   def scenario
204     panel_elements.map { |e|
205       e.scenario
206     }.join
207   end
208   
209   def plain_scenario
210     panel_elements.map { |e|
211       e.plain_scenario
212     }.join
213   end
214   
215   def licensed_pictures
216     r = {}
217     ((self.panel_pictures || []) + (self.ground_pictures || [])).compact.each do |elm|
218       r[elm.picture_id] = elm.picture unless r[elm.picture_id]
219     end
220     r
221   end
222   
223   def self.visible_count
224     Panel.count
225   end
226   
227   def self.collect_element_value elements, name
228     elements.map {|e|
229       e.map {|o|
230         o[name]
231       }
232     }.flatten
233   end
234   
235   def self.validate_t ary
236     i = 0
237     ary.compact.sort.each do |t|
238       break false unless t == i
239       i += 1
240     end
241     ary.compact.size == i
242   end
243   
244   def self.validate_element_t elements, name
245     Panel.validate_t(Panel.collect_element_value(elements, name))
246   end
247   
248   def self.validate_elements_t c
249     c.map {|conf|
250       Panel.validate_element_t(conf[:elements], conf[:name]) ? nil : false
251     }.compact.empty?
252   end
253   
254   def validate_t_list
255     [
256       {:elements => [self.panel_pictures, self.speech_balloons], :name => :t}
257     ]
258   end
259   def validate_child
260 #    r1 = Panel.validate_elements_id validate_id_list
261     Panel.validate_elements_t validate_t_list
262   end
263   
264   def store attr, au
265     if attr == false
266       self.errors.add :base, I18n.t('errors.invalid_json')
267       return false
268     end
269     self.attributes = attr
270     self.overwrite au
271     res = false
272     Panel.transaction do
273       unless validate_child
274         self.errors.add :base, I18n.t('errors.invalid_t')
275         raise ActiveRecord::Rollback
276       end
277       res = self.save
278     end
279     res
280   end
281   
282   def destroy_with_elements
283     res = false
284     Panel.transaction do
285       self.panel_elements.each do |element|
286         raise ActiveRecord::Rollback unless element.destroy
287       end
288       raise ActiveRecord::Rollback unless self.destroy
289       res = true
290     end
291     res
292   end
293   
294 =begin
295   def self.validate_id ary, pid
296     ary.map {|v|
297       if pid
298         (v == pid or v == nil) ? nil : false
299       else
300         v ? false : nil
301       end
302     }.compact.empty?
303   end
304   
305   def self.validate_element_id elements, name, pid
306     Panel.validate_id(Panel.collect_element_value(elements, name), pid)
307   end
308   
309   def self.validate_elements_id c
310     c.map {|conf|
311       Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
312     }.compact.empty?
313   end
314   
315   def validate_id_list
316     r = self.speech_balloons.map {|sb|
317       {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
318     }
319     r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})
320     r
321   end
322 =end
323   
324 end