From b429b47a264d5d2730186b87ebdbf134a906aefa Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Sun, 5 Jan 2014 22:23:25 -0300 Subject: [PATCH] Invite users through profile id --- app/models/invitation.rb | 15 +++++++++++---- test/unit/invitation_test.rb | 13 +++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 0f89515..2c42e36 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -50,7 +50,10 @@ class Invitation < Task next if contact_to_invite == _("Firstname Lastname ") contact_to_invite.strip! - if match = contact_to_invite.match(/(.*)<(.*)>/) and match[2].match(Noosfero::Constants::EMAIL_FORMAT) + find_by_profile_id = false + if contact_to_invite.match(/^\d*$/) + find_by_profile_id = true + elsif match = contact_to_invite.match(/(.*)<(.*)>/) and match[2].match(Noosfero::Constants::EMAIL_FORMAT) friend_name = match[1].strip friend_email = match[2] elsif match = contact_to_invite.strip.match(Noosfero::Constants::EMAIL_FORMAT) @@ -60,11 +63,15 @@ class Invitation < Task next end - user = User.find_by_email(friend_email) + begin + user = find_by_profile_id ? Person.find_by_id(contact_to_invite).user : User.find_by_email(friend_email) + rescue + user = nil + end - task_args = if user.nil? + task_args = if user.nil? && !find_by_profile_id {:person => person, :friend_name => friend_name, :friend_email => friend_email, :message => message} - elsif !(user.person.is_a_friend?(person) && profile.person?) + elsif user.present? && !(user.person.is_a_friend?(person) && profile.person?) {:person => person, :target => user.person} end diff --git a/test/unit/invitation_test.rb b/test/unit/invitation_test.rb index 27aee60..4113cb1 100644 --- a/test/unit/invitation_test.rb +++ b/test/unit/invitation_test.rb @@ -120,4 +120,17 @@ class InvitationTest < ActiveSupport::TestCase should 'have a message with url' do assert_equal "\n\nTo accept invitation, please follow this link: ", Invitation.default_message_to_accept_invitation end + + should 'invite friends through profile id' do + person = create_user('testuser1').person + friend = create_user('testuser2').person + community = fast_create(Community) + + assert_difference InviteMember, :count do + Invitation.invite(person, [friend.id.to_s], 'hello friend ', community) + end + assert_difference InviteFriend, :count do + Invitation.invite(person, [friend.id.to_s], 'hello friend ', person) + end + end end -- libgit2 0.21.2