OSDN Git Service

t#29400:itr3?
[pettanr/pettanr.git] / app / models / story.rb
index 26575e6..df03c85 100644 (file)
@@ -19,7 +19,6 @@ class Story < ActiveRecord::Base
   end
   
   def own? author
-    return false unless author
     self.author_id == author.id
   end
   
@@ -29,6 +28,77 @@ class Story < ActiveRecord::Base
     res
   end
   
+  def self.default_panel_size
+    30
+  end
+  
+  def self.max_panel_size
+    200
+  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
+  end
+  
+  def self.panel_count cnt, prm = self.default_panel_size
+    count = prm.to_i
+    count = self.max_panel_size if count > self.max_panel_size
+    count = self.default_panel_size if count < 1
+    count
+  end
+  
+  def self.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 = ? and comics.visible > 0 and panels.publish > 0', 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.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)
+  end
+  
+  def to_json_list
+    self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})
+  end
+  
   def self.new_t comic_id
     r = Story.max_t(comic_id)
     r.blank? ? 0 : r.to_i + 1
@@ -128,66 +198,5 @@ class Story < ActiveRecord::Base
     end
   end
   
-  def self.default_panel_size
-    30
-  end
-  
-  def self.max_panel_size
-    200
-  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
-  end
-  
-  def self.panel_count cnt, prm = self.default_panel_size
-    count = prm.to_i
-    count = self.max_panel_size if count > self.max_panel_size
-    count = self.default_panel_size if count < 1
-    count
-  end
-  
-  def self.list comic, author, offset = 0, limit = Story.default_panel_size
-    opt = self.list_opt
-    opt.merge!({:conditions => ['stories.comic_id = ? and panels.publish > 0', comic.id], :order => 'stories.t', :offset => offset, :limit => limit})
-    Story.find(:all, opt)
-  end
-  
-  def self.list_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:resource_picture => {:artist => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
-  end
-  
-  def self.list_json_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:resource_picture => {:artist => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
-  end
-  
-  def to_json_list
-    self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})
-  end
-  
   
 end