OSDN Git Service

t#31538:fix default value
[pettanr/pettanr.git] / app / models / artist.rb
index 63a1f20..bef4215 100644 (file)
@@ -7,6 +7,15 @@ class Artist < ActiveRecord::Base
   validates :name, :presence => true, :length => {:maximum => 30}
   validates :author_id, :numericality => {:allow_blank => true}
   
+  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
@@ -16,12 +25,19 @@ class Artist < ActiveRecord::Base
     self.author_id = au.id
   end
   
-  def own? author
-    return false unless author
-    self.author_id == author.id
+  def own? roles
+    roles = [roles] unless roles.respond_to?(:each)
+    au = Artist.get_author_from_roles roles
+    return false unless au
+    self.author_id == au.id
   end
   
-  def visible? author
+  def visible? roles
+    if MagicNumber['run_mode'] == 0
+      return false unless guest_role_check(roles)
+    else
+      return false unless resource_reader_role_check(roles)
+    end
     true
   end
   
@@ -58,20 +74,24 @@ class Artist < ActiveRecord::Base
     offset
   end
   
+  def self.list_where
+    'artists.author_id is not null'
+  end
+  
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(Artist.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:order => 'created_at desc'})
-    Artist.find(:all, opt)
+    Artist.where(self.list_where()).includes(Artist.list_opt).order('artists.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(Artist.where(self.list_where()).count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
-    {:include => {:author => {}, :original_pictures => {}, :pictures => {}, :resource_pictures => {}} }
+    {:author => {} }
   end
   
   def self.list_json_opt
-    {:include => {:author => {}, :original_pictures => {}, :pictures => {}, :resource_pictures => {}} }
+    {:include => {:author => {}} }
   end
   
   def self.show aid, au
@@ -91,15 +111,27 @@ class Artist < ActiveRecord::Base
   end
   
   def self.show_opt
-    {:include => {:author => {}, :original_pictures => {}, :pictures => {}, :resource_pictures => {}} }
+    {:include => {:author => {}} }
   end
   
   def self.show_json_opt
-    {:include => {:author => {}, :original_pictures => {}, :pictures => {}, :resource_pictures => {}} }
+    {:include => {:author => {}} }
   end
   
   def self.visible_count
-    Artist.count
+    Artist.count :conditions => ['artists.author_id is not null']
+  end
+  
+  def self.export(dt = nil)
+    opt = {}
+    cond = if dt
+      ['artists.author_id is not null and artists.updated_at >= ?', dt]
+    else
+      'artists.author_id is not null'
+    end
+    opt.merge!({:conditions => cond}) 
+    opt.merge!({:order => 'id'})
+    Artist.find(:all, opt)
   end
   
 end