1 class ScrollPanel < Peta::Leaf
7 validates :scroll_id, :presence => true, :numericality => true, :existence => {:both => false}
8 validates :panel_id, :presence => true, :numericality => true, :existence => {:both => false}
9 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
10 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
18 def overwrite operators
19 return false unless operators.author
20 self.author_id = operators.author.id
23 def visible? operators
24 return false unless super
25 self.owner_model.visible? operators
29 'scroll_panels.updated_at desc'
36 def self.by_author_list_includes
44 def self.play_list_where cid
45 ['scroll_panels.scroll_id = ?', cid]
48 def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
49 ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
60 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
61 :speech_balloons =>{:balloon => {}, :speech => {}}
66 def self.list_json_opt
74 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
75 :speech_balloons =>{:balloon => {}, :speech => {}}
88 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
89 :speech_balloons =>{:balloon => {}, :speech => {}}
98 def scroll_panel_as_json au
99 panel_include = if self.panel and self.panel.visible?(au)
100 {:include => {:author => {}}, :methods => :elements}
102 {:include => {:author => {}}}
104 self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
107 def self.list_as_json_text ary, au
108 '[' + ary.map {|i| i.scroll_panel_as_json(au) }.join(',') + ']'
111 def self.licensed_pictures scroll_panels
113 scroll_panels.each do |scroll_panel|
114 r.merge!(scroll_panel.panel.licensed_pictures) if scroll_panel.panel
119 def self.new_t scroll_id
120 r = ScrollPanel.max_t(scroll_id)
121 r.blank? ? 0 : r.to_i + 1
124 def self.max_t scroll_id
125 ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id])
128 def self.find_t scroll_id, t
129 ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t])
132 def self.collect_t scroll_panel
133 r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
139 ary.compact.sort.each do |t|
140 break false unless t == i
143 ary.compact.size == i
146 def self.validate_t scroll_panel
147 ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
151 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
154 def lesser_shift old_t
155 self.t = 0 if self.t < 0
156 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
159 def higher_shift old_t
160 nf = ScrollPanel.find_t(self.scroll_id, self.t)
161 max_t = ScrollPanel.max_t(self.scroll_id).to_i
162 self.t = max_t if self.t > max_t
163 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
166 def update_shift old_t
174 def rotate old_t = nil
177 self.t = ScrollPanel.new_t self.scroll_id
184 self.update_shift old_t
190 return nil if self.scroll_id == nil or self.panel_id == nil
191 self.scroll.own?(operators) and self.panel.usable?(operators)
194 def store operators, old_t = nil
196 ScrollPanel.transaction do
197 case self.allow? operators
201 raise ActiveRecord::Forbidden
205 raise ActiveRecord::Rollback unless res
206 res = ScrollPanel.validate_t(self)
208 self.errors.add :t, 'unserialized'
209 raise ActiveRecord::Rollback
215 def destroy_and_shorten
217 ScrollPanel.transaction do
218 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
219 raise ActiveRecord::Rollback unless self.destroy