diff options
-rw-r--r-- | app/controllers/application_controller.rb | 21 | ||||
-rw-r--r-- | app/controllers/public_body_controller.rb | 8 | ||||
-rw-r--r-- | app/controllers/request_controller.rb | 8 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 18 |
4 files changed, 40 insertions, 15 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b0351f7d1..8fd2da54a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -361,6 +361,27 @@ class ApplicationController < ActionController::Base def get_search_page_from_params return (params[:page] || "1").to_i end + def perform_search_typeahead(query, model) + # strip out unintended search operators - see + # https://github.com/sebbacon/alaveteli/issues/328 + # XXX this is a result of the OR hack below -- should fix by + # allowing a parameter to perform_search to control the + # default operator! + query = query.gsub(/(\s-\s|&)/, "") + query = query.split(/ +(?!-)/) + if query.last.nil? || query.last.strip.length < 3 + xapian_requests = nil + else + query = query.join(' OR ') # XXX: HACK for OR instead of default AND! + if model == PublicBody + collapse = nil + elsif model == InfoRequestEvent + collapse = 'request_collapse' + end + xapian_requests = perform_search([model], query, 'relevant', collapse, 5) + end + return xapian_requests + end # Store last visited pages, for contact form; but only for logged in users, as otherwise this breaks caching def set_last_request(info_request) diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index 94d1351db..659433c9e 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -186,13 +186,7 @@ class PublicBodyController < ApplicationController # Since acts_as_xapian doesn't support the Partial match flag, we work around it # by making the last work a wildcard, which is quite the same query = params[:query] - query = query.split(' ') - if query.last.nil? || query.last.strip.length < 3 - @xapian_requests = nil - else - query = query.join(' OR ') # XXX: HACK for OR instead of default AND! - @xapian_requests = perform_search([PublicBody], query, 'relevant', nil, 5) - end + @xapian_requests = perform_search_typeahead(query, PublicBody) render :partial => "public_body/search_ahead" end end diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index f3bbd6708..6e33fe043 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -755,13 +755,7 @@ class RequestController < ApplicationController # Since acts_as_xapian doesn't support the Partial match flag, we work around it # by making the last work a wildcard, which is quite the same query = params[:q] - query = query.split(' ') - if query.last.nil? || query.last.strip.length < 3 - @xapian_requests = nil - else - query = query.join(' OR ') # XXX: HACK for OR instead of default AND! - @xapian_requests = perform_search([InfoRequestEvent], query, 'relevant', 'request_collapse', 5) - end + @xapian_requests = perform_search_typeahead(query, InfoRequestEvent) render :partial => "request/search_ahead.rhtml" end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 2bf45e914..fc62edc14 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -1487,11 +1487,27 @@ describe RequestController, "when doing type ahead searches" do assigns[:xapian_requests].results[1][:model].title.should == info_requests(:naughty_chicken_request).title end - it "should not return matches for short words" do + it "should not return matches for short words" do get :search_typeahead, :q => "a" response.should render_template('request/_search_ahead.rhtml') assigns[:xapian_requests].should be_nil end + + it "should not give an error when user users unintended search operators" do + for phrase in ["Marketing/PR activities - Aldborough E-Act Free Schoo", + "Request for communications between DCMS/Ed Vaizey and ICO from Jan 1st 2011 - May ", + "Bellevue Road Ryde Isle of Wight PO33 2AR - what is the"] + lambda { + get :search_typeahead, :q => phrase + }.should_not raise_error(StandardError) + end + end + + it "should return all requests matching any of the given keywords" do + get :search_typeahead, :q => "dog -chicken" + assigns[:xapian_requests].results.size.should == 1 + end + end |