95 lines
3.3 KiB
Ruby
95 lines
3.3 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 :jam_track_id, :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
|
|
|
|
def move_up
|
|
#normalize_position
|
|
if self.position > 1
|
|
# Switch with previous
|
|
previous_track = self.jam_track.jam_track_tracks.where("position=?", self.position-1).first
|
|
if previous_track
|
|
JamTrack.transaction do
|
|
previous_track.position,self.position = self.position,previous_track.position
|
|
previous_track.save(validate:false)
|
|
self.save(validate:false)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def move_down
|
|
count=normalize_position
|
|
if self.position < count
|
|
# Switch with next:
|
|
next_track = self.jam_track.jam_track_tracks.where("position=?", self.position+1).first
|
|
if next_track
|
|
next_track.position,self.position = self.position,next_track.position
|
|
next_track.save(validate:false)
|
|
self.save(validate:false)
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
def normalize_position
|
|
parent = self.jam_track
|
|
position = 0
|
|
if parent
|
|
JamTrack.transaction do
|
|
parent.jam_track_tracks.each do |jtt|
|
|
position += 1
|
|
if jtt.position != position
|
|
jtt.position = position
|
|
jtt.save(validate:false)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
position
|
|
end # normalize_position
|
|
|
|
end # class
|
|
end # module
|