OSDN Git Service

fix peta test
[pettanr/pettanr.git] / app / models / story.rb
index 0f240aa..5c550e1 100644 (file)
 #ストーリー
-class Story < ActiveRecord::Base
+class Story < Peta::Binder
+  load_manifest
+  has_many :comic_stories
+  has_many :story_sheets
   belongs_to :author
-  belongs_to :panel
-  belongs_to :comic
   
-  validates :comic_id, :presence => true, :numericality => true, :existence => true
-  validates :panel_id, :presence => true, :numericality => true, :existence => true
-  validates :author_id, :presence => true, :numericality => true, :existence => true
-  validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
+  validates :title, :presence => true, :length => {:maximum => 100}
+  validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
+  validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   
-  def supply_default au
-    return false unless au
-    self.author_id = au.id
+  def supply_default
+    self.visible = 0 if self.visible.blank?
+    self.author_id = nil
   end
   
-  def self.new_t comic_id
-    r = Story.max_t(comic_id)
-    r.blank? ? 0 : r.to_i + 1
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
+    super()
   end
   
-  def self.max_t comic_id
-    Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
-  end
-  
-  def self.find_t comic_id, t
-    Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
-  end
-  
-  def self.collect_t story
-    r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
-    r << story if story.new_record?
-    r.map {|s| s.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 story
-    Story.serial?(Story.collect_t(story))
-  end
-  
-  def insert_shift
-    Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
-  end
-  
-  def lesser_shift old_t
-    Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
-  end
-  
-  def higher_shift old_t
-    nf = Story.find_t(self.comic_id, self.t)
-    max_t = Story.max_t(self.comic_id).to_i
-    self.t = max_t if self.t > max_t
-    Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
-  end
-  
-  def update_shift old_t
-    if self.t > old_t
-      higher_shift old_t
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
     else
-      lesser_shift old_t
+      self.visible > 0
     end
   end
   
-  def rotate old_t = nil
-    if self.new_record?
-      if self.t.blank?
-        self.t = Story.new_t self.comic_id
-      else
-        self.insert_shift
-      end
-    else
-      if self.t.blank?
-      else
-        self.update_shift old_t
-      end
-    end
-  end
-  
-  def store old_t = nil
-    res = false
-    Story.transaction do
-      self.rotate old_t
-      res = self.save
-      unless Story.validate_t(self)
-        res = false
-        raise ActiveRecord::Rollback 
-      end
-    end
-    res
-  end
-  
-  def move_to new_t
-    return true if self.t == new_t
-    if self.t > new_t
-      Panel.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, new_t, self.t])
-    else
-      nf = Panel.find_t(self.comic_id, new_t)
-      max_t = Panel.max_t.to_i self.comic_id
-      new_t = max_t if new_t > max_t
-      Panel.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, self.t, new_t])
-    end
-    self.t = new_t
-    self.save
+  def self.public_list_where
+    'stories.visible > 0'
   end
   
-  def destroy_and_shorten
-    Panel.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
-    self.destroy
+  def self.show_opt
+    {:include => {:story_sheets => {:sheet => {}}, :author => {}}}
   end
   
 end