OSDN Git Service

t#32471:add profiles
[pettanr/pettanr.git] / app / models / story_sheet.rb
index f988d29..0f493b1 100644 (file)
@@ -52,13 +52,6 @@ class StorySheet < ActiveRecord::Base
     offset
   end
   
-  def self.sheet_count cnt, prm = self.default_sheet_size
-    count = prm.to_i
-    count = self.max_sheet_size if count > self.max_sheet_size
-    count = self.default_sheet_size if count < 1
-    count
-  end
-  
   def self.default_page_size
     25
   end
@@ -80,8 +73,8 @@ class StorySheet < ActiveRecord::Base
     page_size
   end
   
-  def self.play_list_where cid
-    ['story_sheets.story_id = ?', cid]
+  def self.play_sheet_where sid
+    ['story_sheets.story_id = ?', sid]
   end
   
   def self.list_where
@@ -96,8 +89,17 @@ class StorySheet < ActiveRecord::Base
     ['story_sheets.author_id = ? and stories.visible > 0', au.id]
   end
   
-  def self.play_list story, author, offset = 0, limit = StorySheet.default_sheet_size
-    StorySheet.where(self.play_list_where(story.id)).includes(StorySheet.list_opt).order('story_sheets.t').offset(offset).limit(limit)
+  def self.play_sheet story, au, 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)
+      else
+        nil
+      end
+    else
+      nil
+    end
   end
   
   def self.list page = 1, page_size = self.default_page_size
@@ -124,11 +126,30 @@ class StorySheet < ActiveRecord::Base
     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
+  
+  def self.list_by_story_where story_id
+    ['story_sheets.story_id = ?', story_id]
+  end
+  
+  def self.list_by_story story_id, roles, page = 1, page_size = self.default_page_size
+    self.where(self.list_by_story_where(story_id)).includes(self.list_opt).order('story_sheets.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_by_sheet_where sheet_id
+    ['story_sheets.sheet_id = ?', sheet_id]
+  end
+  
+  def self.list_by_sheet sheet_id, roles, page = 1, page_size = self.default_page_size
+    self.where(self.list_by_sheet_where(sheet_id)).includes(self.list_opt).order('story_sheets.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
   def self.list_opt
     {
       :author => {}, 
       :story => {
-        :author => {}
       }, 
       :sheet => {
         :author => {},
@@ -139,7 +160,7 @@ class StorySheet < ActiveRecord::Base
   def self.list_json_opt
     {:include => {
       :author => {}, 
-      :story => {:include => {:author => {}}} ,
+      :story => {} ,
       :sheet => {:include => {:author => {}}} 
     }}
   end
@@ -163,9 +184,7 @@ class StorySheet < ActiveRecord::Base
   def self.show_opt
     {:include => {
       :author => {}, 
-      :story => {
-        :author => {}
-      }, 
+      :story => {}, 
       :sheet => {
         :author => {} 
       }
@@ -175,16 +194,37 @@ class StorySheet < ActiveRecord::Base
   def self.show_json_opt
     {:include => {
       :author => {}, 
-      :story => {:include => {:author => {}}} ,
+      :story => {} ,
       :sheet => {:include => {:author => {}}} 
     }}
   end
   
+  def elements
+    self.panel.elements
+  end
+  
+  def story_sheet_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}})
+  end
+  
+  def self.list_as_json_text ary, au
+    '[' + ary.map {|i| i.story_sheet_as_json(au) }.join(',') + ']'
+  end
+  
   def self.new_t story_id
     r = StorySheet.max_t(story_id)
     r.blank? ? 0 : r.to_i + 1
   end
   
+  def self.top_sheet story, author
+    StorySheet.play_list(story, author).first
+  end
+  
   def self.max_t story_id
     StorySheet.maximum(:t, :conditions => ['story_id = ?', story_id])
   end