1 class ScrollPanel < ActiveRecord::Base
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}
18 return false unless au
19 self.author_id = au.id
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
30 if MagicNumber['run_mode'] == 0
31 return false unless guest_role_check(roles)
33 return false unless reader_role_check(roles)
35 return true if self.scroll.own?(roles)
36 self.scroll.visible? roles
39 def self.default_panel_size
43 def self.max_panel_size
47 def self.offset cnt, prm = nil
49 offset = cnt - 1 if offset >= cnt
50 offset = cnt - offset.abs if offset < 0
51 offset = 0 if offset < 0
55 def self.panel_count cnt, prm = self.default_panel_size
57 count = self.max_panel_size if count > self.max_panel_size
58 count = self.default_panel_size if count < 1
62 def self.default_page_size
66 def self.max_page_size
70 def self.page prm = nil
76 def self.page_size prm = self.default_page_size
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
83 def self.play_list_where cid
84 ['scroll_panels.scroll_id = ?', cid]
91 def self.mylist_where au
92 ['scroll_panels.author_id = ?', au.id]
95 def self.himlist_where au
96 ['scroll_panels.author_id = ? and scrolls.visible > 0', au.id]
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)
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)
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)
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)
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)
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)
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)
135 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
136 :speech_balloons =>{:balloon => {}, :speech => {}}
141 def self.list_json_opt
149 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
150 :speech_balloons =>{:balloon => {}, :speech => {}}
155 def self.show spid, roles
157 opt.merge!(ScrollPanel.show_opt)
158 res = ScrollPanel.find spid, opt
159 raise ActiveRecord::Forbidden unless res.visible?(roles)
163 def self.edit spid, au
165 opt.merge!(ScrollPanel.show_opt)
166 res = ScrollPanel.find spid, opt
167 raise ActiveRecord::Forbidden unless res.own?(au)
179 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
180 :speech_balloons =>{:balloon => {}, :speech => {}}
189 def scroll_panel_as_json au
190 panel_include = if self.panel and self.panel.visible?(au)
191 {:include => {:author => {}}, :methods => :elements}
193 {:include => {:author => {}}}
195 self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
198 def self.list_as_json_text ary, au
199 '[' + ary.map {|i| i.scroll_panel_as_json(au) }.join(',') + ']'
202 def self.licensed_pictures scroll_panels
204 scroll_panels.each do |scroll_panel|
205 r.merge!(scroll_panel.panel.licensed_pictures) if scroll_panel.panel
210 def self.new_t scroll_id
211 r = ScrollPanel.max_t(scroll_id)
212 r.blank? ? 0 : r.to_i + 1
215 def self.max_t scroll_id
216 ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id])
219 def self.find_t scroll_id, t
220 ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t])
223 def self.collect_t scroll_panel
224 r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
230 ary.compact.sort.each do |t|
231 break false unless t == i
234 ary.compact.size == i
237 def self.validate_t scroll_panel
238 ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
242 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
245 def lesser_shift old_t
246 self.t = 0 if self.t < 0
247 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
250 def higher_shift old_t
251 nf = ScrollPanel.find_t(self.scroll_id, self.t)
252 max_t = ScrollPanel.max_t(self.scroll_id).to_i
253 self.t = max_t if self.t > max_t
254 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
257 def update_shift old_t
265 def rotate old_t = nil
268 self.t = ScrollPanel.new_t self.scroll_id
275 self.update_shift old_t
281 return nil if self.scroll_id == nil or self.panel_id == nil
282 self.scroll.own?(self.author) and self.panel.usable?(self.author)
285 def store old_t = nil
287 ScrollPanel.transaction do
292 raise ActiveRecord::Forbidden
296 raise ActiveRecord::Rollback unless res
297 res = ScrollPanel.validate_t(self)
299 self.errors.add :t, 'unserialized'
300 raise ActiveRecord::Rollback
306 def destroy_and_shorten
308 ScrollPanel.transaction do
309 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
310 raise ActiveRecord::Rollback unless self.destroy