70 lines
2.9 KiB
Ruby
70 lines
2.9 KiB
Ruby
module JamRuby
|
|
class PerformanceSample < ActiveRecord::Base
|
|
|
|
PERMISSION_MSG = "You do not have permission to perform this operation."
|
|
|
|
attr_accessible :user_id, :service_type, :claimed_recording_id, :service_id, :url
|
|
|
|
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 :service_type, presence:true, length: {maximum: 100}
|
|
|
|
# JamKazam validators
|
|
validate :claimed_recording_id_present, :if => lambda { |p| p.service_type == "jamkazam" }
|
|
validate :user_type_recording_unique, :if => lambda { |p| p.service_type == "jamkazam" }
|
|
|
|
# Non-JamKazam validators
|
|
validate :service_id_present, :if => lambda { |p| p.service_type != "jamkazam" }
|
|
validate :user_type_service_unique, :if => lambda { |p| p.service_type != "jamkazam" }
|
|
|
|
def claimed_recording_id_present
|
|
raise StateError, "Claimed recording is required for JamKazam performance samples" if self.claimed_recording_id.blank?
|
|
end
|
|
|
|
def user_type_recording_unique
|
|
match = PerformanceSample.exists?(:user_id => self.user_id, :claimed_recording_id => self.claimed_recording_id, :service_type => self.service_type)
|
|
raise ConflictError, "You already have this JamKazam recording listed as a sample" if match
|
|
end
|
|
|
|
def service_id_present
|
|
raise StateError, "Service ID is required for non-JamKazam performance samples" if self.service_id.blank?
|
|
end
|
|
|
|
def user_type_service_unique
|
|
match = PerformanceSample.exists?(:user_id => self.user_id, :service_id => self.service_id, :service_type => self.service_type)
|
|
raise ConflictError, "You already have this #{self.service_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.create(current_user, options = {})
|
|
auth_user(current_user, options)
|
|
raise StateError, "Missing required information" if options[:service_type].blank?
|
|
|
|
ps = PerformanceSample.new({
|
|
:user_id => current_user.id,
|
|
:service_type => options[:service_type],
|
|
:claimed_recording_id => options[:claimed_recording_id],
|
|
:service_id => options[:service_id],
|
|
:url => options[:url]
|
|
})
|
|
|
|
ps.save!
|
|
end
|
|
|
|
def self.delete(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 StateError, "The performance sample ID is missing." if options[:id].blank?
|
|
PerformanceSample.destroy(options[:id])
|
|
end
|
|
|
|
private
|
|
def self.auth_user(current_user, options={})
|
|
raise PermissionError, PERMISSION_MSG if current_user.nil? || options[:user_id] != current_user.id
|
|
end
|
|
end
|
|
end |