-class ScrollPanel < Pettanr::Content
+class ScrollPanel < Peta::Leaf
+ load_manifest
belongs_to :author
belongs_to :panel
belongs_to :scroll
validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
- def self.owner_model
- Scroll
- end
-
def supply_default
self.scroll_id = nil
self.panel_id = nil
self.author_id = operators.author.id
end
- def visible? operators
- return false unless super
- self.owner_model.visible? operators
- end
-
- def self.list_order
+ def self.public_list_order
'scroll_panels.updated_at desc'
end
'scrolls.visible > 0'
end
+ def self.by_author_list_includes
+ {
+ :scroll => {
+ :author => {}
+ }
+ }
+ end
+
def self.play_list_where cid
['scroll_panels.scroll_id = ?', cid]
end
r
end
- def self.new_t scroll_id
- r = ScrollPanel.max_t(scroll_id)
- r.blank? ? 0 : r.to_i + 1
- end
-
- def self.max_t scroll_id
- ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id])
- end
-
- def self.find_t scroll_id, t
- ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t])
- end
-
- def self.collect_t scroll_panel
- r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
- r.map {|sp| sp.t}
- end
-
- def self.serial? ary
- i = 0
- ary.compact.sort.each do |t|
- break false unless t == i
- i += 1
- end
- ary.compact.size == i
- end
-
- def self.validate_t scroll_panel
- ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
- end
-
- def insert_shift
- ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
- end
-
- def lesser_shift old_t
- self.t = 0 if self.t < 0
- ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
- end
-
- def higher_shift old_t
- nf = ScrollPanel.find_t(self.scroll_id, self.t)
- max_t = ScrollPanel.max_t(self.scroll_id).to_i
- self.t = max_t if self.t > max_t
- ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
- end
-
- def update_shift old_t
- if self.t > old_t
- higher_shift old_t
- else
- lesser_shift old_t
- end
- end
-
- def rotate old_t = nil
- if self.new_record?
- if self.t.blank?
- self.t = ScrollPanel.new_t self.scroll_id
- else
- self.insert_shift
- end
- else
- if self.t.blank?
- else
- self.update_shift old_t
- end
- end
- end
-
def allow? operators
return nil if self.scroll_id == nil or self.panel_id == nil
self.scroll.own?(operators) and self.panel.usable?(operators)
end
res = self.save
raise ActiveRecord::Rollback unless res
- res = ScrollPanel.validate_t(self)
+ res = ScrollPanel.validate_t(self.scroll_id)
unless res
self.errors.add :t, 'unserialized'
raise ActiveRecord::Rollback
res
end
- def destroy_and_shorten
- res = false
- ScrollPanel.transaction do
- ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
- raise ActiveRecord::Rollback unless self.destroy
- res = true
- end
- res
- end
-
end