OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / app / models / author.rb
index 3fd96ec..b3f96f6 100644 (file)
@@ -3,10 +3,21 @@ class Author < ActiveRecord::Base
   belongs_to :user
   has_many :comics
   has_many :panels
+  belongs_to :working_panel, :class_name => "Panel"
   
   validates :name, :presence => true, :length => {:maximum => 30}
+  validates :working_panel_id, :numericality => {:allow_nil => true}
   validates :user_id, :numericality => true, :existence => {:both => false}
   
+  before_validation :valid_encode
+  
+  def valid_encode
+    ['name'].each do |a|
+      next if attributes[a] == nil
+      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+    end
+  end
+  
   def supply_default
     self.name = 'no name' if self.name.blank?
   end
@@ -35,6 +46,10 @@ class Author < ActiveRecord::Base
     Artist.find_by_author(self) != nil
   end
   
+  def working?
+    self.working_panel_id and self.working_panel
+  end
+  
   def step2 n
     self.name = n
     self.save
@@ -62,15 +77,15 @@ class Author < ActiveRecord::Base
   end
   
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(Author.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:order => 'created_at desc'})
-    Author.find(:all, opt)
+    Author.includes(Author.list_opt).order('authors.created_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(Author.count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
-    {:include => {:artist => {}} }
+    {:artist => {} }
   end
   
   def self.list_json_opt