OSDN Git Service

t#32471:add profiles
[pettanr/pettanr.git] / app / models / scroll_panel.rb
1 class ScrollPanel < ActiveRecord::Base
2   belongs_to :author
3   belongs_to :panel
4   belongs_to :scroll
5   
6   validates :scroll_id, :presence => true, :numericality => true, :existence => {:both => false}
7   validates :panel_id, :presence => true, :numericality => true, :existence => {:both => false}
8   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
9   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
10   
11   def supply_default
12     self.scroll_id = nil
13     self.panel_id = nil
14     self.t = nil
15   end
16   
17   def overwrite au
18     return false unless au
19     self.author_id = au.id
20   end
21   
22   def own? roles
23     roles = [roles] unless roles.respond_to?(:each)
24     au = ScrollPanel.get_author_from_roles roles
25     return false unless au
26     self.author_id == au.id
27   end
28   
29   def visible? roles
30     if MagicNumber['run_mode'] == 0
31       return false unless guest_role_check(roles)
32     else
33       return false unless reader_role_check(roles)
34     end
35     return true if self.scroll.own?(roles)
36     self.scroll.visible? roles
37   end
38   
39   def self.default_panel_size
40     30
41   end
42   
43   def self.max_panel_size
44     200
45   end
46   
47   def self.offset cnt, prm = nil
48     offset = prm.to_i
49     offset = cnt - 1 if offset >= cnt
50     offset = cnt - offset.abs if offset < 0
51     offset = 0 if offset < 0
52     offset
53   end
54   
55   def self.panel_count cnt, prm = self.default_panel_size
56     count = prm.to_i
57     count = self.max_panel_size if count > self.max_panel_size
58     count = self.default_panel_size if count < 1
59     count
60   end
61   
62   def self.default_page_size
63     25
64   end
65   
66   def self.max_page_size
67     100
68   end
69   
70   def self.page prm = nil
71     page = prm.to_i
72     page = 1 if page < 1
73     page
74   end
75   
76   def self.page_size prm = self.default_page_size
77     page_size = prm.to_i
78     page_size = self.max_page_size if page_size > self.max_page_size
79     page_size = self.default_page_size if page_size < 1
80     page_size
81   end
82   
83   def self.play_list_where cid
84     ['scroll_panels.scroll_id = ?', cid]
85   end
86   
87   def self.list_where
88     'scrolls.visible > 0'
89   end
90   
91   def self.mylist_where au
92     ['scroll_panels.author_id = ?', au.id]
93   end
94   
95   def self.himlist_where au
96     ['scroll_panels.author_id = ? and scrolls.visible > 0', au.id]
97   end
98   
99   def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
100     ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
101   end
102   
103   def self.list page = 1, page_size = self.default_page_size
104     ScrollPanel.where(self.list_where()).includes(ScrollPanel.list_opt).order('scroll_panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
105   end
106   
107   def self.mylist au, page = 1, page_size = Author.default_scroll_panel_page_size
108     ScrollPanel.where(self.mylist_where(au)).includes(ScrollPanel.list_opt).order('scroll_panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
109   end
110   
111   def self.himlist au, page = 1, page_size = Author.default_scroll_panel_page_size
112     ScrollPanel.where(self.himlist_where(au)).includes(ScrollPanel.list_opt).order('scroll_panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
113   end
114   
115   def self.list_paginate page = 1, page_size = self.default_page_size
116     Kaminari.paginate_array(Array.new(ScrollPanel.where(self.list_where()).includes(ScrollPanel.list_opt).count, nil)).page(page).per(page_size)
117   end
118   
119   def self.mylist_paginate au, page = 1, page_size = Author.default_scroll_panel_page_size
120     Kaminari.paginate_array(Array.new(ScrollPanel.where(self.mylist_where(au)).includes(ScrollPanel.list_opt).count, nil)).page(page).per(page_size)
121   end
122   
123   def self.himlist_paginate au, page = 1, page_size = Author.default_scroll_panel_page_size
124     Kaminari.paginate_array(Array.new(ScrollPanel.where(self.himlist_where(au)).includes(ScrollPanel.list_opt).count, nil)).page(page).per(page_size)
125   end
126   
127   def self.list_by_scroll_where scroll_id
128     ['scroll_panels.scroll_id = ?', scroll_id]
129   end
130   
131   def self.list_by_scroll scroll_id, roles, page = 1, page_size = self.default_page_size
132     self.where(self.list_by_scroll_where(scroll_id)).includes(self.list_opt).order('scroll_panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
133   end
134   
135   def self.list_by_panel_where panel_id
136     ['scroll_panels.panel_id = ?', panel_id]
137   end
138   
139   def self.list_by_panel panel_id, roles, page = 1, page_size = self.default_page_size
140     self.where(self.list_by_panel_where(panel_id)).includes(self.list_opt).order('scroll_panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
141   end
142   
143   def self.list_opt
144     {
145       :author => {}, 
146       :scroll => {
147         :author => {}
148       }, 
149       :panel => {
150         :author => {}, 
151         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
152         :speech_balloons =>{:balloon => {}, :speech => {}}
153       }
154     }
155   end
156   
157   def self.list_json_opt
158     {:include => {
159       :author => {}, 
160       :scroll => {
161         :author => {}
162       }, 
163       :panel => {
164         :author => {}, 
165         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
166         :speech_balloons =>{:balloon => {}, :speech => {}}
167       }
168     }}
169   end
170   
171   def self.show spid, roles
172     opt = {}
173     opt.merge!(ScrollPanel.show_opt)
174     res = ScrollPanel.find spid, opt
175     raise ActiveRecord::Forbidden unless res.visible?(roles)
176     res
177   end
178   
179   def self.edit spid, au
180     opt = {}
181     opt.merge!(ScrollPanel.show_opt)
182     res = ScrollPanel.find spid, opt
183     raise ActiveRecord::Forbidden unless res.own?(au)
184     res
185   end
186   
187   def self.show_opt
188     {:include => {
189       :author => {}, 
190       :scroll => {
191         :author => {}
192       }, 
193       :panel => {
194         :author => {}, 
195         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
196         :speech_balloons =>{:balloon => {}, :speech => {}}
197       }
198     }}
199   end
200   
201   def elements
202     self.panel.elements
203   end
204   
205   def scroll_panel_as_json au
206     panel_include = if self.panel and self.panel.visible?(au)
207       {:include => {:author => {}}, :methods => :elements}
208     else
209       {:include => {:author => {}}}
210     end
211     self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
212   end
213   
214   def self.list_as_json_text ary, au
215     '[' + ary.map {|i| i.scroll_panel_as_json(au) }.join(',') + ']'
216   end
217   
218   def self.licensed_pictures scroll_panels
219     r = {}
220     scroll_panels.each do |scroll_panel|
221       r.merge!(scroll_panel.panel.licensed_pictures) if scroll_panel.panel
222     end
223     r
224   end
225   
226   def self.new_t scroll_id
227     r = ScrollPanel.max_t(scroll_id)
228     r.blank? ? 0 : r.to_i + 1
229   end
230   
231   def self.max_t scroll_id
232     ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id])
233   end
234   
235   def self.find_t scroll_id, t
236     ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t])
237   end
238   
239   def self.collect_t scroll_panel
240     r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
241     r.map {|sp| sp.t}
242   end
243   
244   def self.serial? ary
245     i = 0
246     ary.compact.sort.each do |t|
247       break false unless t == i
248       i += 1
249     end
250     ary.compact.size == i
251   end
252   
253   def self.validate_t scroll_panel
254     ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
255   end
256   
257   def insert_shift
258     ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
259   end
260   
261   def lesser_shift old_t
262     self.t = 0 if self.t < 0
263     ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
264   end
265   
266   def higher_shift old_t
267     nf = ScrollPanel.find_t(self.scroll_id, self.t)
268     max_t = ScrollPanel.max_t(self.scroll_id).to_i
269     self.t = max_t if self.t > max_t
270     ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
271   end
272   
273   def update_shift old_t
274     if self.t > old_t
275       higher_shift old_t
276     else
277       lesser_shift old_t
278     end
279   end
280   
281   def rotate old_t = nil
282     if self.new_record?
283       if self.t.blank?
284         self.t = ScrollPanel.new_t self.scroll_id
285       else
286         self.insert_shift
287       end
288     else
289       if self.t.blank?
290       else
291         self.update_shift old_t
292       end
293     end
294   end
295   
296   def allow?
297     return nil if self.scroll_id == nil or self.panel_id == nil
298     self.scroll.own?(self.author) and self.panel.usable?(self.author)
299   end
300   
301   def store old_t = nil
302     res = false
303     ScrollPanel.transaction do
304       case self.allow?
305       when true
306         self.rotate old_t
307       when false
308         raise ActiveRecord::Forbidden
309       else
310       end
311       res = self.save
312       raise ActiveRecord::Rollback unless res
313       res = ScrollPanel.validate_t(self) 
314       unless res
315         self.errors.add :t, 'unserialized'
316         raise ActiveRecord::Rollback 
317       end
318     end
319     res
320   end
321   
322   def destroy_and_shorten
323     res = false
324     ScrollPanel.transaction do
325       ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
326       raise ActiveRecord::Rollback unless self.destroy
327       res = true
328     end
329     res
330   end
331   
332 end