OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / scroll_panel.rb
index 8cfc3aa..dbc4de2 100644 (file)
@@ -6,9 +6,44 @@ class ScrollPanel < Peta::Leaf
   
   validates :scroll_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :panel_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_scroll, -> do
+    includes(:scroll)
+  end
+  
+  scope :find_index, -> do
+    with_scroll.where(Scroll.arel_table[:visible].gt 0).references(:scroll)
+  end
+  
+  scope :find_private, -> (operators) do 
+    with_scroll.where(Scroll.arel_table[:author_id].eq operators.author.id).references(:scroll)
+  end
+  
+  scope :by_scroll, -> (scroll_id) do 
+    where(scroll_id: scroll_id)
+  end
+  
+  scope :find_by_scroll, -> (scroll_id) do 
+    find_index.by_scroll(scroll_id).references(:scroll)
+  end
+  
+  scope :find_by_panel, -> (panel_id) do 
+    find_index.where(panel_id: panel_id).references(:scroll)
+  end
+  
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(Scroll.arel_table[:author_id].eq author_id).references(:scroll)
+  end
+  
+  scope :find_play, -> (scroll_id) do 
+    find_by_scroll(scroll_id)
+  end
+  
+  scope :find_private_play, -> (scroll_id, operators) do 
+    find_private(operators).by_scroll(scroll_id)
+  end
+  
   def supply_default
     self.scroll_id = nil
     self.panel_id = nil
@@ -16,66 +51,26 @@ class ScrollPanel < Peta::Leaf
   end
   
   def overwrite operators
-    return false unless operators.author
-    self.author_id = operators.author.id
-  end
-  
-  def self.public_list_order
-    'scroll_panels.updated_at desc'
   end
   
-  def self.public_list_where
+  def self.public_list_where list
     'scrolls.visible > 0'
   end
   
-  def self.list_opt
-    {
-      :author => {}, 
-      :scroll => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }
-  end
-  
-  def self.list_json_opt
-    {:include => {
-      :author => {}, 
-      :scroll => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }}
+  def self.by_author_list_includes
+    [:scroll => :author]
   end
   
   def self.show_opt
     {:include => {
-      :author => {}, 
       :scroll => {
         :author => {}
       }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
     }}
   end
   
   def self.licensed_pictures scroll_panels
-    r = {}
-    scroll_panels.each do |scroll_panel|
-      r.merge!(scroll_panel.panel.licensed_pictures) if scroll_panel.panel
-    end
-    r
+    Panel.licensed_pictures scroll_panels.select {|sp| sp.panel }.map {|sp| sp.panel }
   end
   
   def allow? operators
@@ -83,25 +78,4 @@ class ScrollPanel < Peta::Leaf
     self.scroll.own?(operators) and self.panel.usable?(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.scroll_id) 
-      unless res
-        self.errors.add :t, 'unserialized'
-        raise ActiveRecord::Rollback 
-      end
-    end
-    res
-  end
-  
 end