OSDN Git Service

fix picture io:
[pettanr/pettanr.git] / lib / peta / leaf.rb
index 59b84a5..f7a2b10 100644 (file)
@@ -8,14 +8,26 @@ module Peta
       super
       return nil if self._skip_load?
       # Class Methods
-      pm = Manifest.manifest.models[self.my_peta.parent_model_name].classify
+      pm = Manifest.manifest.models[self.my_peta.parent_item_name].classify
       define_singleton_method("parent_model") do 
         pm
       end
-      pfk = self.my_peta.parent_model_name + '_id'
+      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
     
@@ -27,6 +39,14 @@ module Peta
       self.parent_model
     end
     
+    def self.binder_model
+      self.parent_model
+    end
+    
+    def self.destination_model
+      self.parent_model
+    end
+    
     def root
       self.__send__ self.class.root_model.item_name
     end
@@ -41,61 +61,28 @@ module Peta
       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
-    
-    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 = self.max_t(binder_id)
       r.blank? ? 0 : r.to_i + 1
     end
     
     def self.max_t binder_id
-      self.maximum(:t, :conditions => [self.binder_key + ' = ?', binder_id])
+      self.where([self.binder_key + ' = ?', binder_id]).maximum(:t)
     end
     
     def self.find_t binder_id, t
-      self.find(:first, :conditions => [self.binder_key + ' = ? and t = ?', binder_id, t])
+      self.where([self.binder_key + ' = ? and t = ?', binder_id, t]).first
     end
     
     def self.collect_t binder_id
-      r = self.find(:all, :conditions => [self.binder_key + ' = ?', binder_id], :order => 't')
+      r = self.where([self.binder_key + ' = ?', binder_id]).order('t')
       r.map {|sp| sp.t}
     end
     
-    def self.top_sheet story, author
-      StorySheet.play_list(story, author).first
-    end
-    
     def self.serial? ary
       i = 0
       ary.compact.sort.each do |t|
@@ -113,30 +100,36 @@ module Peta
       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
-      self.class.update_all('t = t + 1', 
-        [self.binder_key + ' = ? and t >= ?', self.binder_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
-      self.class.update_all('t = t + 1', 
-        [self.binder_key + ' = ? and (t >= ? and t < ?)', self.binder_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 = 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
-      self.class.update_all('t = t - 1', 
-        [self.binder_key + ' = ? and (t > ? and t <= ?)', self.binder_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
@@ -162,14 +155,37 @@ module Peta
       end
     end
     
-    def destroy_and_shorten
+    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
+    
+    # destroy and shorten
+    def destroy
       res = false
       self.class.transaction do
+        # renumber t
         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
+        res = super
+        raise ActiveRecord::Rollback unless res
       end
       res
     end