1
0
Fork 0
beathaven/app/models/artist.rb

63 lines
1.4 KiB
Ruby
Raw Normal View History

2012-08-26 23:53:30 +00:00
class Artist < ActiveRecord::Base
has_many :albums
has_many :performers
has_many :tracks, through: :performers
2012-09-17 08:25:38 +00:00
has_many :artist_genres
2012-08-26 23:53:30 +00:00
has_many :genres, through: :artist_genres
2012-09-17 08:25:38 +00:00
attr_accessible :bio, :is_group, :name, :pic, :rovi_id, :albums, :genres
2012-08-26 23:53:30 +00:00
scope :discography, lambda {
includes(:albums).includes(:tracks)
}
2012-08-26 23:53:30 +00:00
def loaded?
pic? && bio?
end
2012-09-01 23:38:14 +00:00
def url
2012-09-17 08:25:38 +00:00
"/artist/#{name.gsub(/\s/, "+")}" rescue ""
2012-09-01 23:38:14 +00:00
end
2012-09-01 17:55:01 +00:00
def import
return unless rovi_id?
robbie_artist = Robbie::Artist.find(rovi_id)
return if robbie_artist.nil?
update_attributes(
name: robbie_artist.name,
is_group: robbie_artist.is_group,
pic: nil,
bio: nil,
albums: robbie_artist.albums.map{ |robbie_album|
Album.find_or_create_by_rovi_id(robbie_album.id).import
},
genres: robbie_artist.genres.map{ |robbie_genre|
genre = Genre.find_or_create_by_rovi_id(robbie_genre.id)
genre.update_attributes(
name: robbie_genre.name
)
genre
}
)
self
2012-08-26 23:53:30 +00:00
end
class << self
def with_name(name)
# DB lookup
artist = where(name: name).discography.first
2012-08-26 23:53:30 +00:00
return artist unless artist.nil?
# Rovi correction
rovi_artist = Robbie::Artist.find_by_name(name)
return artist if rovi_artist && artist = find_by_rovi_id(rovi_artist.id)
# Parsing artist if ok
Artist.create(rovi_id: robbie_artist.id).import
2012-08-26 23:53:30 +00:00
end
end
end