Commit b429b47a264d5d2730186b87ebdbf134a906aefa

Authored by Larissa Reis
1 parent 6d5e63f5

Invite users through profile id

app/models/invitation.rb
... ... @@ -50,7 +50,10 @@ class Invitation < Task
50 50 next if contact_to_invite == _("Firstname Lastname <friend@email.com>")
51 51  
52 52 contact_to_invite.strip!
53   - if match = contact_to_invite.match(/(.*)<(.*)>/) and match[2].match(Noosfero::Constants::EMAIL_FORMAT)
  53 + find_by_profile_id = false
  54 + if contact_to_invite.match(/^\d*$/)
  55 + find_by_profile_id = true
  56 + elsif match = contact_to_invite.match(/(.*)<(.*)>/) and match[2].match(Noosfero::Constants::EMAIL_FORMAT)
54 57 friend_name = match[1].strip
55 58 friend_email = match[2]
56 59 elsif match = contact_to_invite.strip.match(Noosfero::Constants::EMAIL_FORMAT)
... ... @@ -60,11 +63,15 @@ class Invitation &lt; Task
60 63 next
61 64 end
62 65  
63   - user = User.find_by_email(friend_email)
  66 + begin
  67 + user = find_by_profile_id ? Person.find_by_id(contact_to_invite).user : User.find_by_email(friend_email)
  68 + rescue
  69 + user = nil
  70 + end
64 71  
65   - task_args = if user.nil?
  72 + task_args = if user.nil? && !find_by_profile_id
66 73 {:person => person, :friend_name => friend_name, :friend_email => friend_email, :message => message}
67   - elsif !(user.person.is_a_friend?(person) && profile.person?)
  74 + elsif user.present? && !(user.person.is_a_friend?(person) && profile.person?)
68 75 {:person => person, :target => user.person}
69 76 end
70 77  
... ...
test/unit/invitation_test.rb
... ... @@ -120,4 +120,17 @@ class InvitationTest &lt; ActiveSupport::TestCase
120 120 should 'have a message with url' do
121 121 assert_equal "\n\nTo accept invitation, please follow this link: <url>", Invitation.default_message_to_accept_invitation
122 122 end
  123 +
  124 + should 'invite friends through profile id' do
  125 + person = create_user('testuser1').person
  126 + friend = create_user('testuser2').person
  127 + community = fast_create(Community)
  128 +
  129 + assert_difference InviteMember, :count do
  130 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', community)
  131 + end
  132 + assert_difference InviteFriend, :count do
  133 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person)
  134 + end
  135 + end
123 136 end
... ...