OSDN Git Service

t#32046:add sheet
[pettanr/pettanr.git] / app / models / story_sheet.rb
index 699b444..2ab6e17 100644 (file)
@@ -73,7 +73,7 @@ class StorySheet < ActiveRecord::Base
     page_size
   end
   
-  def self.play_list_where sid
+  def self.play_sheet_where sid
     ['story_sheets.story_id = ?', sid]
   end
   
@@ -90,8 +90,16 @@ class StorySheet < ActiveRecord::Base
   end
   
   def self.play_sheet story, au, page = 1
-    ss = StorySheet.where(self.play_list_where(story.id)).includes(StorySheet.list_opt).order('story_sheets.t').offset(page -1).limit(1).first
-    ss ? ss.sheet : nil
+    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
@@ -119,14 +127,13 @@ class StorySheet < ActiveRecord::Base
   end
   
   def self.play_paginate story, page
-    Kaminari.paginate_array(Array.new(StorySheet.where(self.play_list_where(story.id)).includes(StorySheet.list_opt).count, nil)).page(page).per(1)
+    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_opt
     {
       :author => {}, 
       :story => {
-        :author => {}
       }, 
       :sheet => {
         :author => {},
@@ -137,7 +144,7 @@ class StorySheet < ActiveRecord::Base
   def self.list_json_opt
     {:include => {
       :author => {}, 
-      :story => {:include => {:author => {}}} ,
+      :story => {} ,
       :sheet => {:include => {:author => {}}} 
     }}
   end
@@ -161,9 +168,7 @@ class StorySheet < ActiveRecord::Base
   def self.show_opt
     {:include => {
       :author => {}, 
-      :story => {
-        :author => {}
-      }, 
+      :story => {}, 
       :sheet => {
         :author => {} 
       }
@@ -173,11 +178,28 @@ 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