OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / artist.rb
index bef4215..955c8ff 100644 (file)
-class Artist < ActiveRecord::Base
-  belongs_to :author
+class Artist < Pettanr::Owner
+  belongs_to :user
   has_many :original_pictures
   has_many :pictures
   has_many :resource_pictures
   
   validates :name, :presence => true, :length => {:maximum => 30}
-  validates :author_id, :numericality => {:allow_blank => true}
+  validates :user_id, :numericality => true, :existence => {:both => false}
+  validates :provider, :numericality => 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
+  @@valid_encode_columns += ['name']
+  @@visible_count_options = {:conditions => ['artists.author_id is not null']}
   
   def supply_default
     self.name = 'no name' if self.name.blank?
   end
   
-  def overwrite au
-    return false unless au
-    self.author_id = au.id
-  end
-  
-  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? 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
-  
   def self.find_by_author author
     Artist.find( :first, :conditions => ['author_id = ?', author.id])
   end
   
-  def self.default_page_size
-    25
-  end
-  
-  def self.max_page_size
-    100
-  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.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.list_where
-    'artists.author_id is not null'
-  end
-  
-  def self.list page = 1, page_size = self.default_page_size
-    Artist.where(self.list_where()).includes(Artist.list_opt).order('artists.created_at desc').offset((page -1) * page_size).limit(page_size)
+    'artists.provider = 0'
   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)
+  def self.list_order
+    'artists.created_at desc'
   end
   
   def self.list_opt
-    {:author => {} }
+    {:user => {:author => {}} }
   end
   
   def self.list_json_opt
-    {:include => {:author => {}} }
-  end
-  
-  def self.show aid, au
-    opt = {}
-    opt.merge!(Artist.show_opt)
-    res = Artist.find(aid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(au)
-    res
-  end
-  
-  def self.edit sid, au
-    opt = {}
-    opt.merge!(Artist.show_opt)
-    res = Artist.find sid, opt
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
+    {:include => {:user => {:author => {}}} }
   end
   
   def self.show_opt
-    {:include => {:author => {}} }
+    {:include => {:user => {:author => {}}} }
   end
   
   def self.show_json_opt
-    {:include => {:author => {}} }
-  end
-  
-  def self.visible_count
-    Artist.count :conditions => ['artists.author_id is not null']
+    {:include => {:user => {:author => {}}} }
   end
   
   def self.export(dt = nil)
     opt = {}
     cond = if dt
-      ['artists.author_id is not null and artists.updated_at >= ?', dt]
+      ['artists.artists.provider = 0 and artists.updated_at >= ?', dt]
     else
-      'artists.author_id is not null'
+      'artists.artists.provider = 0'
     end
     opt.merge!({:conditions => cond}) 
     opt.merge!({:order => 'id'})