Commit 310f1e116dbf6df180ece3ece2c7842afaa1c34c

Authored by Larissa Reis
1 parent 35c3c598

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 7 def invite_friends
8 8 @import_from = params[:import_from] || "manual"
9 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 21 if request.post?
11 22 contact_list = ContactList.create
12 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 84 end
74 85 end
75 86  
76   - include InviteHelper
77   - helper :invite
78   -
79 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 89 end
83 90  
84 91 protected
... ...
app/helpers/invite_helper.rb
1 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 2 end
... ...
app/views/invite/invite_friends.html.erb
... ... @@ -10,7 +10,7 @@
10 10 <%= form_tag :action => 'invite_registered_friend' do %>
11 11 <% search_action = url_for(:action => 'search_friend') %>
12 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 14 :focus => false }) %>
15 15  
16 16 <% button_bar do %>
... ...
lib/noosfero/plugin.rb
... ... @@ -513,12 +513,6 @@ class Noosfero::Plugin
513 513 nil
514 514 end
515 515  
516   - # -> Adds aditional fields for invite_friends search
517   - # returns = [{:field => 'field1', :name => 'field 1 name'}, {...}]
518   - def search_friend_fields
519   - nil
520   - end
521   -
522 516 # -> Adds additional blocks to profiles and environments.
523 517 # Your plugin must implements a class method called 'extra_blocks'
524 518 # that returns a hash with the following syntax.
... ...
plugins/stoa/lib/ext/person.rb
... ... @@ -3,6 +3,8 @@ require_dependency &#39;person&#39;
3 3 class Person
4 4 attr_accessible :usp_id, :invitation_code
5 5  
  6 + SEARCHABLE_FIELDS[:usp_id] = 5
  7 +
6 8 validates_uniqueness_of :usp_id, :allow_nil => true
7 9 settings_items :invitation_code
8 10 validate :usp_id_or_invitation, :if => lambda { |person| person.environment && person.environment.plugin_enabled?(StoaPlugin)}
... ...
test/functional/invite_controller_test.rb
... ... @@ -271,31 +271,6 @@ class InviteControllerTest &lt; ActionController::TestCase
271 271 assert_equal [{"id" => friend1.id, "name" => friend1.name}].to_json, @response.body
272 272 end
273 273  
274   - should 'search friends profiles by fields provided by plugins' do
275   - class Plugin1 < Noosfero::Plugin
276   - def search_friend_fields
277   - [{:field => 'nickname'}, {:field => 'contact_phone'}]
278   - end
279   - end
280   - Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
281   -
282   - environment = Environment.default
283   - environment.enable_plugin(Plugin1.name)
284   -
285   - friend1 = create_user('harry').person
286   - friend2 = create_user('william').person
287   - friend1.nickname = 'prince'
288   - friend2.contact_phone = '2222'
289   - friend1.save
290   - friend2.save
291   -
292   - get :search_friend, :profile => profile.identifier, :q => 'prince'
293   - assert_equal [{"id" => friend1.id, "name" => friend1.name}].to_json, @response.body
294   -
295   - get :search_friend, :profile => profile.identifier, :q => '222'
296   - assert_equal [{"id" => friend2.id, "name" => friend2.name}].to_json, @response.body
297   - end
298   -
299 274 should 'invite registered users through profile id' do
300 275 friend1 = create_user('testuser1').person
301 276 friend2 = create_user('testuser2').person
... ...