OSDN Git Service

merge
[pettanr/pettanr.git] / app / models / comic_story.rb
index 9e2fd28..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,11 +65,7 @@ class ComicStory < Peta::Leaf
     self.disp_t_by_text + ':' + self.story.title
   end
   
-  def self.public_list_order
-    'comic_stories.updated_at desc'
-  end
-  
-  def self.list_where
+  def self.public_list_where list
     'comics.visible > 0'
   end
   
@@ -48,33 +77,10 @@ class ComicStory < Peta::Leaf
     }
   end
   
-  def self.play_list_where cid
-    ['comic_stories.comic_id = ?', cid]
-  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.list_opt
-    {
-      :comic => {
-        :author => {}, 
-      }
-    }
-  end
-  
-  def self.list_json_opt
-    {:include => {
-      :comic => {
-        :author => {}, 
-      }
-    }}
-  end
-  
   def self.show_opt
     {:include => {
       :comic => {
+        :author => {}
       }
     }}
   end
@@ -84,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