OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / sheet.rb
index 9ff4b81..6d5360e 100644 (file)
@@ -10,6 +10,44 @@ class Sheet < Peta::Root
   validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
   accepts_nested_attributes_for :sheet_panels, :allow_destroy => true
   
+  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(sheet_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 :with_stories, -> do
+    includes(story_sheets: :story)
+  end
+  
+  scope :find_by_story, -> (story_id) do 
+    with_stories.find_index.where(Story.arel_table[:id].eq story_id).references(:story)
+  end
+  
+  # scope of find_play
+  def self.find_play(id)
+    SheetPanel.find_play(id)
+  end
+  
+  # scope of find_private_play
+  def self.find_private_play(id, operators)
+    SheetPanel.find_private_play(id, operators)
+  end
+  
   def supply_default
     self.visible = 0 if self.visible.blank?
     self.author_id = nil
@@ -38,7 +76,7 @@ class Sheet < Peta::Root
   def symbol_filename
   end
   
-  def self.public_list_where
+  def self.public_list_where list
     'sheets.visible > 0'
   end
   
@@ -72,28 +110,12 @@ class Sheet < Peta::Root
   
   def copyable?
     r = true
-    Sheet.each_element_class_names do |n|
-      self.elements_by_class_name(n).each do |elm|
-        next if elm.copyable?
-        r = false
-        break
-      end
-      break unless r
+    self.elements_items.each do |elm|
+      next if elm.copyable?
+      r = false
+      break
     end
     r
   end
   
-  def copy
-    attr = self.copy_attributes
-    Sheet.each_element_class_names do |n|
-      element_attr = Sheet.class_name_to_class(n).panelize(
-        self.elements_by_class_name(n).map {|elm|
-          elm.copy_attributes.merge elm.panel_attributes
-        }
-      )
-      attr.merge! element_attr
-    end
-    attr
-  end
-  
 end