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

37 lines
1.9 KiB
Ruby

module JamRuby
class PerformanceSample < ActiveRecord::Base
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
belongs_to :claimed_recording, :class_name => "JamRuby::ClaimedRecording", :foreign_key => "claimed_recording_id"
validates :type, presence:true, length: {maximum: 100}
validates :username, presence:true, length: {maximum: 100}
validate :user_type_recording_unique, :if => lambda { |p| p.type == "jamkazam" }
validate :user_type_service_unique, :if => lambda { |p| p.type != "jamkazam" }
def user_type_recording_unique
match = PerformanceSample.exists?(:user_id => user.id, :claimed_recording_id => self.claimed_recording_id)
raise ConflictError, "You already have this JamKazam recording listed as a sample" if match
end
def user_type_service_unique
match = PerformanceSample.exists?(:user_id => user.id, :service_id => self.service_id)
raise ConflictError, "You already have this #{self.type} sample listed (#{self.service_id}." if match
end
def self.index(options = {})
raise JamArgumentError, "The user is not specified." if options[:id].blank?
PerformanceSample.where("user_id = ?", options[:id])
end
def self.save(current_user, options = {})
raise PermissionError, "You do not have permission to perform this operation" if current_user.nil? || options[:user_id] != current_user.id
PerformanceSample.create(:user => user, :type => options[:type], :username => options[:username])
end
def self.destroy(current_user, options = {})
raise PermissionError, "You do not have permission to perform this operation" if current_user.nil? || options[:user_id] != current_user.id
raise JamArgumentError, "The performance sample ID is missing." if options[:id].blank?
PerformanceSample.destroy_all("id = ?", options[:id])
end
end
end