OSDN Git Service

fix anything
[pettanr/pettanr.git] / app / models / story.rb
index 804c586..2d09d53 100644 (file)
@@ -1,5 +1,6 @@
 #ストーリー
-class Story < ActiveRecord::Base
+class Story < Peta::Binder
+  load_manifest
   has_many :story_sheets
   belongs_to :comic
   
@@ -7,14 +8,6 @@ class Story < ActiveRecord::Base
   validates :title, :presence => true, :length => {:maximum => 100}
   validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
-  before_validation :valid_encode
-  
-  def valid_encode
-    ['title', 'description'].each do |a|
-      next if attributes[a] == nil
-      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
-    end
-  end
   
   def supply_default
     self.comic_id = nil
@@ -25,20 +18,8 @@ class Story < ActiveRecord::Base
   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.comic.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.own?(roles)
+  def visible? operators
+    return false unless super
     self.visible > 0
   end
   
@@ -58,93 +39,28 @@ class Story < ActiveRecord::Base
     StorySheet.where(['story_sheets.story_id = ?', self.id]).count
   end
   
-  def self.default_page_size
-    25
-  end
-  
-  def self.max_page_size
-    100
-  end
-  
-  def self.default_panel_size
-    30
-  end
-  
-  def self.max_panel_size
-    200
-  end
-  
-  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.list_where
+  def self.public_list_where
     'stories.visible > 0'
   end
   
-  def self.mylist_where au
-    ['comics.author_id = ?', au.id]
-  end
-  
-  def self.himlist_where au
-    ['comics.author_id = ? and stories.visible > 0', au.id]
-  end
-  
-  def self.list page = 1, page_size = self.default_page_size
-    Story.where(self.list_where()).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.mylist au, page = 1, page_size = Author.default_story_page_size
-    Story.where(self.mylist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
-  end
-  
-  def self.himlist au, page = 1, page_size = Author.default_story_page_size
-    Story.where(self.himlist_where(au)).includes(Story.list_opt).order('stories.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(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)).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)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
+  def self.list_order
+    'stories.updated_at desc'
   end
   
   def self.list_opt
     {:comic => {:author => {}} }
   end
   
-  def self.list_json_opt
-    {:include => {:comic => {:include => {:author => {}}} }}
-  end
-  
-  def self.show sid, roles
-    opt = {}
-    opt.merge!(Story.show_opt)
-    res = Story.find(sid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(roles)
-    res
+  def self.by_author_list_includes
+    {
+      :comic => {
+        :author => {}
+      }
+    }
   end
   
-  def self.edit sid, au
-    opt = {}
-    opt.merge!(Story.show_opt)
-    res = Story.find(sid, opt)
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
+  def self.list_json_opt
+    {:include => {:comic => {:include => {:author => {}}} }}
   end
   
   def self.show_opt
@@ -241,15 +157,15 @@ class Story < ActiveRecord::Base
     end
   end
   
-  def allow? au
+  def allow? operators
     return nil if self.comic_id == nil
-    self.comic.own?(au)
+    self.comic.own?(operators)
   end
   
-  def store au, old_t = nil
+  def store operators, old_t = nil
     res = false
     Story.transaction do
-      case self.allow? au
+      case self.allow? operators
       when true
         self.rotate old_t
       when false