From 27d7172543005be9f97f6b2f37ee5f0672aa3f7b Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 22 May 2013 03:40:24 +0200 Subject: [PATCH] Improved organization search. --- app/controllers/search_controller.rb | 9 +++++---- app/models/organization.rb | 12 +++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 8875b0db6..1e9b7b1f3 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -21,8 +21,8 @@ class SearchController < ApplicationController # do query users = User.search( - :query => params[:term], - :limit => params[:limit], + :query => params[:term], + :limit => params[:limit], :current_user => current_user, ) user_result = [] @@ -32,10 +32,11 @@ class SearchController < ApplicationController end organizations = Organization.search( - :query => params[:term], - :limit => params[:limit], + :query => params[:term], + :limit => params[:limit], :current_user => current_user, ) + organizations_data = {} organization_result = [] organizations.each do |organization| diff --git a/app/models/organization.rb b/app/models/organization.rb index 67747c88d..dc5f26451 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -16,9 +16,19 @@ class Organization < ApplicationModel organizations = Organization.find( :all, :limit => limit, - :conditions => ['name LIKE ?', "%#{query}%"], + :conditions => ['name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"], :order => 'name' ) + + # if only a few organizations are found, search for names of users + if organizations.length <= 3 + organizations = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').find( + :all, + :limit => limit, + :conditions => ['users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"], + :order => 'organizations.name' + ) + end return organizations end