diff options
-rw-r--r-- | app/models/user.rb | 33 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 42 | ||||
-rw-r--r-- | todo.txt | 1 |
3 files changed, 53 insertions, 23 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 49770ba2b..d48e2975d 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.92 2009-04-21 22:48:41 francis Exp $ +# $Id: user.rb,v 1.93 2009-04-22 01:00:23 francis Exp $ require 'digest/sha1' @@ -65,20 +65,6 @@ 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' @@ -89,14 +75,17 @@ class User < ActiveRecord::Base after_save :reindex_referencing_models def reindex_referencing_models return if no_xapian_reindex == true - for comment in self.comments - for info_request_event in comment.info_request_events - info_request_event.xapian_mark_needs_index + + if self.changes.include?('url_name') + for comment in self.comments + for info_request_event in comment.info_request_events + info_request_event.xapian_mark_needs_index + end end - end - for info_request in self.info_requests - for info_request_event in info_request.info_request_events - info_request_event.xapian_mark_needs_index + 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 diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index 114cae387..9df56d1ca 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -113,6 +113,48 @@ describe " when indexing requests by user they are from" do end end +describe " when indexing comments by user they are by" do + fixtures :users, :info_request_events, :info_requests, :comments + + before(:all) do + rebuild_xapian_index + end + + it "should find requests from the user" do + xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_emnameem", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 1 + end + + it "should update index correctly when URL name of user changes" do + verbose = false + + # initial search + xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_emnameem", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 1 + models_found_before = xapian_object.results.map { |x| x[:model] } + + # change the URL name of the body + u = users(:silly_name_user) + u.name = 'Silly Name' + u.save! + u.url_name.should == 'silly_name' + ActsAsXapian.update_index(true, verbose) # true = flush to disk + + # check we get results expected + xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_emnameem", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 0 + xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_name", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 1 + models_found_after = xapian_object.results.map { |x| x[:model] } + + models_found_before.should == models_found_after + end +end + + + + + @@ -3,7 +3,6 @@ When a InfoRequest's url_title changes: [ :request, 'R', "request" ] - all events for that request When User's url_name changes: - [ :requested_by, 'B', "requested_by" ] - all events for all requests by that user [ :commented_by, 'C', "commented_by" ] - all comments made by that user Rebuild all index - turn off cron meanwhile? |