2015-01-29 05:48:24 +00:00
|
|
|
module JamRuby
|
2015-02-13 08:16:48 +00:00
|
|
|
class OnlinePresence < ActiveRecord::Base
|
2015-02-01 13:42:44 +00:00
|
|
|
|
2015-02-12 03:33:10 +00:00
|
|
|
PERMISSION_MSG = "You do not have permission to perform this operation."
|
2015-02-04 04:37:20 +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
|
|
|
attr_accessible :player_id, :service_type, :username
|
|
|
|
|
belongs_to :player, polymorphic: true
|
2015-01-31 21:07:34 +00:00
|
|
|
|
2015-02-05 08:34:25 +00:00
|
|
|
validates :service_type, presence:true, length: {maximum: 100}
|
2015-02-01 13:42:44 +00:00
|
|
|
validates :username, presence:true, length: {maximum: 100}
|
|
|
|
|
|
2015-02-12 03:33:10 +00:00
|
|
|
validate :username_service_type_unique
|
|
|
|
|
|
|
|
|
|
def username_service_type_unique
|
2015-02-13 08:16:48 +00:00
|
|
|
match = OnlinePresence.exists?(:username => self.username, :service_type => self.service_type)
|
2015-02-12 03:33:10 +00:00
|
|
|
raise ConflictError, "Username #{self.username} is already associated with a #{self.service_type} account" if match
|
|
|
|
|
end
|
|
|
|
|
|
2015-02-01 13:42:44 +00:00
|
|
|
def self.index(options = {})
|
2015-02-13 06:32:55 +00:00
|
|
|
raise StateError, "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
|
|
|
OnlinePresence.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_player(target_player, options)
|
2015-02-10 07:44:16 +00:00
|
|
|
raise StateError, "Missing required information" if options[:service_type].blank? || options[:username].blank?
|
2015-02-04 04:37:20 +00:00
|
|
|
|
2015-02-15 13:23:26 +00:00
|
|
|
up = OnlinePresence.new({
|
2015-06-01 02:50:04 +00:00
|
|
|
:player_id => target_player.id,
|
2015-02-15 13:23:26 +00:00
|
|
|
:service_type => options[:service_type],
|
|
|
|
|
:username => options[:username]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
up.save! if save
|
|
|
|
|
up
|
2015-02-04 04:37:20 +00:00
|
|
|
end
|
|
|
|
|
|
2015-06-01 02:50:04 +00:00
|
|
|
def self.update(target_player, options = {})
|
|
|
|
|
auth_player(target_player, options)
|
2015-02-10 07:44:16 +00:00
|
|
|
raise StateError, "Missing required information" if options[:service_type].blank? || options[:username].blank? || options[:id].blank?
|
2015-02-05 08:34:25 +00:00
|
|
|
|
2015-02-13 08:16:48 +00:00
|
|
|
up = OnlinePresence.find(options[:id])
|
2015-02-10 07:44:16 +00:00
|
|
|
up.service_type = options[:service_type]
|
|
|
|
|
up.username = options[:username]
|
|
|
|
|
up.save!
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-01 02:50:04 +00:00
|
|
|
def self.delete(target_player, options = {})
|
2015-02-05 08:34:25 +00:00
|
|
|
id = options[:id]
|
2015-02-10 07:44:16 +00:00
|
|
|
raise StateError, "Missing required information" if id.blank?
|
2015-02-13 08:16:48 +00:00
|
|
|
online_presence = OnlinePresence.find(id)
|
2015-02-10 07:44:16 +00:00
|
|
|
|
2015-06-01 02:50:04 +00:00
|
|
|
if online_presence.player_id != target_player.id
|
2015-04-29 17:56:16 +00:00
|
|
|
raise JamPermissionError, PERMISSION_MSG
|
2015-02-05 08:34:25 +00:00
|
|
|
end
|
2015-02-01 13:42:44 +00:00
|
|
|
|
2015-02-13 08:16:48 +00:00
|
|
|
unless online_presence.nil?
|
|
|
|
|
OnlinePresence.destroy(id)
|
2015-02-04 04:37:20 +00:00
|
|
|
end
|
2015-01-31 21:07:34 +00:00
|
|
|
end
|
2015-02-05 08:34:25 +00:00
|
|
|
|
|
|
|
|
private
|
2015-06-01 02:50:04 +00:00
|
|
|
def self.auth_player(target_player, options={})
|
2015-06-03 21:34:37 +00:00
|
|
|
if target_player.nil? || options[:player_id] != target_player.id
|
|
|
|
|
raise JamPermissionError, PERMISSION_MSG
|
|
|
|
|
end
|
2015-02-05 08:34:25 +00:00
|
|
|
end
|
2015-01-29 05:48:24 +00:00
|
|
|
end
|
|
|
|
|
end
|