OSDN Git Service

work
[pettanr/pettanr.git] / app / models / story_sheet.rb
index 2ab6e17..65be87a 100644 (file)
@@ -1,4 +1,4 @@
-class StorySheet < ActiveRecord::Base
+class StorySheet < Pettanr::Content
   belongs_to :author
   belongs_to :story
   belongs_to :sheet
@@ -8,92 +8,48 @@ class StorySheet < ActiveRecord::Base
   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
   
+  def self.owner_model
+    Story
+  end
+  
   def supply_default
     self.story_id = nil
     self.sheet_id = nil
     self.t = nil
   end
   
-  def overwrite au
-    return false unless au
-    self.author_id = au.id
-  end
-  
-  def own? roles
-    roles = [roles] unless roles.respond_to?(:each)
-    au = StorySheet.get_author_from_roles roles
-    return false unless au
-    self.author_id == au.id
-  end
-  
-  def visible? roles
-    if MagicNumber['run_mode'] == 0
-      return false unless guest_role_check(roles)
-    else
-      return false unless reader_role_check(roles)
-    end
-    return true if self.story.own?(roles)
-    self.story.visible? roles
-  end
-  
-  def self.default_sheet_size
-    30
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
   end
   
-  def self.max_sheet_size
-    200
+  def visible? operators
+    return false unless super
+    self.owner_model.visible? operators
   end
   
-  def self.offset cnt, prm = nil
-    offset = prm.to_i
-    offset = cnt - 1 if offset >= cnt
-    offset = cnt - offset.abs if offset < 0
-    offset = 0 if offset < 0
-    offset
+  def self.list_where
+    'stories.visible > 0'
   end
   
-  def self.default_page_size
-    25
+  def self.list_order
+    'story_sheets.updated_at desc'
   end
   
-  def self.max_page_size
-    100
+  def self.play_sheet_where sid
+    ['story_sheets.story_id = ?', sid]
   end
   
 def self.page prm = nil
+ def self.page prm = nil
     page = prm.to_i
     page = 1 if page < 1
     page
   end
-  
-  def self.page_size prm = self.default_page_size
-    page_size = prm.to_i
-    page_size = self.max_page_size if page_size > self.max_page_size
-    page_size = self.default_page_size if page_size < 1
-    page_size
-  end
-  
-  def self.play_sheet_where sid
-    ['story_sheets.story_id = ?', sid]
-  end
-  
-  def self.list_where
-    'stories.visible > 0'
-  end
-  
-  def self.mylist_where au
-    ['story_sheets.author_id = ?', au.id]
-  end
-  
-  def self.himlist_where au
-    ['story_sheets.author_id = ? and stories.visible > 0', au.id]
-  end
-  
-  def self.play_sheet story, au, page = 1
+  def self.play_sheet story, operators, page = 1
     ss = StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).order('story_sheets.t').offset(page -1).limit(1).first
     if ss 
       if ss.sheet
-        Sheet.show(ss.sheet.id, au)
+        Sheet.show(ss.sheet.id, operators)
       else
         nil
       end
@@ -102,30 +58,6 @@ class StorySheet < ActiveRecord::Base
     end
   end
   
-  def self.list page = 1, page_size = self.default_page_size
-    StorySheet.where(self.list_where()).includes(StorySheet.list_opt).order('story_sheets.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.mylist au, page = 1, page_size = Author.default_story_sheet_page_size
-    StorySheet.where(self.mylist_where(au)).includes(StorySheet.list_opt).order('story_sheets.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.himlist au, page = 1, page_size = Author.default_story_sheet_page_size
-    StorySheet.where(self.himlist_where(au)).includes(StorySheet.list_opt).order('story_sheets.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(StorySheet.where(self.list_where()).includes(StorySheet.list_opt).count, nil)).page(page).per(page_size)
-  end
-  
-  def self.mylist_paginate au, page = 1, page_size = Author.default_story_sheet_page_size
-    Kaminari.paginate_array(Array.new(StorySheet.where(self.mylist_where(au)).includes(StorySheet.list_opt).count, nil)).page(page).per(page_size)
-  end
-  
-  def self.himlist_paginate au, page = 1, page_size = Author.default_story_sheet_page_size
-    Kaminari.paginate_array(Array.new(StorySheet.where(self.himlist_where(au)).includes(StorySheet.list_opt).count, nil)).page(page).per(page_size)
-  end
-  
   def self.play_paginate story, page
     Kaminari.paginate_array(Array.new(StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).count, nil)).page(page).per(1)
   end
@@ -149,22 +81,6 @@ class StorySheet < ActiveRecord::Base
     }}
   end
   
-  def self.show spid, roles
-    opt = {}
-    opt.merge!(StorySheet.show_opt)
-    res = StorySheet.find spid, opt
-    raise ActiveRecord::Forbidden unless res.visible?(roles)
-    res
-  end
-  
-  def self.edit spid, au
-    opt = {}
-    opt.merge!(StorySheet.show_opt)
-    res = StorySheet.find spid, opt
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
-  end
-  
   def self.show_opt
     {:include => {
       :author => {}, 
@@ -274,15 +190,15 @@ class StorySheet < ActiveRecord::Base
     end
   end
   
-  def allow?
+  def allow? operators
     return nil if self.story_id == nil or self.sheet_id == nil
-    self.story.own?(self.author) and self.sheet.usable?(self.author)
+    self.story.own?(operators) and self.sheet.usable?(operators)
   end
   
-  def store old_t = nil
+  def store operators, old_t = nil
     res = false
     StorySheet.transaction do
-      case self.allow?
+      case self.allow? operators
       when true
         self.rotate old_t
       when false