OSDN Git Service

fix: racer
[pettanr/pettanr.git] / app / models / comic_story.rb
index 77d4003..7f773d8 100644 (file)
@@ -6,9 +6,44 @@ class ComicStory < Peta::Leaf
   
   validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :story_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
   
+  scope :with_comic, -> do
+    includes(:comic)
+  end
+  
+  scope :find_index, -> do
+    with_comic.where(Comic.arel_table[:visible].gt 0).references(:comic)
+  end
+  
+  scope :find_private, -> (operators) do 
+    with_comic.where(Comic.arel_table[:author_id].eq operators.author.id).references(:comic)
+  end
+  
+  scope :by_comic, -> (comic_id) do 
+    where(comic_id: comic_id)
+  end
+  
+  scope :find_by_comic, -> (comic_id) do 
+    find_index.by_comic(comic_id)
+  end
+  
+  scope :find_by_story, -> (story_id) do 
+    find_index.where(story_id: story_id).references(:comic)
+  end
+  
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(Comic.arel_table[:author_id].eq author_id).references(:comic)
+  end
+  
+  scope :find_play, -> (comic_id) do 
+    find_by_comic(comic_id)
+  end
+  
+  scope :find_private_play, -> (comic_id, operators) do 
+   find_private(operators).by_comic(comic_id)
+  end
+  
   def supply_default
     self.comic_id = nil
     self.story_id = nil
@@ -16,8 +51,6 @@ class ComicStory < Peta::Leaf
   end
   
   def overwrite operators
-    return false unless operators.author
-    self.author_id = operators.author.id
   end
   
   def disp_t
@@ -32,7 +65,7 @@ class ComicStory < Peta::Leaf
     self.disp_t_by_text + ':' + self.story.title
   end
   
-  def self.public_list_where
+  def self.public_list_where list
     'comics.visible > 0'
   end
   
@@ -47,6 +80,7 @@ class ComicStory < Peta::Leaf
   def self.show_opt
     {:include => {
       :comic => {
+        :author => {}
       }
     }}
   end
@@ -56,25 +90,4 @@ class ComicStory < Peta::Leaf
     self.comic.own?(operators) and self.story.own?(operators)
   end
   
-  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.comic_id) 
-      unless res
-        self.errors.add :t, 'unserialized'
-        raise ActiveRecord::Rollback 
-      end
-    end
-    res
-  end
-  
 end