diff options
-rw-r--r-- | app/models/user.rb | 16 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 42 | ||||
-rw-r--r-- | vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb | 23 |
3 files changed, 68 insertions, 13 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index c9df7bf5c..49770ba2b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,7 +23,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: user.rb,v 1.91 2009-04-14 11:04:55 louise Exp $ +# $Id: user.rb,v 1.92 2009-04-21 22:48:41 francis Exp $ require 'digest/sha1' @@ -65,6 +65,20 @@ class User < ActiveRecord::Base "user" end + # if the URL name has changed, then all requested_by: queries + # will break unless we update index for every event for every + # request linked to it + after_save :reindex_requested_by + def reindex_requested_by + if self.changes.include?('url_name') + for info_request in self.info_requests + for info_request_event in info_request.info_request_events + info_request_event.xapian_mark_needs_index + end + end + end + end + def after_initialize if self.admin_level.nil? self.admin_level = 'none' diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index 0228713b2..114cae387 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -38,7 +38,7 @@ describe PublicBody, " when indexing public bodies with Xapian" do end describe " when indexing requests by body they are to" do - fixtures :public_bodies, :info_request_events + fixtures :public_bodies, :info_request_events, :info_requests before(:all) do rebuild_xapian_index @@ -75,6 +75,46 @@ describe " when indexing requests by body they are to" do end end +describe " when indexing requests by user they are from" do + fixtures :users, :info_request_events, :info_requests + + before(:all) do + rebuild_xapian_index + end + + it "should find requests from the user" do + xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 4 + end + + it "should update index correctly when URL name of user changes" do + verbose = false + + # initial search + xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 4 + models_found_before = xapian_object.results.map { |x| x[:model] } + + # change the URL name of the body + u= users(:bob_smith_user) + u.name = 'Robert Smith' + u.save! + u.url_name.should == 'robert_smith' + ActsAsXapian.update_index(true, verbose) # true = flush to disk + + # check we get results expected + xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 0 + xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:robert_smith", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 4 + models_found_after = xapian_object.results.map { |x| x[:model] } + + models_found_before.should == models_found_after + end +end + + + diff --git a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb index 8be42ccde..53541cb66 100644 --- a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb +++ b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb @@ -199,22 +199,23 @@ module ActsAsXapian raise NoXapianRubyBindingsError.new("Xapian Ruby bindings not installed") unless ActsAsXapian.bindings_available raise "acts_as_xapian hasn't been called in any models" if @@init_values.empty? - # if DB is not nil, then we're already initialised, so don't do it again - return unless @@writable_db.nil? + # if DB is not nil, then we're already initialised, so don't do it + # again XXX reopen it each time, xapian_spec.rb needs this so database + # gets written twice correctly. + # return unless @@writable_db.nil? prepare_environment new_path = @@db_path + suffix raise "writable_suffix/suffix inconsistency" if @@writable_suffix && @@writable_suffix != suffix - if @@writable_db.nil? - # for indexing - @@writable_db = Xapian::WritableDatabase.new(new_path, Xapian::DB_CREATE_OR_OPEN) - @@term_generator = Xapian::TermGenerator.new() - @@term_generator.set_flags(Xapian::TermGenerator::FLAG_SPELLING, 0) - @@term_generator.database = @@writable_db - @@term_generator.stemmer = @@stemmer - @@writable_suffix = suffix - end + + # for indexing + @@writable_db = Xapian::WritableDatabase.new(new_path, Xapian::DB_CREATE_OR_OPEN) + @@term_generator = Xapian::TermGenerator.new() + @@term_generator.set_flags(Xapian::TermGenerator::FLAG_SPELLING, 0) + @@term_generator.database = @@writable_db + @@term_generator.stemmer = @@stemmer + @@writable_suffix = suffix end ###################################################################### |