2015-01-29 05:48:24 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class PerformanceSample < ActiveRecord::Base
|
2015-02-12 03:33:10 +00:00
|
|
|
|
|
|
|
|
PERMISSION_MSG = "You do not have permission to perform this operation."
|
|
|
|
|
|
VRFS-3242 : Schema and model changes required for band profile functionality.
* Additional attributes for band_type, band_status, concert_count,
add_new_members, play_commitment, touring_option, paid_gigs,
hourly_rate, gig_minimum
* For joined table musician_instruments, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table performance_stamples, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table online_presences, remove the hard requirement that
they be joined to a user, rather a “player” that is polymorphic.
* Change models as appropriate with new attributes and modify
belongs_to / has_many directives as necessary.
* Fix existing usages of user_id to work with polymorphic player_id.
* Fix tests that use user_id
* Add new tests that exercise online_presence, performance_samples, and
instruments that target a band, rather than a user.
2015-05-14 02:06:14 +00:00
|
|
|
attr_accessible :player_id, :service_type, :claimed_recording_id, :service_id, :url, :description
|
2015-02-12 03:33:10 +00:00
|
|
|
|
VRFS-3242 : Schema and model changes required for band profile functionality.
* Additional attributes for band_type, band_status, concert_count,
add_new_members, play_commitment, touring_option, paid_gigs,
hourly_rate, gig_minimum
* For joined table musician_instruments, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table performance_stamples, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table online_presences, remove the hard requirement that
they be joined to a user, rather a “player” that is polymorphic.
* Change models as appropriate with new attributes and modify
belongs_to / has_many directives as necessary.
* Fix existing usages of user_id to work with polymorphic player_id.
* Fix tests that use user_id
* Add new tests that exercise online_presence, performance_samples, and
instruments that target a band, rather than a user.
2015-05-14 02:06:14 +00:00
|
|
|
belongs_to :player, polymorphic: true
|
2015-02-01 13:42:44 +00:00
|
|
|
belongs_to :claimed_recording, :class_name => "JamRuby::ClaimedRecording", :foreign_key => "claimed_recording_id"
|
2015-01-31 21:07:34 +00:00
|
|
|
|
2015-02-12 03:33:10 +00:00
|
|
|
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
|
2015-01-31 21:07:34 +00:00
|
|
|
|
|
|
|
|
def user_type_recording_unique
|
VRFS-3242 : Schema and model changes required for band profile functionality.
* Additional attributes for band_type, band_status, concert_count,
add_new_members, play_commitment, touring_option, paid_gigs,
hourly_rate, gig_minimum
* For joined table musician_instruments, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table performance_stamples, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table online_presences, remove the hard requirement that
they be joined to a user, rather a “player” that is polymorphic.
* Change models as appropriate with new attributes and modify
belongs_to / has_many directives as necessary.
* Fix existing usages of user_id to work with polymorphic player_id.
* Fix tests that use user_id
* Add new tests that exercise online_presence, performance_samples, and
instruments that target a band, rather than a user.
2015-05-14 02:06:14 +00:00
|
|
|
match = PerformanceSample.exists?(:player_id => self.player_id, :claimed_recording_id => self.claimed_recording_id, :service_type => self.service_type)
|
2015-02-01 13:42:44 +00:00
|
|
|
raise ConflictError, "You already have this JamKazam recording listed as a sample" if match
|
2015-01-31 21:07:34 +00:00
|
|
|
end
|
|
|
|
|
|
2015-02-12 03:33:10 +00:00
|
|
|
def service_id_present
|
|
|
|
|
raise StateError, "Service ID is required for non-JamKazam performance samples" if self.service_id.blank?
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-31 21:07:34 +00:00
|
|
|
def user_type_service_unique
|
VRFS-3242 : Schema and model changes required for band profile functionality.
* Additional attributes for band_type, band_status, concert_count,
add_new_members, play_commitment, touring_option, paid_gigs,
hourly_rate, gig_minimum
* For joined table musician_instruments, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table performance_stamples, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table online_presences, remove the hard requirement that
they be joined to a user, rather a “player” that is polymorphic.
* Change models as appropriate with new attributes and modify
belongs_to / has_many directives as necessary.
* Fix existing usages of user_id to work with polymorphic player_id.
* Fix tests that use user_id
* Add new tests that exercise online_presence, performance_samples, and
instruments that target a band, rather than a user.
2015-05-14 02:06:14 +00:00
|
|
|
match = PerformanceSample.exists?(:player_id => self.player_id, :service_id => self.service_id, :service_type => self.service_type)
|
2015-02-12 03:33:10 +00:00
|
|
|
raise ConflictError, "You already have this #{self.service_type} sample listed (#{self.service_id})." if match
|
2015-02-01 13:42:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.index(options = {})
|
|
|
|
|
raise JamArgumentError, "The user is not specified." if options[:id].blank?
|
VRFS-3242 : Schema and model changes required for band profile functionality.
* Additional attributes for band_type, band_status, concert_count,
add_new_members, play_commitment, touring_option, paid_gigs,
hourly_rate, gig_minimum
* For joined table musician_instruments, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table performance_stamples, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table online_presences, remove the hard requirement that
they be joined to a user, rather a “player” that is polymorphic.
* Change models as appropriate with new attributes and modify
belongs_to / has_many directives as necessary.
* Fix existing usages of user_id to work with polymorphic player_id.
* Fix tests that use user_id
* Add new tests that exercise online_presence, performance_samples, and
instruments that target a band, rather than a user.
2015-05-14 02:06:14 +00:00
|
|
|
PerformanceSample.where("player_id = ?", options[:id])
|
2015-02-01 13:42:44 +00:00
|
|
|
end
|
|
|
|
|
|
2015-06-01 02:50:04 +00:00
|
|
|
# Create with target_player (target_player is either user or band):
|
|
|
|
|
def self.create(target_player, options = {}, save = true)
|
|
|
|
|
auth_user(target_player, options)
|
2015-02-12 03:33:10 +00:00
|
|
|
raise StateError, "Missing required information" if options[:service_type].blank?
|
|
|
|
|
|
|
|
|
|
ps = PerformanceSample.new({
|
2015-06-01 02:50:04 +00:00
|
|
|
:player_id => target_player.id,
|
2015-02-12 03:33:10 +00:00
|
|
|
:service_type => options[:service_type],
|
|
|
|
|
:claimed_recording_id => options[:claimed_recording_id],
|
|
|
|
|
:service_id => options[:service_id],
|
2015-04-05 01:46:37 +00:00
|
|
|
:url => options[:url],
|
|
|
|
|
:description => options[:description]
|
2015-02-12 03:33:10 +00:00
|
|
|
})
|
|
|
|
|
|
2015-02-15 13:23:26 +00:00
|
|
|
ps.save! if save
|
|
|
|
|
ps
|
2015-02-12 03:33:10 +00:00
|
|
|
end
|
|
|
|
|
|
2015-06-01 02:50:04 +00:00
|
|
|
def self.delete(target_player, options = {})
|
|
|
|
|
raise JamPermissionError, "You do not have permission to perform this operation" if target_player.nil? || options[:player_id] != target_player.id
|
2015-02-13 06:32:55 +00:00
|
|
|
raise StateError, "The performance sample ID is missing." if options[:id].blank?
|
|
|
|
|
PerformanceSample.destroy(options[:id])
|
2015-01-31 21:07:34 +00:00
|
|
|
end
|
2015-02-12 03:33:10 +00:00
|
|
|
|
|
|
|
|
private
|
2015-06-01 02:50:04 +00:00
|
|
|
def self.auth_user(target_player, options={})
|
|
|
|
|
raise JamPermissionError, PERMISSION_MSG if target_player.nil? || options[:player_id] != target_player.id
|
2015-02-12 03:33:10 +00:00
|
|
|
end
|
2015-01-29 05:48:24 +00:00
|
|
|
end
|
|
|
|
|
end
|