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, :jam_track_tracks_attributes, :plan_code, as: :admin validates :name, presence: true, uniqueness: true, length: {maximum: 200} validates :description, length: {maximum: 1000} validates_format_of :bpm, with: /^\d+\.*\d{0,1}$/ 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 :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_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 accepts_nested_attributes_for :jam_track_tracks, allow_destroy: true # 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}.jka" 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/jka', :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_date = downloads[-1][:next] if downloads.length > 0 next_date = since if next_date.nil? # echo back to the client the same value they passed in, if there are no results { 'downloads' => downloads, 'next' => next_date.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