diff options
-rw-r--r-- | app/controllers/public_body_controller.rb | 13 | ||||
-rw-r--r-- | app/controllers/request_controller.rb | 14 | ||||
-rwxr-xr-x | app/helpers/link_to_helper.rb | 11 | ||||
-rw-r--r-- | app/views/layouts/default.rhtml | 2 | ||||
-rw-r--r-- | app/views/public_body/_search_ahead.rhtml | 2 | ||||
-rw-r--r-- | app/views/request/_search_ahead.rhtml | 2 | ||||
-rw-r--r-- | doc/CHANGES.md | 2 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 9 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 9 | ||||
-rw-r--r-- | spec/integration/view_request_spec.rb | 32 |
10 files changed, 72 insertions, 24 deletions
diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index ff9fbadb3..62229a441 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -185,11 +185,14 @@ class PublicBodyController < ApplicationController def search_typeahead # 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(' ').join(' OR ') # XXX: HACK for OR instead of default AND! - @xapian_requests = perform_search([PublicBody], query, 'relevant', nil, 5) - + 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([PublicBody], query, 'relevant', nil, 5) + end render :partial => "public_body/search_ahead" end end diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index f7d870b4f..a7558acf0 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -754,12 +754,14 @@ class RequestController < ApplicationController def search_typeahead # 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(' ').join(' OR ') # XXX: HACK for OR instead of default AND! - @xapian_requests = perform_search([InfoRequestEvent], query, 'relevant', 'request_collapse', 5) - + 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 render :partial => "request/search_ahead.rhtml" end diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb index 54b8d69d0..5866c31f0 100755 --- a/app/helpers/link_to_helper.rb +++ b/app/helpers/link_to_helper.rb @@ -185,9 +185,16 @@ module LinkToHelper end - def main_url(relative_path) + def main_url(relative_path, append = nil) url_prefix = "http://" + MySociety::Config.get("DOMAIN", '127.0.0.1:3000') - return url_prefix + relative_path + url = url_prefix + relative_path + if !append.nil? + env = Rack::MockRequest.env_for(url) + req = Rack::Request.new(env) + req.path_info += append + url = req.url + end + return url end # Basic date format diff --git a/app/views/layouts/default.rhtml b/app/views/layouts/default.rhtml index 675bb38de..2f8e0bf36 100644 --- a/app/views/layouts/default.rhtml +++ b/app/views/layouts/default.rhtml @@ -49,7 +49,7 @@ <% end %> <% end %> <% if @has_json %> - <link rel="alternate" type="application/json" title="JSON version of this page" href="<%=h main_url(request.request_uri) %>.json"> + <link rel="alternate" type="application/json" title="JSON version of this page" href="<%=h main_url(request.request_uri, '.json') %>"> <% end %> <% if @no_crawl %> diff --git a/app/views/public_body/_search_ahead.rhtml b/app/views/public_body/_search_ahead.rhtml index 19c7eb4e8..436471544 100644 --- a/app/views/public_body/_search_ahead.rhtml +++ b/app/views/public_body/_search_ahead.rhtml @@ -1,4 +1,5 @@ <p> + <% if !@xapian_requests.nil? %> <% if @xapian_requests.results.size > 0 %> <h3><%= _('Top search results:') %></h3> <p> @@ -12,6 +13,7 @@ <%= render :partial => 'body_listing_single', :locals => { :public_body => result[:model] } %> <% end %> </div> + <% end %> </p> diff --git a/app/views/request/_search_ahead.rhtml b/app/views/request/_search_ahead.rhtml index 9c49680c3..d0b19de7d 100644 --- a/app/views/request/_search_ahead.rhtml +++ b/app/views/request/_search_ahead.rhtml @@ -1,4 +1,5 @@ <div id="request_search_ahead_results"> + <% if !@xapian_requests.nil? %> <% if @xapian_requests.results.size > 0 %> <h3><%= _("Possibly related requests:") %></h3> <% end %> @@ -9,4 +10,5 @@ <p> <a id="body-site-search-link" target="_blank"><%= _("Or search in their website for this information.") %></a> </p> + <% end %> </div> diff --git a/doc/CHANGES.md b/doc/CHANGES.md index ea09de187..9afdc9e7c 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -3,6 +3,8 @@ ## Highlighted features * It should now be possible to develop the software on OSX * Base design refactored: CSS simplified and reduced, base design colours removed, now provided in example Alaveteli theme override +* It is now possible to rebuild the xapian index for specific terms, rather than having to drop and rebuild the entire database every time (as previously). See rake xapian:rebuild_index for more info. +* When listing authorities, show all authorities in default locale, rather than only those in the currently selected locale. ## Upgrade notes * **IMPORTANT! We now depend on Xapian 1.2**, which means you may need to install Xapian from backports. See [issue #159] for more info. diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index d47ea7b8d..8182e1331 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -177,7 +177,7 @@ describe PublicBodyController, "when doing type ahead searches" do it "should return nothing for the empty query string" do get :search_typeahead, :q => "" response.should render_template('public_body/_search_ahead') - assigns[:xapian_requests].results.size.should == 0 + assigns[:xapian_requests].should be_nil end it "should return a body matching the given keyword, but not users with a matching description" do @@ -202,10 +202,9 @@ describe PublicBodyController, "when doing type ahead searches" do assigns[:xapian_requests].results[0][:model].name.should == public_bodies(:humpadink_public_body).name end - it "should return partial matches" do - get :search_typeahead, :q => "geral" # 'geral' for 'Geraldine' + it "should not return matches for short words" do + get :search_typeahead, :q => "b" response.should render_template('public_body/_search_ahead') - assigns[:xapian_requests].results.size.should == 1 - assigns[:xapian_requests].results[0][:model].name.should == public_bodies(:geraldine_public_body).name + assigns[:xapian_requests].should be_nil end end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 9eddbc294..74a55062d 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -1458,7 +1458,7 @@ describe RequestController, "when doing type ahead searches" do it "should return nothing for the empty query string" do get :search_typeahead, :q => "" response.should render_template('request/_search_ahead.rhtml') - assigns[:xapian_requests].results.size.should == 0 + assigns[:xapian_requests].should be_nil end it "should return a request matching the given keyword, but not users with a matching description" do @@ -1476,11 +1476,10 @@ 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 return partial matches" do - get :search_typeahead, :q => "chick" # 'chick' for 'chicken' + 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].results.size.should == 1 - assigns[:xapian_requests].results[0][:model].title.should == info_requests(:naughty_chicken_request).title + assigns[:xapian_requests].should be_nil end end diff --git a/spec/integration/view_request_spec.rb b/spec/integration/view_request_spec.rb new file mode 100644 index 000000000..cf1e4ca6c --- /dev/null +++ b/spec/integration/view_request_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "When viewing requests" do + + fixtures [ + :users, + :public_bodies, + :public_body_translations, + :public_body_versions, + :info_requests, + :raw_emails, + :outgoing_messages, + :incoming_messages, + :comments, + :info_request_events, + :track_things, + ] + + before(:each) do + emails = raw_emails.clone + load_raw_emails_data(emails) + end + + it "should not make endlessly recursive JSON <link>s" do + @dog_request = info_requests(:fancy_dog_request) + get "request/#{@dog_request.url_title}?unfold=1" + response.body.should_not include("dog?unfold=1.json") + response.body.should include("dog.json?unfold=1") + end + +end + |