OSDN Git Service

fix leaf view
[pettanr/pettanr.git] / lib / peta / leaf.rb
index 243c60d..59b84a5 100644 (file)
@@ -6,11 +6,16 @@ module Peta
     
     def self.load_manifest
       super
+      return nil if self._skip_load?
       # Class Methods
       pm = Manifest.manifest.models[self.my_peta.parent_model_name].classify
       define_singleton_method("parent_model") do 
         pm
       end
+      pfk = self.my_peta.parent_model_name + '_id'
+      define_singleton_method("binder_key") do 
+        pfk
+      end
       # Instance Methods
     end
     
@@ -22,10 +27,28 @@ module Peta
       self.parent_model
     end
     
+    def root
+      self.__send__ self.class.root_model.item_name
+    end
+    
+    def own? operators
+      self.root.own? operators
+    end
+    
+    # super return if my item
+    def visible? operators
+      return false if super == false
+      self.root.visible? operators
+    end
+    
     def self.play_list_where cid
       ['scroll_panels.scroll_id = ?', cid]
     end
     
+    def self.play_list_order
+      self.table_name + '.t'
+    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
@@ -52,20 +75,20 @@ module Peta
     end
     
     def self.new_t binder_id
-      r = ScrollPanel.max_t(binder_id)
+      r = self.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])
+      self.maximum(:t, :conditions => [self.binder_key + ' = ?', binder_id])
     end
     
     def self.find_t binder_id, t
-      self.class.find(:first, :conditions => ['scroll_id = ? and t = ?', binder_id, t])
+      self.find(:first, :conditions => [self.binder_key + ' = ? 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')
+    def self.collect_t binder_id
+      r = self.find(:all, :conditions => [self.binder_key + ' = ?', binder_id], :order => 't')
       r.map {|sp| sp.t}
     end
     
@@ -73,11 +96,6 @@ module Peta
       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|
@@ -87,24 +105,38 @@ module Peta
       ary.compact.size == i
     end
     
-    def self.validate_t scroll_panel
-      ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
+    def self.validate_t binder_id
+      self.serial?(self.collect_t(binder_id))
+    end
+    
+    def binder_key
+      self.class.binder_key
+    end
+    
+    def binder_id
+      self.attributes[self.binder_key]
     end
     
     def insert_shift
-      ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
+      self.class.update_all('t = t + 1', 
+        [self.binder_key + ' = ? and t >= ?', self.binder_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])
+      self.class.update_all('t = t + 1', 
+        [self.binder_key + ' = ? and (t >= ? and t < ?)', self.binder_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
+      nf = self.class.find_t(self.binder_id, self.t)
+      max_t = self.class.max_t(self.binder_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])
+      self.class.update_all('t = t - 1', 
+        [self.binder_key + ' = ? and (t > ? and t <= ?)', self.binder_id, old_t, self.t]
+      )
     end
     
     def update_shift old_t
@@ -118,7 +150,7 @@ module Peta
     def rotate old_t = nil
       if self.new_record?
         if self.t.blank?
-          self.t = ScrollPanel.new_t self.scroll_id
+          self.t = self.class.new_t self.binder_id
         else
           self.insert_shift
         end
@@ -130,15 +162,12 @@ module Peta
       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])
+      self.class.transaction do
+        self.class.update_all('t = t - 1', 
+          [self.binder_key + ' = ? and (t > ?)', self.binder_id, self.t]
+        )
         raise ActiveRecord::Rollback unless self.destroy
         res = true
       end