OSDN Git Service

v07
[pettanr/pettanr.git] / app / models / story.rb
index 6284c56..cd02e7f 100644 (file)
@@ -3,17 +3,53 @@ class Story < Peta::Binder
   load_manifest
   has_many :comic_stories
   has_many :story_sheets
-  has_many :sheets, :through => :story_sheets
-  has_many :play_story_sheets, :class_name => 'StorySheet', :order => 't'
-  has_many :play_sheets, :source => 'Sheet', :through => :play_story_sheets
   belongs_to :author
   
   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}
   
+  scope :find_index, -> do
+    where(arel_table[:visible].gt 0)
+  end
+  
+  scope :find_private, -> (operators) do 
+    where(author_id: operators.author.id)
+  end
+  
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(author_id: author_id)
+  end
+  
+  scope :with_comics, -> do
+    includes(comic_stories: :comic)
+  end
+  
+  scope :find_by_comic, -> (comic_id) do 
+    with_comics.find_index.where(Comic.arel_table[:id].eq comic_id).references(:comic)
+  end
+  
+  scope :with_sheets, -> do
+    includes(story_sheets: :sheet)
+  end
+  
+  scope :find_by_sheet, -> (sheet_id) do 
+    with_sheets.find_index.where(Sheet.arel_table[:id].eq sheet_id).references(:sheet)
+  end
+  
+  # scope of find_play
+  def self.find_play(id)
+    StorySheet.find_play(id)
+  end
+  
+  # scope of find_private_play
+  def self.find_private_play(id, operators)
+    StorySheet.find_private_play(id, operators)
+  end
+  
   def supply_default
     self.visible = 0 if self.visible.blank?
+    self.author_id = nil
   end
   
   def overwrite operators
@@ -33,28 +69,12 @@ class Story < Peta::Binder
     end
   end
   
-  def story_sheets_count
-    StorySheet.where(['story_sheets.story_id = ?', self.id]).count
-  end
-  
-  def self.public_list_where
+  def self.public_list_where list
     'stories.visible > 0'
   end
   
-  def self.list_opt
-    {:comic_stories => {:comic => {}}, :author => {} }
-  end
-  
-  def self.list_json_opt
-    {:include => {:comic_stories => {:include => {:comic => {}}}, :author => {}}}
-  end
-  
   def self.show_opt
-    {:include => {:comic_stories => {:comic => {}}, :author => {}}}
-  end
-  
-  def self.show_json_opt
-    {:include => {:comic_stories => {:include => {:comic => {}}}, :author => {}}}
+    {:include => {:story_sheets => {:sheet => {}}, :author => {}}}
   end
   
 end