40 lines
769 B
Ruby
Raw Normal View History

2011-06-14 20:05:12 +04:00
class Track < ActiveRecord::Base
2011-06-15 00:01:42 +04:00
belongs_to :album
2011-09-26 04:05:46 +04:00
has_many :track_artists
2011-09-22 19:40:44 +04:00
has_many :artists, :through => :track_artists
2011-11-26 19:03:33 +04:00
has_many :playlist_items
2011-10-20 15:01:57 +04:00
after_save :clear_artist_cache
2011-10-20 15:01:57 +04: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
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
2011-06-14 20:05:12 +04:00
end