module JamRuby class JamTrack < ActiveRecord::Base include JamRuby::S3ManagerMixin TIME_SIGNATURES = %w{4/4 3/4 2/4 6/8 5/8'} STATUS = %w{Staging Production Retired} RECORDING_TYPE = %w{Cover Original} PRO = %w{ASCAP BMI SESAC} SALES_REGION = ['United States', 'Worldwide'] PRODUCT_TYPE = 'JamTrack' mount_uploader :url, JamTrackUploader attr_accessible :name, :description, :bpm, :time_signature, :status, :recording_type, :original_artist, :songwriter, :publisher, :licensor, :licensor_id, :pro, :genre, :genre_id, :sales_region, :price, :reproduction_royalty, :public_performance_royalty, :reproduction_royalty_amount, :licensor_royalty_amount, :pro_royalty_amount, :plan_code, :initial_play_silence, :jam_track_tracks_attributes, :jam_track_tap_ins_attributes, :available, as: :admin validates :name, presence: true, uniqueness: true, length: {maximum: 200} validates :description, length: {maximum: 1000} validates :time_signature, inclusion: {in: [nil] + TIME_SIGNATURES} validates :status, inclusion: {in: [nil] + STATUS} validates :recording_type, inclusion: {in: [nil] + RECORDING_TYPE} validates :original_artist, length: {maximum: 200} validates :songwriter, length: {maximum: 1000} validates :publisher, length: {maximum: 1000} validates :pro, inclusion: {in: [nil] + PRO} validates :sales_region, inclusion: {in: [nil] + SALES_REGION} validates_format_of :price, with: /^\d+\.*\d{0,2}$/ validates :initial_play_silence, numericality: true, :allow_nil => true validates :reproduction_royalty, inclusion: {in: [nil, true, false]} validates :public_performance_royalty, inclusion: {in: [nil, true, false]} validates_format_of :reproduction_royalty_amount, with: /^\d+\.*\d{0,3}$/ validates_format_of :licensor_royalty_amount, with: /^\d+\.*\d{0,3}$/ validates_format_of :pro_royalty_amount, with: /^\d+\.*\d{0,3}$/ before_save :sanitize_active_admin belongs_to :genre, class_name: "JamRuby::Genre" belongs_to :licensor , class_name: 'JamRuby::JamTrackLicensor', foreign_key: 'licensor_id' has_many :jam_track_tracks, :class_name => "JamRuby::JamTrackTrack", order: 'position ASC' has_many :jam_track_tap_ins, :class_name => "JamRuby::JamTrackTapIn", order: 'offset_time ASC' has_many :jam_track_rights, :class_name => "JamRuby::JamTrackRight" #, inverse_of: 'jam_track', :foreign_key => "jam_track_id" has_many :owners, :through => :jam_track_rights, :class_name => "JamRuby::User", :source => :user has_many :playing_sessions, :class_name => "JamRuby::ActiveMusicSession" accepts_nested_attributes_for :jam_track_tracks, allow_destroy: true accepts_nested_attributes_for :jam_track_tap_ins, allow_destroy: true class << self def index(options, user) limit = options[:limit] limit ||= 20 limit = limit.to_i start = options[:start].presence start = start.to_i || 0 query = JamTrack.joins(:jam_track_tracks) .paginate(page: 1 + start/limit, per_page: limit) if options[:show_purchased_only] query = query.joins(:jam_track_rights) query = query.where("jam_track_rights.user_id = ?", user.id) end query = query.where("jam_tracks.available = ?", true) unless user.admin query = query.where("jam_tracks.genre_id = '#{options[:genre]}'") unless options[:genre].blank? query = query.where("jam_track_tracks.instrument_id = '#{options[:instrument]}'") unless options[:instrument].blank? query = query.where("jam_tracks.sales_region = '#{options[:availability]}'") unless options[:availability].blank? query = query.group("jam_tracks.id") query = query.order('jam_tracks.name') if query.length == 0 [query, nil] elsif query.length < limit [query, nil] else [query, start + limit] end end end # create storage directory that will house this jam_track, as well as def store_dir "jam_tracks/#{id}" end # create name of the file def filename "#{name}.jkz" 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/jkz', :secure => false}) end def can_download?(user) owners.include?(user) end def right_for_user(user) jam_track_rights.where("user_id=?", user).first end def self.list_downloads(user, limit = 100, since = 0) since = 0 unless since || since == '' # guard against nil downloads = [] user.jam_track_rights .limit(limit) .where('jam_track_rights.id > ?', since) .each do |jam_track_right| downloads << { :type => "jam_track", :id => jam_track_right.id.to_s, :jam_track_id => jam_track_right.jam_track_id, :length => jam_track_right.length, :md5 => jam_track_right.md5, :url => jam_track_right.url, :created_at => jam_track_right.created_at, :next => jam_track_right.id } end next_id = downloads[-1][:next] if downloads.length > 0 next_id = since if next_id.nil? # echo back to the client the same value they passed in, if there are no results { 'downloads' => downloads, 'next' => next_id.to_s } end private def sanitize_active_admin self.genre_id = nil if self.genre_id == '' self.licensor_id = nil if self.licensor_id == '' end end end