OSDN Git Service

fix: bin tool
[pettanr/pettanr.git] / lib / peta / leaf.rb
index 8dedbd6..f7a2b10 100644 (file)
@@ -4,59 +4,82 @@ module Peta
     
     # Dynamic ClassMethods
     
-    def self.play_list_where cid
-      ['scroll_panels.scroll_id = ?', cid]
+    def self.load_manifest
+      super
+      return nil if self._skip_load?
+      # Class Methods
+      pm = Manifest.manifest.models[self.my_peta.parent_item_name].classify
+      define_singleton_method("parent_model") do 
+        pm
+      end
+      destm = Manifest.manifest.models[self.my_peta.destination_item_name].classify
+      define_singleton_method("destination_model") do 
+        destm
+      end
+      pfk = self.my_peta.parent_item_name + '_id'
+      define_singleton_method("binder_key") do 
+        pfk
+      end
+      dest_key = if self.my_peta.destination_item_name
+        self.my_peta.destination_item_name + '_id'
+      else
+        nil
+      end
+      define_singleton_method("destination_key") do 
+        dest_key
+      end
+      # Instance Methods
     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)
+    def self.element?
+      true
     end
     
-    def self.play_sheet_where sid
-      ['story_sheets.story_id = ?', sid]
+    def self.root_model
+      self.parent_model
     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
+    def self.binder_model
+      self.parent_model
     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)
+    def self.destination_model
+      self.parent_model
     end
     
-    def self.new_t binder_id
-      r = ScrollPanel.max_t(binder_id)
-      r.blank? ? 0 : r.to_i + 1
+    def root
+      self.__send__ self.class.root_model.item_name
     end
     
-    def self.max_t binder_id
-      self.class..maximum(:t, :conditions => ['scroll_id = ?', binder_id])
+    def own? operators
+      self.root.own? operators
     end
     
-    def self.find_t binder_id, t
-      self.class.find(:first, :conditions => ['scroll_id = ? and t = ?', binder_id, t])
+    # super return if my item
+    def visible? operators
+      return false if super == false
+      self.root.visible? operators
     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}
+    def self.play_list_order
+      self.table_name + '.t'
+    end
+    
+    def self.new_t binder_id
+      r = self.max_t(binder_id)
+      r.blank? ? 0 : r.to_i + 1
     end
     
-    def self.top_sheet story, author
-      StorySheet.play_list(story, author).first
+    def self.max_t binder_id
+      self.where([self.binder_key + ' = ?', binder_id]).maximum(:t)
+    end
+    
+    def self.find_t binder_id, t
+      self.where([self.binder_key + ' = ? and t = ?', binder_id, t]).first
     end
     
-    def self.collect_t story_sheet
-      r = StorySheet.find(:all, :conditions => ['story_id = ?', story_sheet.story_id], :order => 't')
+    def self.collect_t binder_id
+      r = self.where([self.binder_key + ' = ?', binder_id]).order('t')
       r.map {|sp| sp.t}
     end
     
@@ -69,24 +92,44 @@ 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 destination_key
+      self.class.destination_key
+    end
+    
+    def binder_id
+      self.attributes[self.binder_key]
+    end
+    
+    def destination_id
+      if self.destination_key
+        self.attributes[self.destination_key]
+      else
+        nil
+      end
     end
     
     def insert_shift
-      ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
+      self.class.where([self.binder_key + ' = ? and t >= ?', self.binder_id, self.t]).update_all('t = t + 1')
     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.where([self.binder_key + ' = ? and (t >= ? and t < ?)', self.binder_id, self.t, old_t]).update_all('t = t + 1')
     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.where([self.binder_key + ' = ? and (t > ? and t <= ?)', self.binder_id, old_t, self.t]).update_all('t = t - 1')
     end
     
     def update_shift old_t
@@ -100,7 +143,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
@@ -112,17 +155,37 @@ 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)
+    def store operators, old_t = nil
+      res = false
+      self.class.transaction do
+        case self.allow? operators
+        when true
+          self.rotate old_t
+        when false
+          raise ActiveRecord::Forbidden
+        else
+        end
+        res = self.save
+        raise ActiveRecord::Rollback unless res
+        res = self.class.validate_t(self.binder_id) 
+        unless res
+          self.errors.add :t, 'unserialized'
+          raise ActiveRecord::Rollback 
+        end
+      end
+      res
     end
     
-    def destroy_and_shorten
+    # destroy and shorten
+    def destroy
       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
+      self.class.transaction do
+        # renumber t
+        self.class.update_all('t = t - 1', 
+          [self.binder_key + ' = ? and (t > ?)', self.binder_id, self.t]
+        )
+        res = super
+        raise ActiveRecord::Rollback unless res
       end
       res
     end