Commit 5c5e450a authored by zhitomirskiyi's avatar zhitomirskiyi

Invitation.new_or_existing_user_by_email ->...

Invitation.new_or_existing_user_by_email -> Invitation.new_or_existing_user_by_service_and_identifier
parent a92d5f41
......@@ -27,21 +27,30 @@ class Invitation < ActiveRecord::Base
create_invitee(opts)
end
def self.new_or_existing_user_by_email(email)
existing_user = User.where(:email => email).first
def self.new_or_existing_user_by_service_and_identifier(service, identifier)
existing_user = User.where(:invitation_service => service,
:invitation_identifier => identifier).first
if service == 'email'
existing_user ||= User.where(:email => identifier).first
else
existing_user ||= User.joins(:services).where(:services => {:provider => service, :uid => identifier}).first
end
if existing_user
existing_user
else
result = User.new()
result.email = email
result.invitation_service = service
result.invitation_identifier = identifier
result.email = identifier if service == 'email'
result.valid?
result
end
end
def self.create_invitee(opts = {})
invitee = new_or_existing_user_by_email(opts[:identifier])
return invitee unless opts[:identifier].match Devise.email_regexp
invitee = new_or_existing_user_by_service_and_identifier(opts[:service], opts[:identifier])
return invitee unless opts[:service] == 'email' && opts[:identifier].match(Devise.email_regexp)
invitee.invites = opts[:invites] || 0
if invitee.new_record?
invitee.errors.clear
......
......@@ -46,24 +46,75 @@ describe Invitation do
end
describe '.new_or_existing_user_by_email' do
let(:inv){Invitation.new_or_existing_user_by_email(@email)}
let(:inv){Invitation.new_or_existing_user_by_service_and_identifier(@type, @identifier)}
before do
@users = []
8.times do
@users << Factory.create(:user)
end
@user_fb_id = 'abc123'
@user_fb = Factory.create(:user, :invitation_service => "facebook", :invitation_identifier => @user_fb_id)
end
it 'returns User.new for a non-existent user for email' do
@type = "email"
@identifier = "maggie@example.org"
inv.invitation_identifier.should == @identifier
inv.invitation_service.should == 'email'
inv.persisted?.should be_false
lambda {
inv.reload
}.should raise_error ActiveRecord::RecordNotFound
end
it 'returns User.new for a non-existent user' do
@email = "maggie@example.org"
inv.email.should == @email
@type = "facebook"
@identifier = "1234892323"
inv.invitation_identifier.should == @identifier
inv.invitation_service.should == @type
inv.persisted?.should be_false
lambda {
inv.reload
}.should raise_error ActiveRecord::RecordNotFound
end
it 'returns an existing user' do
@email = @users[3].email
inv.should == @users[3]
context 'returns an existing user' do
context 'active users' do
it 'by email' do
@identifier = @users[3].email
@type = 'email'
inv.should == @users[3]
end
it 'by service' do
uid = '123324234'
@users[0].services << Services::Facebook.create(:provider => 'facebook', :uid => uid)
@users[0].save
@type = 'facebook'
@identifier = uid
inv.should == @users[0]
end
end
context 'invitated users' do
it 'by email' do
@identifier = @users[3].email
@type = 'email'
@users[3].invitation_identifier = @identifier
@users[3].invitation_service = @type
@users[3].save
inv.should == @users[3]
end
it 'by service' do
@identifier = @user_fb_id
@type = 'facebook'
inv.should == @user_fb
end
end
end
end
......@@ -236,6 +287,7 @@ describe Invitation do
@new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect)
acceptance_params = {:invitation_token => "abc",
:username => "user",
:email => @email,
:password => "secret",
:password_confirmation => "secret",
:person => {:profile => {:first_name => "Bob",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment