OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / scroll_panel.rb
index 8153e6d..dbc4de2 100644 (file)
@@ -1,4 +1,4 @@
-class ScrollPanel < Peta::Content
+class ScrollPanel < Peta::Leaf
   load_manifest
   belongs_to :author
   belongs_to :panel
@@ -6,181 +6,71 @@ class ScrollPanel < Peta::Content
   
   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}
   
-  def tag_attributes column = nil, opt = {}
-    {
-    }
+  scope :with_scroll, -> do
+    includes(:scroll)
   end
   
-  def supply_default
-    self.scroll_id = nil
-    self.panel_id = nil
-    self.t = nil
+  scope :find_index, -> do
+    with_scroll.where(Scroll.arel_table[:visible].gt 0).references(:scroll)
   end
   
-  def overwrite operators
-    return false unless operators.author
-    self.author_id = operators.author.id
+  scope :find_private, -> (operators) do 
+    with_scroll.where(Scroll.arel_table[:author_id].eq operators.author.id).references(:scroll)
   end
   
-  def visible? operators
-    return false unless super
-    self.owner_model.visible? operators
+  scope :by_scroll, -> (scroll_id) do 
+    where(scroll_id: scroll_id)
   end
   
-  def self.list_order
-    'scroll_panels.updated_at desc'
+  scope :find_by_scroll, -> (scroll_id) do 
+    find_index.by_scroll(scroll_id).references(:scroll)
   end
   
-  def self.list_where
-    'scrolls.visible > 0'
+  scope :find_by_panel, -> (panel_id) do 
+    find_index.where(panel_id: panel_id).references(:scroll)
   end
   
-  def self.play_list_where cid
-    ['scroll_panels.scroll_id = ?', cid]
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(Scroll.arel_table[:author_id].eq author_id).references(:scroll)
   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)
+  scope :find_play, -> (scroll_id) do 
+    find_by_scroll(scroll_id)
   end
   
-  def self.list_opt
-    {
-      :author => {}, 
-      :scroll => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }
+  scope :find_private_play, -> (scroll_id, operators) do 
+    find_private(operators).by_scroll(scroll_id)
   end
   
-  def self.list_json_opt
-    {:include => {
-      :author => {}, 
-      :scroll => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }}
+  def supply_default
+    self.scroll_id = nil
+    self.panel_id = nil
+    self.t = nil
   end
   
-  def self.show_opt
-    {:include => {
-      :author => {}, 
-      :scroll => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }}
+  def overwrite operators
   end
   
-  def elements
-    self.panel.elements
+  def self.public_list_where list
+    'scrolls.visible > 0'
   end
   
-  def scroll_panel_as_json au
-    panel_include = if self.panel and self.panel.visible?(au)
-      {:include => {:author => {}}, :methods => :elements}
-    else
-      {:include => {:author => {}}}
-    end
-    self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
+  def self.by_author_list_includes
+    [:scroll => :author]
   end
   
-  def self.list_as_json_text ary, au
-    '[' + ary.map {|i| i.scroll_panel_as_json(au) }.join(',') + ']'
+  def self.show_opt
+    {:include => {
+      :scroll => {
+        :author => {}
+      }, 
+    }}
   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
-  end
-  
-  def self.new_t scroll_id
-    r = ScrollPanel.max_t(scroll_id)
-    r.blank? ? 0 : r.to_i + 1
-  end
-  
-  def self.max_t scroll_id
-    ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id])
-  end
-  
-  def self.find_t scroll_id, t
-    ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t])
-  end
-  
-  def self.collect_t scroll_panel
-    r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
-    r.map {|sp| sp.t}
-  end
-  
-  def self.serial? ary
-    i = 0
-    ary.compact.sort.each do |t|
-      break false unless t == i
-      i += 1
-    end
-    ary.compact.size == i
-  end
-  
-  def self.validate_t scroll_panel
-    ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
-  end
-  
-  def insert_shift
-    ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
-  end
-  
-  def lesser_shift old_t
-    self.t = 0 if self.t < 0
-    ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
-  end
-  
-  def higher_shift old_t
-    nf = ScrollPanel.find_t(self.scroll_id, self.t)
-    max_t = ScrollPanel.max_t(self.scroll_id).to_i
-    self.t = max_t if self.t > max_t
-    ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
-  end
-  
-  def update_shift old_t
-    if self.t > old_t
-      higher_shift old_t
-    else
-      lesser_shift old_t
-    end
-  end
-  
-  def rotate old_t = nil
-    if self.new_record?
-      if self.t.blank?
-        self.t = ScrollPanel.new_t self.scroll_id
-      else
-        self.insert_shift
-      end
-    else
-      if self.t.blank?
-      else
-        self.update_shift old_t
-      end
-    end
+    Panel.licensed_pictures scroll_panels.select {|sp| sp.panel }.map {|sp| sp.panel }
   end
   
   def allow? operators
@@ -188,35 +78,4 @@ class ScrollPanel < Peta::Content
     self.scroll.own?(operators) and self.panel.usable?(operators)
   end
   
-  def store operators, old_t = nil
-    res = false
-    ScrollPanel.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 = ScrollPanel.validate_t(self) 
-      unless res
-        self.errors.add :t, 'unserialized'
-        raise ActiveRecord::Rollback 
-      end
-    end
-    res
-  end
-  
-  def destroy_and_shorten
-    res = false
-    ScrollPanel.transaction do
-      ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
-      raise ActiveRecord::Rollback unless self.destroy
-      res = true
-    end
-    res
-  end
-  
 end