OSDN Git Service

v07
[pettanr/pettanr.git] / app / models / scroll.rb
index 291ff0c..3a50002 100644 (file)
-class Scroll < Pettanr::Item
+class Scroll < Peta::Binder
+  load_manifest
   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}
-
-  def self.singular
-    'Scroll'
-  end
   
-  def self.plural
-    'Scrolls'
+  scope :find_index, -> do
+    where(arel_table[:visible].gt 0)
   end
   
-  def self.owner_type
-    :author
+  scope :find_private, -> (operators) do 
+    where(author_id: operators.author.id)
   end
   
-  def self.valid_encode_columns
-    super.merge ['title']
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(author_id: author_id)
   end
   
-  def supply_default
-    self.visible = 0 if self.visible.blank?
+  scope :with_panels, -> do
+    includes(scroll_panels: :panel)
   end
   
-  def overwrite operators
-    return false unless operators.author
-    self.author_id = operators.author.id
+  scope :find_by_panel, -> (panel_id) do 
+    with_panels.find_index.where(Panel.arel_table[:id].eq panel_id).references(:panel)
   end
   
-  def own? roles
-    roles = [roles] unless roles.respond_to?(:each)
-    au = Scroll.get_author_from_roles roles
-    return false unless au
-    self.author_id == au.id
+  # scope of find_play
+  def self.find_play(id)
+    ScrollPanel.find_play(id)
   end
   
-  def visible? operators
-    return false unless super
-    self.visible > 0
-  end
-  
-  def scroll_panels_count
-    ScrollPanel.where(['scroll_panels.scroll_id = ?', self.id]).count
-  end
-  
-  def self.list_order
-    'scrolls.updated_at desc'
-  end
-  
-  def self.list_where
-    'scrolls.visible > 0'
+  # scope of find_private_play
+  def self.find_private_play(id, operators)
+    ScrollPanel.find_private_play(id, operators)
   end
   
-  def self.list_opt
-    {:scroll_panels => {:panel => {}}, :author => {} }
+  def supply_default
+    self.visible = 0 if self.visible.blank?
+    self.author_id = nil
   end
   
-  def self.list_json_opt
-    {:include => {:scroll_panels => {:include => {:panel => {}}}, :author => {}}}
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
+    super()
   end
   
-  def self.show sid, operators
-    opt = {}
-    opt.merge!(Scroll.show_opt)
-    res = Scroll.find(sid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(operators)
-    res
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
+    else
+      self.visible > 0
+    end
   end
   
-  def self.edit sid, au
-    opt = {}
-    opt.merge!(Scroll.show_opt)
-    res = Scroll.find(sid, opt)
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
+  def self.public_list_where list
+    'scrolls.visible > 0'
   end
   
   def self.show_opt
     {:include => {:scroll_panels => {:panel => {}}, :author => {}}}
   end
   
-  def self.show_json_opt
-    {:include => {:scroll_panels => {:include => {:panel => {}}}, :author => {}}}
-  end
-  
-  def self.visible_count
-    Scroll.count 'visible > 0'
-  end
-  
-  def destroy_with_scroll_panel
-    res = false
-    Scroll.transaction do
-      self.scroll_panels.each do |scroll_panel|
-        raise ActiveRecord::Rollback unless scroll_panel.destroy
-      end
-      raise ActiveRecord::Rollback unless self.destroy
-      res = true
-    end
-    res
-  end
-  
   def scenario
     panels.map {|panel|
       panel.scenario