+module Peta
+ class Leaf < Content
+ self.abstract_class = true
+
+ # Dynamic ClassMethods
+
+ def self.play_list_where cid
+ ['scroll_panels.scroll_id = ?', cid]
+ end
+
+ def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
+ ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
+ end
+
+ def self.play_sheet_where sid
+ ['story_sheets.story_id = ?', sid]
+ end
+
+ def self.play_sheet story, operators, page = 1
+ ss = StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).order('story_sheets.t').offset(page -1).limit(1).first
+ if ss
+ if ss.sheet
+ Sheet.show(ss.sheet.id, operators)
+ else
+ nil
+ end
+ else
+ nil
+ end
+ end
+
+ def self.play_paginate story, page
+ Kaminari.paginate_array(Array.new(StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).count, nil)).page(page).per(1)
+ end
+
+ def self.new_t binder_id
+ r = ScrollPanel.max_t(binder_id)
+ r.blank? ? 0 : r.to_i + 1
+ end
+
+ def self.max_t binder_id
+ self.class..maximum(:t, :conditions => ['scroll_id = ?', binder_id])
+ end
+
+ def self.find_t binder_id, t
+ self.class.find(:first, :conditions => ['scroll_id = ? and t = ?', binder_id, t])
+ end
+
+ def self.collect_t scroll_panel
+ r = self.class.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
+ r.map {|sp| sp.t}
+ end
+
+ def self.top_sheet story, author
+ StorySheet.play_list(story, author).first
+ end
+
+ def self.collect_t story_sheet
+ r = StorySheet.find(:all, :conditions => ['story_id = ?', story_sheet.story_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
+
+ 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
+end
+