OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / app / models / ground_picture.rb
index c481a1c..225db2d 100644 (file)
@@ -6,7 +6,7 @@ class GroundPicture < ActiveRecord::Base
   validates :repeat, :numericality => true, :inclusion => { :in => 0..3 }
   validates :x, :numericality => true
   validates :y, :numericality => true
-  validates :picture_id, :numericality => true, :existence => true
+  validates :picture_id, :numericality => true, :existence => {:both => false}
   validates :z, :presence => true, :numericality => {:greater_than => 0}
   
   def supply_default
@@ -18,16 +18,14 @@ class GroundPicture < ActiveRecord::Base
   def overwrite
   end
   
-  def visible? au
-    if au == nil
-      return false if MagicNumber['run_mode'] == 1
-    elsif au.is_a?(Author)
-    elsif au.is_a?(Admin)
-      return true
+  def visible? roles
+    if MagicNumber['run_mode'] == 0
+      return false unless guest_role_check(roles)
     else
-      return false
+      return false unless reader_role_check(roles)
     end
-    self.panel.publish?
+    return true if self.panel.own?(roles)
+    self.panel.visible? roles
   end
   
   def self.default_page_size
@@ -51,30 +49,50 @@ class GroundPicture < ActiveRecord::Base
     page_size
   end
   
+  def self.list_where
+    'panels.publish > 0'
+  end
+  
+  def self.mylist_where au
+    ['panels.author_id = ?', au.id]
+  end
+  
+  def self.himlist_where au
+    ['panels.author_id = ? and panels.publish > 0', au.id]
+  end
+  
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(GroundPicture.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'ground_pictures.updated_at desc'})
-    GroundPicture.find(:all, opt)
+    GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
+    GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
+    GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_paginate page = 1, page_size = self.default_page_size
+    Kaminari.paginate_array(Array.new(GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
+    Kaminari.paginate_array(Array.new(GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
+    Kaminari.paginate_array(Array.new(GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
-    {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
+    {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
   end
   
   def self.list_json_opt
     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
   end
   
-  def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'ground_pictures.updated_at desc'})
-    GroundPicture.find(:all, opt)
-  end
-  
   def self.show cid, au
     opt = {}
     opt.merge!(GroundPicture.show_opt)
@@ -91,4 +109,8 @@ class GroundPicture < ActiveRecord::Base
     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
   end
   
+  def scenario
+    ''
+  end
+  
 end