OSDN Git Service

t#32046:add sheet
[pettanr/pettanr.git] / app / models / story.rb
index b94b11d..7fadd62 100644 (file)
@@ -1,13 +1,11 @@
 #ストーリー
 class Story < ActiveRecord::Base
-  belongs_to :author
   has_many :story_sheets
   belongs_to :comic
   
   validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :title, :presence => true, :length => {:maximum => 100}
   validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
-  validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
   before_validation :valid_encode
   
@@ -24,16 +22,14 @@ class Story < ActiveRecord::Base
     self.t = nil
   end
   
-  def overwrite au
-    return false unless au
-    self.author_id = au.id
+  def overwrite
   end
   
   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
+    self.comic.author_id == au.id
   end
   
   def visible? roles
@@ -84,11 +80,11 @@ class Story < ActiveRecord::Base
   end
   
   def self.mylist_where au
-    ['stories.author_id = ?', au.id]
+    ['comics.author_id = ?', au.id]
   end
   
   def self.himlist_where au
-    ['stories.author_id = ? and stories.visible > 0', au.id]
+    ['comics.author_id = ? and stories.visible > 0', au.id]
   end
   
   def self.list page = 1, page_size = self.default_page_size
@@ -104,23 +100,23 @@ class Story < ActiveRecord::Base
   end
   
   def self.list_paginate page = 1, page_size = self.default_page_size
-    Kaminari.paginate_array(Array.new(Story.where(self.list_where()).count, nil)).page(page).per(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)).count, nil)).page(page).per(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)).count, nil)).page(page).per(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.list_opt
-    {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }
+    {:comic => {:author => {}} }
   end
   
   def self.list_json_opt
-    {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :author => {}}} }}
+    {:include => {:comic => {:include => {:author => {}}} }}
   end
   
   def self.show sid, roles
@@ -140,11 +136,11 @@ class Story < ActiveRecord::Base
   end
   
   def self.show_opt
-    {:include => {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }}
+    {:include => {:comic => {:author => {}} }}
   end
   
   def self.show_json_opt
-    {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :author => {}}} }}
+    {:include => {:comic => {:include => {:author => {}}} }}
   end
   
   def self.visible_count
@@ -233,15 +229,15 @@ class Story < ActiveRecord::Base
     end
   end
   
-  def allow?
+  def allow? au
     return nil if self.comic_id == nil
-    self.comic.own?(self.author)
+    self.comic.own?(au)
   end
   
-  def store old_t = nil
+  def store au, old_t = nil
     res = false
     Story.transaction do
-      case self.allow?
+      case self.allow? au
       when true
         self.rotate old_t
       when false