1 class ScrollPanel < Peta::Element
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.play_list_where cid
37 ['scroll_panels.scroll_id = ?', cid]
40 def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
41 ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
52 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
53 :speech_balloons =>{:balloon => {}, :speech => {}}
58 def self.list_json_opt
66 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
67 :speech_balloons =>{:balloon => {}, :speech => {}}
80 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
81 :speech_balloons =>{:balloon => {}, :speech => {}}
90 def scroll_panel_as_json au
91 panel_include = if self.panel and self.panel.visible?(au)
92 {:include => {:author => {}}, :methods => :elements}
94 {:include => {:author => {}}}
96 self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
99 def self.list_as_json_text ary, au
100 '[' + ary.map {|i| i.scroll_panel_as_json(au) }.join(',') + ']'
103 def self.licensed_pictures scroll_panels
105 scroll_panels.each do |scroll_panel|
106 r.merge!(scroll_panel.panel.licensed_pictures) if scroll_panel.panel
111 def self.new_t scroll_id
112 r = ScrollPanel.max_t(scroll_id)
113 r.blank? ? 0 : r.to_i + 1
116 def self.max_t scroll_id
117 ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id])
120 def self.find_t scroll_id, t
121 ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t])
124 def self.collect_t scroll_panel
125 r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
131 ary.compact.sort.each do |t|
132 break false unless t == i
135 ary.compact.size == i
138 def self.validate_t scroll_panel
139 ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
143 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
146 def lesser_shift old_t
147 self.t = 0 if self.t < 0
148 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
151 def higher_shift old_t
152 nf = ScrollPanel.find_t(self.scroll_id, self.t)
153 max_t = ScrollPanel.max_t(self.scroll_id).to_i
154 self.t = max_t if self.t > max_t
155 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
158 def update_shift old_t
166 def rotate old_t = nil
169 self.t = ScrollPanel.new_t self.scroll_id
176 self.update_shift old_t
182 return nil if self.scroll_id == nil or self.panel_id == nil
183 self.scroll.own?(operators) and self.panel.usable?(operators)
186 def store operators, old_t = nil
188 ScrollPanel.transaction do
189 case self.allow? operators
193 raise ActiveRecord::Forbidden
197 raise ActiveRecord::Rollback unless res
198 res = ScrollPanel.validate_t(self)
200 self.errors.add :t, 'unserialized'
201 raise ActiveRecord::Rollback
207 def destroy_and_shorten
209 ScrollPanel.transaction do
210 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
211 raise ActiveRecord::Rollback unless self.destroy