jam-cloud/ruby/lib/jam_ruby/models/jam_track_track.rb

48 lines
1.9 KiB
Ruby

module JamRuby
# describes an audio track (like the drums, or guitar) that comprises a JamTrack
class JamTrackTrack < ActiveRecord::Base
include JamRuby::S3ManagerMixin
# there should only be one Master per JamTrack, but there can be N Track per JamTrack
TRACK_TYPE = %w{Master Track}
mount_uploader :url, JamTrackTrackUploader
attr_accessible :track_type, :instrument, :instrument_id, :position, :part, :url, as: :admin
validates :position, presence: true, numericality: {only_integer: true}, length: {in: 1..1000}
validates :part, length: {maximum: 20}
validates :track_type, inclusion: {in: TRACK_TYPE }
validates_uniqueness_of :position, scope: :jam_track_id
validates_uniqueness_of :part, scope: :jam_track_id
# validates :jam_track, presence: true
belongs_to :instrument, class_name: "JamRuby::Instrument"
belongs_to :jam_track, class_name: "JamRuby::JamTrack"
# create storage directory that will house this jam_track, as well as
def store_dir
"#{jam_track.store_dir}/tracks"
end
# create name of the file
def filename
track_type == 'Master' ? 'master.ogg' : "#{part}.ogg"
end
# creates a short-lived URL that has access to the object.
# the idea is that this is used when a user who has the rights to this tries to download this JamTrack
# we would verify their rights (can_download?), and generates a URL in response to the click so that they can download
# but the url is short lived enough so that it wouldn't be easily shared
def sign_url(expiration_time = 120)
s3_manager.sign_url(self[:url], {:expires => expiration_time, :response_content_type => 'audio/ogg', :secure => false})
end
def can_download?(user)
# I think we have to make a special case for 'previews', but maybe that's just up to the controller to not check can_download?
jam_track.owners.include?(user)
end
end
end