OSDN Git Service

t#32046:add sheet
[pettanr/pettanr.git] / app / models / story.rb
index 0fa3733..7fadd62 100644 (file)
@@ -1,30 +1,35 @@
 #ストーリー
 class Story < ActiveRecord::Base
-  belongs_to :author
-  belongs_to :panel
+  has_many :story_sheets
   belongs_to :comic
   
   validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :panel_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :author_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 :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
-    self.panel_id = nil
+    self.visible = 0 if self.visible.blank?
     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
@@ -33,67 +38,12 @@ class Story < ActiveRecord::Base
     else
       return false unless reader_role_check(roles)
     end
-    return true if self.comic.own?(roles)
-    self.comic.visible? roles
-  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
+    return true if self.own?(roles)
+    self.visible > 0
   end
   
-  def self.play_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 = ?', 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 => {}}
-      }
-    }}
+  def disp_t
+    self.t + 1
   end
   
   def self.default_page_size
@@ -104,6 +54,14 @@ class Story < ActiveRecord::Base
     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
@@ -117,34 +75,54 @@ class Story < ActiveRecord::Base
     page_size
   end
   
+  def self.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
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['comics.visible > 0'], :order => 'stories.updated_at desc'})
-    Story.find(:all, opt)
+    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
-    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)
+    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
-    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 = ? and comics.visible > 0', au.id], :order => 'stories.updated_at desc'})
-    Story.find(:all, opt)
+    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)
+  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
+    res = Story.find(sid, opt)
     raise ActiveRecord::Forbidden unless res.visible?(roles)
     res
   end
@@ -152,48 +130,33 @@ class Story < ActiveRecord::Base
   def self.edit sid, au
     opt = {}
     opt.merge!(Story.show_opt)
-    res = Story.find sid, opt
+    res = Story.find(sid, opt)
     raise ActiveRecord::Forbidden unless res.own?(au)
     res
   end
   
   def self.show_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
-  end
-  
-  def elements
-    self.panel.elements
-  end
-  
-  def story_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 => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
+    {:include => {:comic => {:author => {}} }}
+  end
+  
+  def self.show_json_opt
+    {:include => {:comic => {:include => {:author => {}}} }}
   end
   
-  def self.list_as_json_text ary, au
-    '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
+  def self.visible_count
+    Story.count 'visible > 0'
   end
   
-  def self.licensed_pictures stories
-    r = {}
-    stories.each do |story|
-      r.merge!(story.panel.licensed_pictures) if story.panel
+  def destroy_with_story_sheet
+    res = false
+    Story.transaction do
+      self.story_sheets.each do |story_sheet|
+        raise ActiveRecord::Rollback unless story_sheet.destroy
+      end
+      raise ActiveRecord::Rollback unless self.destroy
+      res = true
     end
-    r
+    res
   end
   
   def self.new_t comic_id
@@ -266,15 +229,15 @@ class Story < ActiveRecord::Base
     end
   end
   
-  def allow?
-    return nil if self.comic_id == nil or self.panel_id == nil
-    self.comic.own?(self.author) and self.panel.usable?(self.author)
+  def allow? au
+    return nil if self.comic_id == nil
+    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
@@ -296,7 +259,7 @@ class Story < ActiveRecord::Base
     res = false
     Story.transaction do
       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
-      raise ActiveRecord::Rollback unless self.destroy
+      raise ActiveRecord::Rollback unless self.destroy_with_story_sheet
       res = true
     end
     res