OSDN Git Service

merge
[pettanr/pettanr.git] / app / models / artist.rb
index 63a1f20..6c97a5e 100644 (file)
-class Artist < ActiveRecord::Base
-  belongs_to :author
+class Artist < Peta::Owner
+  load_manifest
+  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_id, :numericality => {:allow_nil => true}
   
-  def supply_default
-    self.name = 'no name' if self.name.blank?
-  end
-  
-  def overwrite au
-    return false unless au
-    self.author_id = au.id
+  scope :find_index, -> do
+    self
   end
   
-  def own? author
-    return false unless author
-    self.author_id == author.id
-  end
-  
-  def visible? author
-    true
+  def supply_default
+    self.name = 'no name' if self.name.blank?
+    self.user_id = nil
   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
+    Artist.where(author_id: author.id).first
   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 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)
-  end
-  
-  def self.list_opt
-    {:include => {:author => {}, :original_pictures => {}, :pictures => {}, :resource_pictures => {}} }
-  end
-  
-  def self.list_json_opt
-    {:include => {:author => {}, :original_pictures => {}, :pictures => {}, :resource_pictures => {}} }
-  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
+  def self.index_list_where list
+    'artists.provider_id is null'
   end
   
   def self.show_opt
-    {:include => {:author => {}, :original_pictures => {}, :pictures => {}, :resource_pictures => {}} }
-  end
-  
-  def self.show_json_opt
-    {:include => {:author => {}, :original_pictures => {}, :pictures => {}, :resource_pictures => {}} }
+    {:include => {:user => {:author => {}}} }
   end
   
-  def self.visible_count
-    Artist.count
+  def self.export(dt = nil)
+    artists = Artist.where('artists.provider = 0')
+    artists = artists.where(['artists.updated_at >= ?', dt]) if dt
+    artists.order(:id)
   end
   
 end