VRFS-159 fixed bugs / added unit tests
This commit is contained in:
parent
80a5a5d7f3
commit
2c4e550910
|
|
@ -13,34 +13,43 @@ module JamRuby
|
|||
|
||||
def self.save(id, band_id, user_id, creator_id, accepted)
|
||||
|
||||
# ensure certain fields are only updated on creation
|
||||
if id.nil?
|
||||
# ensure recipient is a Musician
|
||||
user = User.find(user_id)
|
||||
unless user.musician?
|
||||
raise JamRuby::JamArgumentError, BAND_INVITATION_FAN_RECIPIENT_ERROR
|
||||
band_invitation = BandInvitation.new()
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
# ensure certain fields are only updated on creation
|
||||
if id.nil?
|
||||
# ensure recipient is a Musician
|
||||
user = User.find(user_id)
|
||||
unless user.musician?
|
||||
raise JamRuby::JamArgumentError, BAND_INVITATION_FAN_RECIPIENT_ERROR
|
||||
end
|
||||
|
||||
band_invitation.band_id = band_id
|
||||
band_invitation.user_id = user_id
|
||||
band_invitation.creator_id = creator_id
|
||||
|
||||
# only the accepted flag can be updated after initial creation
|
||||
else
|
||||
band_invitation = BandInvitation.find(id)
|
||||
band_invitation.accepted = accepted
|
||||
end
|
||||
|
||||
band_invitation = BandInvitation.new()
|
||||
band_invitation.band_id = band_id
|
||||
band_invitation.user_id = user_id
|
||||
band_invitation.creator_id = creator_id
|
||||
band_invitation.updated_at = Time.now.getutc
|
||||
band_invitation.save
|
||||
|
||||
# only the accepted flag can be updated after initial creation
|
||||
else
|
||||
band_invitation = BandInvitation.find(id)
|
||||
band_invitation.accepted = accepted
|
||||
# accept logic => (1) auto-friend each band member and (2) add the musician to the band
|
||||
if accepted
|
||||
band_musicians = BandMusician.where(:band_id => band_invitation.band.id)
|
||||
unless band_musicians.nil?
|
||||
band_musicians.each do |bm|
|
||||
Friendship.save(band_invitation.receiver.id, bm.user_id)
|
||||
end
|
||||
end
|
||||
|
||||
# accepting an invitation adds the musician to the band
|
||||
BandMusician.create(:band_id => band_invitation.band.id, :user_id => band_invitation.receiver.id, :admin => false)
|
||||
end
|
||||
end
|
||||
|
||||
band_invitation.updated_at = Time.now.getutc
|
||||
band_invitation.save
|
||||
|
||||
# TODO: wrap this and previous block in transaction
|
||||
# accepting an invitation adds the musician to the band
|
||||
if accepted
|
||||
BandMusician.create(:band_id => band_invitation.band.id, :user_id => band_invitation.receiver.id, :admin => false)
|
||||
end
|
||||
|
||||
return band_invitation
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -26,22 +26,14 @@ module JamRuby
|
|||
friend_request.save
|
||||
else
|
||||
ActiveRecord::Base.transaction do
|
||||
friend_request = FriendRequest.find(id) #("user_id='#{user_id}' AND friend_id='#{friend_id}'")
|
||||
friend_request = FriendRequest.find(id)
|
||||
friend_request.status = status
|
||||
friend_request.updated_at = Time.now.getutc
|
||||
friend_request.save
|
||||
|
||||
# create both records for this friendship
|
||||
if friend_request.status == "accept"
|
||||
friendship = Friendship.new()
|
||||
friendship.user_id = friend_request.user_id
|
||||
friendship.friend_id = friend_request.friend_id
|
||||
friendship.save
|
||||
|
||||
friendship = Friendship.new()
|
||||
friendship.user_id = friend_request.friend_id
|
||||
friendship.friend_id = friend_request.user_id
|
||||
friendship.save
|
||||
if friend_request.status == "accept"
|
||||
Friendship.save(friend_request.user_id, friend_request.friend_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -57,10 +49,10 @@ module JamRuby
|
|||
# any have been marked as spam or blocked, set the status of this friend request
|
||||
# to match so it doesn't show up in the queue
|
||||
unless friend_requests.nil? || friend_requests.size == 0
|
||||
if friend_requests.exists(:status => "spam")
|
||||
if friend_requests.exists?(:status => "spam")
|
||||
friend_request.status = "spam"
|
||||
|
||||
elsif friend_requests.exists(:status => "block")
|
||||
elsif friend_requests.exists?(:status => "block")
|
||||
friend_request.status = "block"
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,10 +1,19 @@
|
|||
module JamRuby
|
||||
class Friendship < ActiveRecord::Base
|
||||
|
||||
attr_accessible :user_id, :friend_id
|
||||
|
||||
self.primary_key = 'id'
|
||||
|
||||
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id", :inverse_of => :inverse_friendships
|
||||
belongs_to :friend, :class_name => "JamRuby::User", :foreign_key => "friend_id", :inverse_of => :friendships
|
||||
|
||||
def self.save(user_id, friend_id)
|
||||
friendship = Friendship.where("user_id='#{user_id}' AND friend_id='#{friend_id}'")
|
||||
unless friendship.nil?
|
||||
Friendship.create(:user_id => user_id, :friend_id => friend_id)
|
||||
Friendship.create(:user_id => friend_id, :friend_id => user_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue