OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / scroll.rb
index 17a5c5a..3a50002 100644 (file)
@@ -3,10 +3,42 @@ class Scroll < Peta::Binder
   has_many :scroll_panels
   belongs_to :author
   
+  has_many :panels, :through => :scroll_panels
+  
   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_panels, -> do
+    includes(scroll_panels: :panel)
+  end
+  
+  scope :find_by_panel, -> (panel_id) do 
+    with_panels.find_index.where(Panel.arel_table[:id].eq panel_id).references(:panel)
+  end
+  
+  # scope of find_play
+  def self.find_play(id)
+    ScrollPanel.find_play(id)
+  end
+  
+  # scope of find_private_play
+  def self.find_private_play(id, operators)
+    ScrollPanel.find_private_play(id, operators)
+  end
+  
   def supply_default
     self.visible = 0 if self.visible.blank?
     self.author_id = nil
@@ -29,7 +61,7 @@ class Scroll < Peta::Binder
     end
   end
   
-  def self.public_list_where
+  def self.public_list_where list
     'scrolls.visible > 0'
   end