OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / scroll_panel.rb
index 9ff9fc2..3db3965 100644 (file)
@@ -1,4 +1,5 @@
-class ScrollPanel < Pettanr::Content
+class ScrollPanel < Peta::Leaf
+  load_manifest
   belongs_to :author
   belongs_to :panel
   belongs_to :scroll
@@ -8,10 +9,6 @@ class ScrollPanel < Pettanr::Content
   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
@@ -23,12 +20,7 @@ class ScrollPanel < Pettanr::Content
     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
   
@@ -36,6 +28,14 @@ class ScrollPanel < Pettanr::Content
     '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
@@ -111,76 +111,6 @@ class ScrollPanel < Pettanr::Content
     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)
@@ -188,7 +118,7 @@ class ScrollPanel < Pettanr::Content
   
   def store operators, old_t = nil
     res = false
-    ScrollPanel.transaction do
+    self.class.transaction do
       case self.allow? operators
       when true
         self.rotate old_t
@@ -198,7 +128,7 @@ class ScrollPanel < Pettanr::Content
       end
       res = self.save
       raise ActiveRecord::Rollback unless res
-      res = ScrollPanel.validate_t(self
+      res = self.class.validate_t(self.scroll_id
       unless res
         self.errors.add :t, 'unserialized'
         raise ActiveRecord::Rollback 
@@ -207,14 +137,4 @@ class ScrollPanel < Pettanr::Content
     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