OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / app / models / comic.rb
index 13ef9c7..738d438 100644 (file)
@@ -4,29 +4,40 @@ class Comic < ActiveRecord::Base
   belongs_to :author
   
   validates :title, :presence => true, :length => {:maximum => 100}
-  validates :width, :presence => true, :numericality => true, :natural_number => true
-  validates :height, :presence => true, :numericality => true, :natural_number => true
-  validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..3}
+  validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
+  before_validation :valid_encode
   
-  before_save do |r|
-    r.supply_default
+  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.visible = 0 if self.visible.blank?
   end
   
-  def own? author
-    return false unless author
-    self.author_id == author.id
+  def overwrite au
+    return false unless au
+    self.author_id = au.id
   end
   
-  def visible? author
-    self.visible > 0 or self.own?(author)
+  def own? roles
+    roles = [roles] unless roles.respond_to?(:each)
+    au = Comic.get_author_from_roles roles
+    return false unless au
+    self.author_id == au.id
   end
   
-  def disp_visible
-    visible == 1 ? 'O' : 'X'
+  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)
+    self.visible > 0
   end
   
   def self.default_page_size
@@ -58,44 +69,100 @@ class Comic < ActiveRecord::Base
     page_size
   end
   
-  def self.list opt = {}, page = 1, page_size = self.default_page_size
-    opt.merge!(self.list_opt) unless opt[:include]
-    opt.merge!({:conditions => ['visible > 0'], :order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
-    Comic.find(:all, opt)
+  def self.list_where
+    'comics.visible > 0'
   end
   
-  def self.list_opt
-    {:include => {:stories => {:panel => {}}, :author => {}}}
+  def self.mylist_where au
+    ['comics.author_id = ?', au.id]
   end
   
-  def self.list_json_opt
-    {:include => {:stories => {:panel => {}}, :author => {}}}
+  def self.himlist_where au
+    ['comics.author_id = ? and comics.visible > 0', au.id]
+  end
+  
+  def self.list page = 1, page_size = self.default_page_size
+    Comic.where(self.list_where()).includes(Comic.list_opt).order('comics.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.mylist au, page = 1, page_size = Author.default_comic_page_size
+    Comic.where(self.mylist_where(au)).includes(Comic.list_opt).order('comics.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_comic_page_size
+    Comic.where(self.himlist_where(au)).includes(Comic.list_opt).order('comics.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(Comic.where(self.list_where()).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_comic_page_size
+    Kaminari.paginate_array(Array.new(Comic.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_comic_page_size
+    Kaminari.paginate_array(Array.new(Comic.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.list_opt
+    {:stories => {:panel => {}}, :author => {} }
   end
   
-  def self.mylist au, opt = {}, page = 1, comic_page_size = Author.default_comic_page_size
-    opt.merge!(self.list_opt) unless opt[:include]
-    opt.merge!({:conditions => ['comics.author_id = ?', au.id], :order => 'comics.updated_at desc', :limit => page_size, :offset => (page -1) * comic_page_size})
-    Comic.find(:all, opt)
+  def self.list_json_opt
+    {:include => {:stories => {:include => {:panel => {}}}, :author => {}}}
   end
   
-  def self.show cid, au, opt = {}
-    c = Comic.find(cid, :include => self.show_include_opt(opt))
-    raise ActiveRecord::Forbidden unless c.visible?(au)
-    c
+  def self.show cid, roles
+    opt = {}
+    opt.merge!(Comic.show_opt)
+    res = Comic.find(cid, opt)
+    raise ActiveRecord::Forbidden unless res.visible?(roles)
+    res
   end
   
-  def self.show_include_opt opt = {}
-    res = {:stories => {:panel => {}}, :author => {}}
-    res.push(opt[:include]) if opt[:include]
+  def self.edit cid, au
+    opt = {}
+    opt.merge!(Comic.show_opt)
+    res = Comic.find(cid, opt)
+    raise ActiveRecord::Forbidden unless res.own?(au)
     res
   end
   
-  def self.show_json_include_opt
+  def self.show_opt
     {:include => {:stories => {:panel => {}}, :author => {}}}
   end
   
+  def self.show_json_opt
+    {:include => {:stories => {:include => {:panel => {}}}, :author => {}}}
+  end
+  
   def self.visible_count
     Comic.count 'visible > 0'
   end
   
+  def destroy_with_story
+    res = false
+    Comic.transaction do
+      self.stories.each do |story|
+        raise ActiveRecord::Rollback unless story.destroy
+      end
+      raise ActiveRecord::Rollback unless self.destroy
+      res = true
+    end
+    res
+  end
+  
+  def scenario
+    panels.map {|panel|
+      panel.scenario
+    }.join
+  end
+  
+  def plain_scenario
+    panels.map {|panel|
+      panel.plain_scenario
+    }.join
+  end
+  
 end