OSDN Git Service

t#31521:fix story pager
[pettanr/pettanr.git] / app / models / story.rb
index b4c609c..cd67cc3 100644 (file)
@@ -20,20 +20,21 @@ class Story < ActiveRecord::Base
     self.author_id = au.id
   end
   
-  def own? au
-    return false unless au.is_a?(Author)
+  def own? roles
+    roles = [roles] unless roles.respond_to?(:each)
+    au = Story.get_author_from_roles roles
+    return false unless au
     self.author_id == au.id
   end
   
-  def visible? au
-    if au == nil
-      return false if MagicNumber['run_mode'] == 1
-    elsif au.is_a?(Author)
-      return true if self.comic.own?(au)
+  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.comic.visible? au
+    return true if self.comic.own?(roles)
+    self.comic.visible? roles
   end
   
   def self.default_panel_size
@@ -59,42 +60,6 @@ class Story < ActiveRecord::Base
     count
   end
   
-  def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
-    opt = {}
-    opt.merge!(Story.list_opt)
-    opt.merge!({:offset => offset, :limit => limit}) if limit > 0
-    opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t'})
-    Story.find(:all, opt)
-  end
-  
-  def self.list_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
-  end
-  
-  def self.list_json_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
-  end
-  
   def self.default_page_size
     25
   end
@@ -116,27 +81,83 @@ class Story < ActiveRecord::Base
     page_size
   end
   
+  def self.play_list_where cid
+    ['stories.comic_id = ?', cid]
+  end
+  
+  def self.list_where
+    'comics.visible > 0'
+  end
+  
+  def self.mylist_where au
+    ['stories.author_id = ?', au.id]
+  end
+  
+  def self.himlist_where au
+    ['stories.author_id = ? and comics.visible > 0', au.id]
+  end
+  
+  def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
+    Story.where(self.play_list_where(comic.id)).includes(Story.list_opt).order('stories.t').offset(offset).limit(limit)
+  end
+  
   def self.list page = 1, page_size = self.default_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 => ['comics.visible > 0'], :order => 'stories.updated_at desc'})
-    Story.find(:all, opt)
+    Story.where(self.list_where()).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
   end
   
   def self.mylist au, page = 1, page_size = Author.default_story_page_size
-    opt = {}
-    opt.merge!(Story.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'})
-    Story.find(:all, opt)
+    Story.where(self.mylist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_story_page_size
+    Story.where(self.himlist_where(au)).includes(Story.list_opt).order('stories.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(Story.where(self.list_where()).includes(Story.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
+    Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
+    Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
   end
   
-  def self.show sid, au
+  def self.list_opt
+    {
+      :author => {}, 
+      :comic => {
+        :author => {}
+      }, 
+      :panel => {
+        :author => {}, 
+        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
+        :speech_balloons =>{:balloon => {}, :speech => {}}
+      }
+    }
+  end
+  
+  def self.list_json_opt
+    {:include => {
+      :author => {}, 
+      :comic => {
+        :author => {}
+      }, 
+      :panel => {
+        :author => {}, 
+        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
+        :speech_balloons =>{:balloon => {}, :speech => {}}
+      }
+    }}
+  end
+  
+  def self.show sid, roles
     opt = {}
     opt.merge!(Story.show_opt)
     res = Story.find sid, opt
-    raise ActiveRecord::Forbidden unless res.visible?(au)
+    raise ActiveRecord::Forbidden unless res.visible?(roles)
     res
   end
   
@@ -157,7 +178,7 @@ class Story < ActiveRecord::Base
       :panel => {
         :author => {}, 
         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
+        :speech_balloons =>{:balloon => {}, :speech => {}}
       }
     }}
   end
@@ -258,16 +279,20 @@ class Story < ActiveRecord::Base
   end
   
   def allow?
-    c = self.comic
-    pl = self.panel
-    c and c.own?(self.author) and pl and pl.usable?(self.author)
+    return nil if self.comic_id == nil or self.panel_id == nil
+    self.comic.own?(self.author) and self.panel.usable?(self.author)
   end
   
   def store old_t = nil
     res = false
-    raise ActiveRecord::Forbidden unless self.allow?
     Story.transaction do
-      self.rotate old_t
+      case self.allow?
+      when true
+        self.rotate old_t
+      when false
+        raise ActiveRecord::Forbidden
+      else
+      end
       res = self.save
       raise ActiveRecord::Rollback unless res
       res = Story.validate_t(self)