Commit 2e14e80a12f2b5f1bf051f29baed0fbe84ef9451

Authored by Michel Felipe
1 parent 7ba0db11

Created 'AddMemberJob' to enqueue added members action to execute in another mom…

…ent. Added functional test for this feature
app/controllers/public/invite_controller.rb
@@ -65,33 +65,33 @@ class InviteController < PublicController @@ -65,33 +65,33 @@ class InviteController < PublicController
65 #Invite or add member without create a task 65 #Invite or add member without create a task
66 #if logged user is admin of environment 66 #if logged user is admin of environment
67 def invite_registered_friend 67 def invite_registered_friend
68 - contacts_to_invite = params['q'].split(',')  
69 - if !contacts_to_invite.empty? && request.post?  
70 68
71 - if user.is_admin?  
72 - person = Person.find(user.id)  
73 - session[:invited_members] = [] 69 + if !params['q'].present? || !request.post?
74 70
75 - Invitation.invite(person, contacts_to_invite, '', profile) do |added_member|  
76 - session[:invited_members] << added_member.id  
77 - end 71 + redirect_to :action => 'invite_friends'
  72 + session[:notice] = _('Please enter a valid profile.')
78 73
79 - session[:notice] = _('This friends was added!')  
80 - else  
81 - Delayed::Job.enqueue InvitationJob.new(user.id, contacts_to_invite, '', profile.id, nil, locale)  
82 - session[:notice] = _('Your invitations are being sent.')  
83 - end 74 + return
  75 + end
84 76
  77 + contacts_to_invite = params['q'].split(',')
85 78
86 - if profile.person?  
87 - redirect_to :controller => 'profile', :action => 'friends'  
88 - else  
89 - redirect_to :controller => 'profile', :action => 'members'  
90 - end 79 + if user.is_admin? && profile.community?
  80 +
  81 + Delayed::Job.enqueue AddMembersJob.new contacts_to_invite, profile.id, locale
  82 +
  83 + session[:notice] = _('This friends was added!')
91 else 84 else
92 - redirect_to :action => 'invite_friends'  
93 - session[:notice] = _('Please enter a valid profile.') 85 + Delayed::Job.enqueue InvitationJob.new(user.id, contacts_to_invite, '', profile.id, nil, locale)
  86 + session[:notice] = _('Your invitations are being sent.')
94 end 87 end
  88 +
  89 + if profile.person?
  90 + redirect_to :controller => 'profile', :action => 'friends'
  91 + else
  92 + redirect_to :controller => 'profile', :action => 'members'
  93 + end
  94 +
95 end 95 end
96 96
97 def search 97 def search
app/models/invitation.rb
@@ -47,7 +47,6 @@ class Invitation &lt; Task @@ -47,7 +47,6 @@ class Invitation &lt; Task
47 end 47 end
48 48
49 def self.invite(person, contacts_to_invite, message, profile) 49 def self.invite(person, contacts_to_invite, message, profile)
50 -  
51 contacts_to_invite.each do |contact_to_invite| 50 contacts_to_invite.each do |contact_to_invite|
52 next if contact_to_invite == _("Firstname Lastname <friend@email.com>") 51 next if contact_to_invite == _("Firstname Lastname <friend@email.com>")
53 52
@@ -80,23 +79,7 @@ class Invitation &lt; Task @@ -80,23 +79,7 @@ class Invitation &lt; Task
80 if profile.person? 79 if profile.person?
81 InviteFriend.create(task_args) if user.nil? || !user.person.is_a_friend?(person) 80 InviteFriend.create(task_args) if user.nil? || !user.person.is_a_friend?(person)
82 elsif profile.community? 81 elsif profile.community?
83 -  
84 - if person.is_admin?  
85 - unless user.person.is_member_of?(profile)  
86 -  
87 - profile.add_member(user.person)  
88 -  
89 - #Call after add member callback  
90 - yield user  
91 -  
92 - end  
93 - else  
94 -  
95 - InviteMember.create(task_args.merge(:community_id => profile.id)) if user.nil? || !user.person.is_member_of?(profile)  
96 -  
97 - #Call after create invite member task callback  
98 - yield user,task_args  
99 - end 82 + InviteMember.create(task_args.merge(:community_id => profile.id)) if user.nil? || !user.person.is_member_of?(profile)
100 else 83 else
101 raise NotImplementedError, 'Don\'t know how to invite people to a %s' % profile.class.to_s 84 raise NotImplementedError, 'Don\'t know how to invite people to a %s' % profile.class.to_s
102 end 85 end
app/models/profile.rb
@@ -665,6 +665,32 @@ private :generate_url, :url_options @@ -665,6 +665,32 @@ private :generate_url, :url_options
665 end 665 end
666 end 666 end
667 667
  668 + # Adds many people to profile by id's or email's
  669 + def add_members(people_ids)
  670 +
  671 + unless people_ids.nil? && people_ids.empty?
  672 + people = []
  673 +
  674 + if people_ids.first =~ /\@/
  675 + people = User.where(email: people_ids)
  676 + else
  677 + people = Person.where(id: people_ids)
  678 + end
  679 +
  680 + people.each do |profile|
  681 + person = profile
  682 +
  683 + if profile.is_a? User
  684 + person = profile.person
  685 + end
  686 +
  687 + unless person.is_member_of?(self)
  688 + add_member person
  689 + end
  690 + end
  691 + end
  692 + end
  693 +
668 def remove_member(person) 694 def remove_member(person)
669 self.disaffiliate(person, Profile::Roles.all_roles(environment.id)) 695 self.disaffiliate(person, Profile::Roles.all_roles(environment.id))
670 end 696 end
lib/add_members_job.rb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +class AddMembersJob < Struct.new(:people_ids, :profile_id, :locale)
  2 +
  3 + def perform
  4 + Noosfero.with_locale(locale) do
  5 +
  6 + profile = Profile.find(profile_id)
  7 + profile.add_members people_ids
  8 +
  9 + end
  10 + end
  11 +end
test/functional/invite_controller_test.rb
@@ -284,6 +284,11 @@ class InviteControllerTest &lt; ActionController::TestCase @@ -284,6 +284,11 @@ class InviteControllerTest &lt; ActionController::TestCase
284 assert_empty json_response 284 assert_empty json_response
285 end 285 end
286 286
  287 + #@todo Copy this test and create a another version
  288 + #of this, for test add members in a community
  289 + #logged as admin of environment!
  290 + # #issue227
  291 + #
287 should 'invite registered users through profile id' do 292 should 'invite registered users through profile id' do
288 friend1 = create_user('testuser1').person 293 friend1 = create_user('testuser1').person
289 friend2 = create_user('testuser2').person 294 friend2 = create_user('testuser2').person
@@ -297,6 +302,29 @@ class InviteControllerTest &lt; ActionController::TestCase @@ -297,6 +302,29 @@ class InviteControllerTest &lt; ActionController::TestCase
297 end 302 end
298 end 303 end
299 304
  305 + should 'add registered users imediatly instead invite if logged user is a environment admin' do
  306 +
  307 + #Add user like a environment admin
  308 + Environment.default.add_admin profile
  309 +
  310 + friend1 = create_user('testuser1').person
  311 + friend2 = create_user('testuser2').person
  312 +
  313 + assert_difference 'Delayed::Job.count', 1 do
  314 + assert_equal 0,community.members.count
  315 +
  316 + post :invite_registered_friend, :profile => @community.identifier, :q => [friend1.id,friend2.id]
  317 +
  318 + assert_response :redirect
  319 + assert_redirected_to :controller => 'profile', :action => 'members'
  320 + end
  321 +
  322 + Delayed::Worker.new.run(Delayed::Job.last)
  323 +
  324 + assert_equal 2,community.members.count
  325 +
  326 + end
  327 +
300 private 328 private
301 329
302 def json_response 330 def json_response