1 class ScrollPanel < Pettanr::Content
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}
22 return false unless au
23 self.author_id = au.id
26 def visible? operators
27 return false unless super
28 self.owner_model.visible? operators
32 'scroll_panels.updated_at desc'
39 def self.play_list_where cid
40 ['scroll_panels.scroll_id = ?', cid]
43 def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
44 ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
55 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
56 :speech_balloons =>{:balloon => {}, :speech => {}}
61 def self.list_json_opt
69 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
70 :speech_balloons =>{:balloon => {}, :speech => {}}
83 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
84 :speech_balloons =>{:balloon => {}, :speech => {}}
93 def scroll_panel_as_json au
94 panel_include = if self.panel and self.panel.visible?(au)
95 {:include => {:author => {}}, :methods => :elements}
97 {:include => {:author => {}}}
99 self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
102 def self.list_as_json_text ary, au
103 '[' + ary.map {|i| i.scroll_panel_as_json(au) }.join(',') + ']'
106 def self.licensed_pictures scroll_panels
108 scroll_panels.each do |scroll_panel|
109 r.merge!(scroll_panel.panel.licensed_pictures) if scroll_panel.panel
114 def self.new_t scroll_id
115 r = ScrollPanel.max_t(scroll_id)
116 r.blank? ? 0 : r.to_i + 1
119 def self.max_t scroll_id
120 ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id])
123 def self.find_t scroll_id, t
124 ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t])
127 def self.collect_t scroll_panel
128 r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
134 ary.compact.sort.each do |t|
135 break false unless t == i
138 ary.compact.size == i
141 def self.validate_t scroll_panel
142 ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
146 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
149 def lesser_shift old_t
150 self.t = 0 if self.t < 0
151 ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
154 def higher_shift old_t
155 nf = ScrollPanel.find_t(self.scroll_id, self.t)
156 max_t = ScrollPanel.max_t(self.scroll_id).to_i
157 self.t = max_t if self.t > max_t
158 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
161 def update_shift old_t
169 def rotate old_t = nil
172 self.t = ScrollPanel.new_t self.scroll_id
179 self.update_shift old_t
185 return nil if self.scroll_id == nil or self.panel_id == nil
186 self.scroll.own?(self.author) and self.panel.usable?(self.author)
189 def store old_t = nil
191 ScrollPanel.transaction do
196 raise ActiveRecord::Forbidden
200 raise ActiveRecord::Rollback unless res
201 res = ScrollPanel.validate_t(self)
203 self.errors.add :t, 'unserialized'
204 raise ActiveRecord::Rollback
210 def destroy_and_shorten
212 ScrollPanel.transaction do
213 ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
214 raise ActiveRecord::Rollback unless self.destroy