40 lines
769 B
Ruby
40 lines
769 B
Ruby
class Track < ActiveRecord::Base
|
|
belongs_to :album
|
|
has_many :track_artists
|
|
has_many :artists, :through => :track_artists
|
|
has_many :playlist_items
|
|
|
|
after_save :clear_artist_cache
|
|
|
|
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
|
|
|
|
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
|
|
|
|
private
|
|
|
|
def clear_artist_cache
|
|
artists.each(&:clear_cache)
|
|
end
|
|
end
|