Commit 844304db138e2c7bb6234be39301f054b7a657cb

Authored by Larissa Reis
1 parent fb93be11

Plugins can provide additional search fields for invites

app/controllers/public/invite_controller.rb
... ... @@ -74,8 +74,11 @@ class InviteController < PublicController
74 74 end
75 75 end
76 76  
  77 + include InviteHelper
  78 + helper :invite
  79 +
77 80 def search_friend
78   - fields = %w[name identifier email]
  81 + fields = %w[name identifier email] + plugins_options.map {|field| field[:field].to_s }
79 82 values = ["%#{params['q']}%"] * fields.count
80 83 render :text => environment.people.find(:all, :joins => ['INNER JOIN users ON profiles.user_id=users.id'], :conditions => [fields.map {|field| "LOWER(#{field}) LIKE ?"}.join(' OR '), *values]).
81 84 map {|person| {:id => person.id, :name => person.name} }.to_json
... ...
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
2 17 end
... ...
app/views/invite/invite_friends.rhtml
... ... @@ -12,7 +12,7 @@
12 12 <% form_tag :action => 'invite_registered_friend' do %>
13 13 <% search_action = url_for(:action => 'search_friend') %>
14 14 <%= token_input_field_tag(:q, 'search-friends', search_action,
15   - { :hint_text => _('Type in a search for your friend'),
  15 + { :hint_text => _('Type in your friend\'s ') + search_friend_fields,
16 16 :focus => false }) %>
17 17  
18 18 <%= render :partial => 'invite/personalize_invitation_mail', :locals => {:mail_template => @mail_template } %>
... ...
lib/noosfero/plugin.rb
... ... @@ -437,6 +437,12 @@ class Noosfero::Plugin
437 437 nil
438 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 +
440 446 # -> Adds additional blocks to profiles and environments.
441 447 # Your plugin must implements a class method called 'extra_blocks'
442 448 # that returns a hash with the following syntax.
... ...
test/functional/invite_controller_test.rb
... ... @@ -255,6 +255,30 @@ class InviteControllerTest &lt; ActionController::TestCase
255 255 assert_equal [{"name" => friend1.name, "id" => friend1.id}, {"name" => friend2.name, "id" => friend2.id}].to_json, @response.body
256 256 end
257 257  
  258 + should 'search friends profiles by fields provided by plugins' do
  259 + class Plugin1 < Noosfero::Plugin
  260 + def search_friend_fields
  261 + [{:field => 'nickname'}, {:field => 'contact_phone'}]
  262 + end
  263 + end
  264 +
  265 + environment = Environment.default
  266 + environment.enable_plugin(Plugin1)
  267 +
  268 + friend1 = create_user('harry').person
  269 + friend2 = create_user('william').person
  270 + friend1.nickname = 'prince'
  271 + friend2.contact_phone = '2222'
  272 + friend1.save
  273 + friend2.save
  274 +
  275 + get :search_friend, :profile => profile.identifier, :q => 'prince'
  276 + assert_equal [{"name" => friend1.name, "id" => friend1.id}].to_json, @response.body
  277 +
  278 + get :search_friend, :profile => profile.identifier, :q => '222'
  279 + assert_equal [{"name" => friend2.name, "id" => friend2.id}].to_json, @response.body
  280 + end
  281 +
258 282 should 'invite registered users through profile id' do
259 283 friend1 = create_user('testuser1').person
260 284 friend2 = create_user('testuser2').person
... ...