-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? au
- if au.is_a?(Author)
- self.author_id == au.id
- elsif au.is_a?(Artist)
- self.id == au.id
- else
- false
- end
- end
-
- def visible? au
- if au == nil
- return false if MagicNumber['run_mode'] == 1
- elsif au.is_a?(Author)
- return true
- else
- return false
- end
- 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!({:conditions => ['artists.author_id is not null'], :order => 'artists.created_at desc'})
- Artist.find(:all, opt)
- end
-
- def self.list_opt
- {:include => {: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
+ def self.index_list_where list
+ 'artists.provider_id is null'
end
def self.show_opt
- {:include => {:author => {}} }
- end
-
- def self.show_json_opt
- {:include => {:author => {}} }
+ {:include => {:user => {:author => {}}} }
end
- def self.visible_count
- Artist.count :conditions => ['artists.author_id is not null']
+ 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