43 lines
999 B
Ruby
43 lines
999 B
Ruby
class Artist < ActiveRecord::Base
|
|
has_many :albums, :conditions => ['has_pic = 1'], :order => 'year ASC', :dependent => :destroy
|
|
has_many :artist_links, :dependent => :destroy
|
|
has_many :artist_aliases, :dependent => :destroy
|
|
has_many :tracks, :through => :track_artists
|
|
has_many :playlists
|
|
|
|
after_initialize :prepare_description
|
|
|
|
def status_str
|
|
%w(loading ok fail)[self.status]
|
|
end
|
|
|
|
def load_again!
|
|
albums.each(&:destroy)
|
|
artist_links.each(&:destroy)
|
|
artist_aliases.each(&:destroy)
|
|
self.status = 0
|
|
save!
|
|
Delayed::Job.enqueue(LoadArtistJob.new(name))
|
|
end
|
|
|
|
def serialize
|
|
{
|
|
name: name,
|
|
original_name: original_name,
|
|
url_name: name.gsub(" ", "+"),
|
|
pic_url: pic_url,
|
|
desc: desc,
|
|
albums: albums.map(&:serialize),
|
|
artist_links: artist_links.map(&:serialize)
|
|
}
|
|
end
|
|
|
|
private
|
|
|
|
def prepare_description
|
|
self.desc.gsub!(/[\[<].*?[\]>]/, '') unless self.desc.nil?
|
|
rescue
|
|
# WTF?!
|
|
end
|
|
end
|