2011-06-14 16:05:12 +00:00
|
|
|
class Track < ActiveRecord::Base
|
2011-06-14 20:01:42 +00:00
|
|
|
belongs_to :album
|
2011-09-26 00:05:46 +00:00
|
|
|
has_many :track_artists
|
2011-09-22 15:40:44 +00:00
|
|
|
has_many :artists, :through => :track_artists
|
2011-11-26 15:03:33 +00:00
|
|
|
has_many :playlist_items
|
2011-10-20 11:01:57 +00:00
|
|
|
|
2011-11-28 23:22:29 +00:00
|
|
|
after_save :clear_artist_cache
|
|
|
|
|
2011-10-20 11:01:57 +00:00
|
|
|
def duration
|
|
|
|
if self.length
|
|
|
|
time = self.length
|
|
|
|
time_m = (time / 60).floor
|
|
|
|
time_s = time - time_m * 60
|
|
|
|
time_m.to_s + ':' + (time_s < 10 ? '0' : '') + time_s.to_s
|
|
|
|
else
|
|
|
|
'0:00'
|
|
|
|
end
|
|
|
|
end
|
2011-11-27 12:37:51 +00:00
|
|
|
|
|
|
|
def serialize
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
artist: artists.first.name,
|
|
|
|
album: album.name,
|
|
|
|
position: position.to_s(36),
|
|
|
|
name: name,
|
|
|
|
length: length,
|
|
|
|
duration: duration,
|
|
|
|
available: available,
|
|
|
|
album_pic: album.pic_url
|
|
|
|
}
|
|
|
|
end
|
2011-11-28 23:22:29 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def clear_artist_cache
|
|
|
|
artists.each(&:clear_cache)
|
|
|
|
end
|
2011-06-14 16:05:12 +00:00
|
|
|
end
|