Commit 82ba86f13532bad12377fa07b0a0faa289067ce1

Authored by Larissa Reis
1 parent 20446fe0

invite-members: refactor friends search by usp_id

  Removes the need of a hotspot to offer the possibility of searching
  members by usp_id on invite page. The previous hotspot to provide
  extra search fields was deemed too specific and complicated.

  Refactor from commits:
    844304db138e2c7bb6234be39301f054b7a657cb
    8470293887b42804d572dcffc5ee02af1a0d2ff6
app/controllers/public/invite_controller.rb
@@ -7,6 +7,17 @@ class InviteController < PublicController @@ -7,6 +7,17 @@ class InviteController < PublicController
7 def invite_friends 7 def invite_friends
8 @import_from = params[:import_from] || "manual" 8 @import_from = params[:import_from] || "manual"
9 @mail_template = params[:mail_template] || environment.invitation_mail_template(profile) 9 @mail_template = params[:mail_template] || environment.invitation_mail_template(profile)
  10 +
  11 + extra_labels = Profile::SEARCHABLE_FIELDS.keys - [:name, :identifier, :nickname]
  12 + labels = [
  13 + _('Name'),
  14 + _('Username'),
  15 + _('Email'),
  16 + ] + extra_labels.map { |label| Profile.human_attribute_name(label) }
  17 + last = labels.pop
  18 + label = labels.join(', ')
  19 + @search_friend_fields = "#{label} #{_('or')} #{last}"
  20 +
10 if request.post? 21 if request.post?
11 contact_list = ContactList.create 22 contact_list = ContactList.create
12 Delayed::Job.enqueue GetEmailContactsJob.new(@import_from, params[:login], params[:password], contact_list.id) if @import_from != 'manual' 23 Delayed::Job.enqueue GetEmailContactsJob.new(@import_from, params[:login], params[:password], contact_list.id) if @import_from != 'manual'
@@ -73,12 +84,8 @@ class InviteController < PublicController @@ -73,12 +84,8 @@ class InviteController < PublicController
73 end 84 end
74 end 85 end
75 86
76 - include InviteHelper  
77 - helper :invite  
78 -  
79 def search_friend 87 def search_friend
80 - fields = %w[name identifier email] + plugins_options.map {|field| field[:field].to_s }  
81 - render :text => find_by_contents(:people, environment.people.not_members_of(profile), params['q'], {:page => 1}, {:fields => fields, :joins => :user})[:results].map {|person| {:id => person.id, :name => person.name} }.to_json 88 + render :text => find_by_contents(:people, environment.people.not_members_of(profile), params['q'], {:page => 1}, {:joins => :user})[:results].map {|person| {:id => person.id, :name => person.name} }.to_json
82 end 89 end
83 90
84 protected 91 protected
app/helpers/invite_helper.rb
1 module InviteHelper 1 module InviteHelper
2 - def plugins_options  
3 - @plugins.dispatch(:search_friend_fields)  
4 - end  
5 -  
6 - def search_friend_fields  
7 - labels = [  
8 - _('Name'),  
9 - _('Username'),  
10 - _('Email'),  
11 - ] + plugins_options.map { |options| options[:name] }  
12 -  
13 - last = labels.pop  
14 - label = labels.join(', ')  
15 - "#{label} #{_('or')} #{last}"  
16 - end  
17 end 2 end
app/views/invite/invite_friends.rhtml
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 <% form_tag :action => 'invite_registered_friend' do %> 10 <% form_tag :action => 'invite_registered_friend' do %>
11 <% search_action = url_for(:action => 'search_friend') %> 11 <% search_action = url_for(:action => 'search_friend') %>
12 <%= token_input_field_tag(:q, 'search-friends', search_action, 12 <%= token_input_field_tag(:q, 'search-friends', search_action,
13 - { :hint_text => _('Type in your friend\'s ') + search_friend_fields, 13 + { :hint_text => _('Type in your friend\'s ') + @search_friend_fields,
14 :focus => false }) %> 14 :focus => false }) %>
15 15
16 <% button_bar do %> 16 <% button_bar do %>
lib/noosfero/plugin.rb
@@ -437,12 +437,6 @@ class Noosfero::Plugin @@ -437,12 +437,6 @@ class Noosfero::Plugin
437 nil 437 nil
438 end 438 end
439 439
440 - # -> Adds aditional fields for invite_friends search  
441 - # returns = [{:field => 'field1', :name => 'field 1 name'}, {...}]  
442 - def search_friend_fields  
443 - nil  
444 - end  
445 -  
446 # -> Adds additional blocks to profiles and environments. 440 # -> Adds additional blocks to profiles and environments.
447 # Your plugin must implements a class method called 'extra_blocks' 441 # Your plugin must implements a class method called 'extra_blocks'
448 # that returns a hash with the following syntax. 442 # that returns a hash with the following syntax.
plugins/stoa/lib/ext/person.rb
1 require_dependency 'person' 1 require_dependency 'person'
2 2
3 class Person 3 class Person
  4 + SEARCHABLE_FIELDS[:usp_id] = 5
  5 +
4 validates_uniqueness_of :usp_id, :allow_nil => true 6 validates_uniqueness_of :usp_id, :allow_nil => true
5 settings_items :invitation_code 7 settings_items :invitation_code
6 validate :usp_id_or_invitation, :if => lambda { |person| person.environment && person.environment.plugin_enabled?(StoaPlugin)} 8 validate :usp_id_or_invitation, :if => lambda { |person| person.environment && person.environment.plugin_enabled?(StoaPlugin)}
test/functional/invite_controller_test.rb
@@ -269,30 +269,6 @@ class InviteControllerTest &lt; ActionController::TestCase @@ -269,30 +269,6 @@ class InviteControllerTest &lt; ActionController::TestCase
269 assert_equal [{"name" => friend1.name, "id" => friend1.id}].to_json, @response.body 269 assert_equal [{"name" => friend1.name, "id" => friend1.id}].to_json, @response.body
270 end 270 end
271 271
272 - should 'search friends profiles by fields provided by plugins' do  
273 - class Plugin1 < Noosfero::Plugin  
274 - def search_friend_fields  
275 - [{:field => 'nickname'}, {:field => 'contact_phone'}]  
276 - end  
277 - end  
278 -  
279 - environment = Environment.default  
280 - environment.enable_plugin(Plugin1)  
281 -  
282 - friend1 = create_user('harry').person  
283 - friend2 = create_user('william').person  
284 - friend1.nickname = 'prince'  
285 - friend2.contact_phone = '2222'  
286 - friend1.save  
287 - friend2.save  
288 -  
289 - get :search_friend, :profile => profile.identifier, :q => 'prince'  
290 - assert_equal [{"name" => friend1.name, "id" => friend1.id}].to_json, @response.body  
291 -  
292 - get :search_friend, :profile => profile.identifier, :q => '222'  
293 - assert_equal [{"name" => friend2.name, "id" => friend2.id}].to_json, @response.body  
294 - end  
295 -  
296 should 'invite registered users through profile id' do 272 should 'invite registered users through profile id' do
297 friend1 = create_user('testuser1').person 273 friend1 = create_user('testuser1').person
298 friend2 = create_user('testuser2').person 274 friend2 = create_user('testuser2').person