1 class ScrollPanel < Peta::Content
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}
12 def tag_attributes column = nil, opt = {}
23 def overwrite operators
24 return false unless operators.author
25 self.author_id = operators.author.id
28 def visible? operators
29 return false unless super
30 self.owner_model.visible? operators
34 'scroll_panels.updated_at desc'
41 def self.play_list_where cid
42 ['scroll_panels.scroll_id = ?', cid]
45 def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
46 ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
57 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
58 :speech_balloons =>{:balloon => {}, :speech => {}}
63 def self.list_json_opt
71 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
72 :speech_balloons =>{:balloon => {}, :speech => {}}
85 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
86 :speech_balloons =>{:balloon => {}, :speech => {}}
95 def scroll_panel_as_json au
96 panel_include = if self.panel and self.panel.visible?(au)
97 {:include => {:author => {}}, :methods => :elements}
99 {:include => {:author => {}}}
101 self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
104 def self.list_as_json_text ary, au
105 '[' + ary.map {|i| i.scroll_panel_as_json(au) }.join(',') + ']'
108 def self.licensed_pictures scroll_panels
110 scroll_panels.each do |scroll_panel|
111 r.merge!(scroll_panel.panel.licensed_pictures) if scroll_panel.panel
116 def self.new_t scroll_id
117 r = ScrollPanel.max_t(scroll_id)
118 r.blank? ? 0 : r.to_i + 1
121 def self.max_t scroll_id
122 ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id])
125 def self.find_t scroll_id, t
126 ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t])
129 def self.collect_t scroll_panel
130 r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
136 ary.compact.sort.each do |t|
137 break false unless t == i
140 ary.compact.size == i
143 def self.validate_t scroll_panel
144 ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
148 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
151 def lesser_shift old_t
152 self.t = 0 if self.t < 0
153 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
156 def higher_shift old_t
157 nf = ScrollPanel.find_t(self.scroll_id, self.t)
158 max_t = ScrollPanel.max_t(self.scroll_id).to_i
159 self.t = max_t if self.t > max_t
160 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
163 def update_shift old_t
171 def rotate old_t = nil
174 self.t = ScrollPanel.new_t self.scroll_id
181 self.update_shift old_t
187 return nil if self.scroll_id == nil or self.panel_id == nil
188 self.scroll.own?(operators) and self.panel.usable?(operators)
191 def store operators, old_t = nil
193 ScrollPanel.transaction do
194 case self.allow? operators
198 raise ActiveRecord::Forbidden
202 raise ActiveRecord::Rollback unless res
203 res = ScrollPanel.validate_t(self)
205 self.errors.add :t, 'unserialized'
206 raise ActiveRecord::Rollback
212 def destroy_and_shorten
214 ScrollPanel.transaction do
215 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
216 raise ActiveRecord::Rollback unless self.destroy