diff options
-rwxr-xr-x | app/helpers/link_to_helper.rb | 4 | ||||
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 6 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 18 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 2 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 14 | ||||
-rw-r--r-- | spec/controllers/user_controller_spec.rb | 70 | ||||
-rw-r--r-- | spec/helpers/link_to_helper_spec.rb | 12 | ||||
-rw-r--r-- | spec/integration/search_request_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 22 | ||||
-rw-r--r-- | spec/spec_helper.rb | 56 | ||||
-rw-r--r-- | vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb | 91 |
11 files changed, 167 insertions, 130 deletions
diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb index 994da0e87..030fab20b 100755 --- a/app/helpers/link_to_helper.rb +++ b/app/helpers/link_to_helper.rb @@ -255,9 +255,11 @@ module LinkToHelper # Basic date format def simple_date(date) + date = date.in_time_zone.to_date unless date.is_a? Date + date_format = _("simple_date_format") date_format = :long if date_format == "simple_date_format" - return I18n.l(date.to_date, :format => date_format) + return I18n.l(date, :format => date_format) end def simple_time(date) diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index 935f8eab6..830486493 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -144,7 +144,7 @@ describe GeneralController, "when showing the front page with fixture data" do describe 'when constructing the list of recent requests' do before(:each) do - rebuild_xapian_index + get_fixtures_xapian_index end describe 'when there are fewer than five successful requests' do @@ -189,8 +189,8 @@ describe GeneralController, 'when using xapian search' do # rebuild xapian index after fixtures loaded before(:each) do - load_raw_emails_data - rebuild_xapian_index + load_raw_emails_data + get_fixtures_xapian_index end it "should redirect from search query URL to pretty URL" do diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index d12818a1c..29ece18cb 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -6,7 +6,7 @@ describe PublicBodyController, "when showing a body" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should be successful" do @@ -29,14 +29,14 @@ describe PublicBodyController, "when showing a body" do assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all( :conditions => ["public_body_id = ?", public_bodies(:geraldine_public_body).id]) end - + it "should assign the requests (2)" do get :show, :url_name => "tgq", :view => 'successful' assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all( :conditions => ["described_state = ? and public_body_id = ?", "successful", public_bodies(:geraldine_public_body).id]) end - + it "should assign the requests (3)" do get :show, :url_name => "dfh", :view => 'all' assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all( @@ -66,7 +66,7 @@ describe PublicBodyController, "when showing a body" do ActionController::Routing::Routes.filters = old_filters end - + it "should redirect to newest name if you use historic name of public body in URL" do get :show, :url_name => "hdink", :view => 'all' response.should redirect_to(:controller => 'public_body', :action => 'show', :url_name => "dfh") @@ -148,7 +148,7 @@ describe PublicBodyController, "when listing bodies" do get :list, :tag => "other" response.should render_template('list') assigns[:public_bodies].should =~ PublicBody.all(:conditions => "id not in (#{public_bodies(:humpadink_public_body).id}, #{PublicBody.internal_admin_body.id})") - + get :list response.should render_template('list') assigns[:public_bodies].should =~ PublicBody.all(:conditions => "id <> #{PublicBody.internal_admin_body.id}") @@ -194,10 +194,10 @@ end describe PublicBodyController, "when doing type ahead searches" do integrate_views - + before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should return nothing for the empty query string" do @@ -205,7 +205,7 @@ describe PublicBodyController, "when doing type ahead searches" do response.should render_template('public_body/_search_ahead') assigns[:xapian_requests].should be_nil end - + it "should return a body matching the given keyword, but not users with a matching description" do get :search_typeahead, :query => "Geraldine" response.should render_template('public_body/_search_ahead') @@ -230,7 +230,7 @@ describe PublicBodyController, "when doing type ahead searches" do end it "should not return matches for short words" do - get :search_typeahead, :query => "b" + get :search_typeahead, :query => "b" response.should render_template('public_body/_search_ahead') assigns[:xapian_requests].should be_nil end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index b0223588e..e898fb91b 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -5,7 +5,7 @@ describe RequestController, "when listing recent requests" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should be successful" do diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index 7daa23769..c785960b5 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -38,7 +38,7 @@ describe TrackController, "when making a new track on a request" do get :track_request, :url_title => @ir.url_title, :feed => 'track' response.should redirect_to(:controller => 'request', :action => 'show', :url_title => @ir.url_title) end - + it "should 404 for non-existent requests" do session[:user_id] = @user.id lambda { @@ -61,9 +61,9 @@ describe TrackController, "when sending alerts for a track" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end - + it "should send alerts" do # Don't do clever locale-insertion-unto-URL stuff old_filters = ActionController::Routing::Routes.filters @@ -138,7 +138,7 @@ describe TrackController, "when viewing RSS feed for a track" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should get the RSS feed" do @@ -168,7 +168,7 @@ describe TrackController, "when viewing JSON version of a track feed" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should get the feed" do @@ -210,9 +210,9 @@ describe TrackController, "when tracking a public body" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end - + it "should work" do geraldine = public_bodies(:geraldine_public_body) get :track_public_body, :feed => 'feed', :url_name => geraldine.url_name diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index 386d1b04b..23006803b 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -8,9 +8,9 @@ describe UserController, "when showing a user" do integrate_views before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end - + it "should be successful" do get :show, :url_name => "bob_smith" response.should be_success @@ -45,7 +45,7 @@ describe UserController, "when showing a user" do get :show, :url_name => "bob_smith" assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all( :conditions => "user_id = #{users(:bob_smith_user).id}") - + get :show, :url_name => "bob_smith", :user_query => "money" assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ [ info_requests(:naughty_chicken_request), @@ -218,7 +218,7 @@ describe UserController, "when signing in" do # Get the confirmation URL, and check we’re still Joe get :confirm, :email_token => post_redirect.email_token session[:user_id].should == users(:admin_user).id - + # And the redirect should still work, of course response.should redirect_to(:controller => 'request', :action => 'list', :post_redirect => 1) @@ -232,21 +232,21 @@ describe UserController, "when signing up" do it "should be an error if you type the password differently each time" do post :signup, { :user_signup => { :email => 'new@localhost', :name => 'New Person', - :password => 'sillypassword', :password_confirmation => 'sillypasswordtwo' } + :password => 'sillypassword', :password_confirmation => 'sillypasswordtwo' } } assigns[:user_signup].errors[:password].should == 'Please enter the same password twice' end it "should be an error to sign up with a misformatted email" do post :signup, { :user_signup => { :email => 'malformed-email', :name => 'Mr Malformed', - :password => 'sillypassword', :password_confirmation => 'sillypassword' } + :password => 'sillypassword', :password_confirmation => 'sillypassword' } } assigns[:user_signup].errors[:email].should_not be_nil end it "should send confirmation mail if you fill in the form right" do post :signup, { :user_signup => { :email => 'new@localhost', :name => 'New Person', - :password => 'sillypassword', :password_confirmation => 'sillypassword' } + :password => 'sillypassword', :password_confirmation => 'sillypassword' } } response.should render_template('confirm') @@ -270,13 +270,13 @@ describe UserController, "when signing up" do it "should send special 'already signed up' mail if you fill the form in with existing registered email" do post :signup, { :user_signup => { :email => 'silly@localhost', :name => 'New Person', - :password => 'sillypassword', :password_confirmation => 'sillypassword' } + :password => 'sillypassword', :password_confirmation => 'sillypassword' } } response.should render_template('confirm') deliveries = ActionMailer::Base.deliveries deliveries.size.should == 1 - + # This text may span a line break, depending on the length of the SITE_NAME deliveries[0].body.should match(/when\s+you\s+already\s+have\s+an/) end @@ -377,7 +377,7 @@ describe UserController, "when changing password" do get :signchangepassword response.should render_template('signchangepassword') end - + it "should change the password, if you have right to do so" do session[:user_id] = users(:bob_smith_user).id session[:user_circumstance] = "change_password" @@ -437,8 +437,8 @@ describe UserController, "when changing email address" do it "should be an error if the password is wrong, everything else right" do @user = users(:bob_smith_user) session[:user_id] = @user.id - - post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost', + + post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost', :password => 'donotknowpassword', :new_email => 'newbob@localhost' }, :submitted_signchangeemail_do => 1 } @@ -455,8 +455,8 @@ describe UserController, "when changing email address" do it "should be an error if old email is wrong, everything else right" do @user = users(:bob_smith_user) session[:user_id] = @user.id - - post :signchangeemail, { :signchangeemail => { :old_email => 'bob@moo', + + post :signchangeemail, { :signchangeemail => { :old_email => 'bob@moo', :password => 'jonespassword', :new_email => 'newbob@localhost' }, :submitted_signchangeemail_do => 1 } @@ -473,8 +473,8 @@ describe UserController, "when changing email address" do it "should work even if the old email had a case difference" do @user = users(:bob_smith_user) session[:user_id] = @user.id - - post :signchangeemail, { :signchangeemail => { :old_email => 'BOB@localhost', + + post :signchangeemail, { :signchangeemail => { :old_email => 'BOB@localhost', :password => 'jonespassword', :new_email => 'newbob@localhost' }, :submitted_signchangeemail_do => 1 } @@ -485,8 +485,8 @@ describe UserController, "when changing email address" do it "should send confirmation email if you get all the details right" do @user = users(:bob_smith_user) session[:user_id] = @user.id - - post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost', + + post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost', :password => 'jonespassword', :new_email => 'newbob@localhost' }, :submitted_signchangeemail_do => 1 } @@ -521,16 +521,16 @@ describe UserController, "when changing email address" do post_redirect = PostRedirect.find_by_email_token(mail_token) post_redirect.circumstance.should == 'change_email' post_redirect.user.should == users(:bob_smith_user) - post_redirect.post_params.should == {"submitted_signchangeemail_do"=>"1", - "action"=>"signchangeemail", + post_redirect.post_params.should == {"submitted_signchangeemail_do"=>"1", + "action"=>"signchangeemail", "signchangeemail"=>{ - "old_email"=>"bob@localhost", - "new_email"=>"newbob@localhost"}, + "old_email"=>"bob@localhost", + "new_email"=>"newbob@localhost"}, "controller"=>"user"} post :signchangeemail, post_redirect.post_params response.should redirect_to(:controller => 'user', :action => 'show', :url_name => 'bob_smith') - flash[:notice].should match(/You have now changed your email address/) + flash[:notice].should match(/You have now changed your email address/) @user.reload @user.email.should == 'newbob@localhost' @user.email_confirmed.should == true @@ -539,8 +539,8 @@ describe UserController, "when changing email address" do it "should send special 'already signed up' mail if you try to change your email to one already used" do @user = users(:bob_smith_user) session[:user_id] = @user.id - - post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost', + + post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost', :password => 'jonespassword', :new_email => 'silly@localhost' }, :submitted_signchangeemail_do => 1 } @@ -572,9 +572,9 @@ describe UserController, "when using profile photos" do @uploadedfile_2 = File.open(file_fixture_name("parrot.jpg")) @uploadedfile_2.stub!(:original_filename).and_return('parrot.jpg') end - + it "should not let you change profile photo if you're not logged in as the user" do - post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 } + post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 } end it "should return a 404 not a 500 when a profile photo has not been set" do @@ -588,10 +588,10 @@ describe UserController, "when using profile photos" do @user.profile_photo.should be_nil session[:user_id] = @user.id - post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 } + post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 } response.should redirect_to(:controller => 'user', :action => 'show', :url_name => "bob_smith") - flash[:notice].should match(/Thank you for updating your profile photo/) + flash[:notice].should match(/Thank you for updating your profile photo/) @user.reload @user.profile_photo.should_not be_nil @@ -601,13 +601,13 @@ describe UserController, "when using profile photos" do @user.profile_photo.should be_nil session[:user_id] = @user.id - post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 } + post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 } response.should redirect_to(:controller => 'user', :action => 'show', :url_name => "bob_smith") - flash[:notice].should match(/Thank you for updating your profile photo/) + flash[:notice].should match(/Thank you for updating your profile photo/) - post :set_profile_photo, { :id => @user.id, :file => @uploadedfile_2, :submitted_draft_profile_photo => 1, :automatically_crop => 1 } + post :set_profile_photo, { :id => @user.id, :file => @uploadedfile_2, :submitted_draft_profile_photo => 1, :automatically_crop => 1 } response.should redirect_to(:controller => 'user', :action => 'show', :url_name => "bob_smith") - flash[:notice].should match(/Thank you for updating your profile photo/) + flash[:notice].should match(/Thank you for updating your profile photo/) @user.reload @user.profile_photo.should_not be_nil @@ -617,7 +617,7 @@ describe UserController, "when using profile photos" do end describe UserController, "when showing JSON version for API" do - + it "should be successful" do get :show, :url_name => "bob_smith", :format => "json" @@ -634,7 +634,7 @@ describe UserController, "when viewing the wall" do integrate_views before(:each) do - rebuild_xapian_index + get_fixtures_xapian_index end it "should show users stuff on their wall, most recent first" do diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb index ef89e8bf9..030fd612d 100644 --- a/spec/helpers/link_to_helper_spec.rb +++ b/spec/helpers/link_to_helper_spec.rb @@ -73,4 +73,16 @@ describe LinkToHelper do end end end + + describe 'simple_date' do + it 'should respect time zones' do + Time.use_zone('Australia/Sydney') do + simple_date(Time.utc(2012, 11, 07, 21, 30, 26)).should == 'November 08, 2012' + end + end + + it 'should handle Date objects' do + simple_date(Date.new(2012, 11, 21)).should == 'November 21, 2012' + end + end end diff --git a/spec/integration/search_request_spec.rb b/spec/integration/search_request_spec.rb index 17a7b4aaa..c564032a6 100644 --- a/spec/integration/search_request_spec.rb +++ b/spec/integration/search_request_spec.rb @@ -4,7 +4,7 @@ describe "When searching" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should not strip quotes from quoted query" do diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index 195b39eee..8c99d550f 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -4,9 +4,9 @@ describe User, " when indexing users with Xapian" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end - + it "should search by name" do # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) xapian_object = InfoRequest.full_search([User], "Silly", 'created_at', true, nil, 100, 1) @@ -21,7 +21,7 @@ describe User, " when indexing users with Xapian" do xapian_object = InfoRequest.full_search([User], "stuff", 'created_at', true, nil, 100, 1) xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == user - + user.about_me = "I am really an aardvark, true story." user.save! update_xapian_index @@ -38,7 +38,7 @@ end describe PublicBody, " when indexing public bodies with Xapian" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should search index the main name field" do @@ -71,7 +71,7 @@ describe PublicBody, " when indexing requests by body they are to" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should find requests to the body" do @@ -126,7 +126,7 @@ end describe User, " when indexing requests by user they are from" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should find requests from the user" do @@ -204,7 +204,7 @@ end describe User, " when indexing comments by user they are by" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should find requests from the user" do @@ -239,7 +239,7 @@ end describe InfoRequest, " when indexing requests by their title" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should find events for the request" do @@ -268,7 +268,7 @@ end describe InfoRequest, " when indexing requests by tag" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should find request by tag, even when changes" do @@ -289,7 +289,7 @@ end describe PublicBody, " when indexing authorities by tag" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should find request by tag, even when changes" do @@ -313,7 +313,7 @@ end describe PublicBody, " when only indexing selected things on a rebuild" do before(:each) do load_raw_emails_data - rebuild_xapian_index + get_fixtures_xapian_index end it "should only index what we ask it to" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e6d81540b..d4dad591d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,7 +23,6 @@ FakeWeb.register_uri(:purge, %r|varnish.localdomain|, :body => "OK") # Use test-specific translations FastGettext.add_text_domain 'app', :path => File.join(File.dirname(__FILE__), 'fixtures', 'locale'), :type => :po FastGettext.default_text_domain = 'app' - Spec::Runner.configure do |config| # If you're not using ActiveRecord you should remove these # lines, delete config/database.yml and disable :active_record @@ -47,6 +46,7 @@ Spec::Runner.configure do |config| :holidays, :track_things_sent_emails + # == Fixtures # # You can declare fixtures for each example_group like this: @@ -99,6 +99,19 @@ def load_file_fixture(file_name) return content end +def parse_all_incoming_messages + IncomingMessage.find(:all).each{ |x| x.parse_raw_email! } +end + +def load_raw_emails_data + raw_emails_yml = File.join(Spec::Runner.configuration.fixture_path, "raw_emails.yml") + for raw_email_id in YAML::load_file(raw_emails_yml).map{|k,v| v["id"]} do + raw_email = RawEmail.find(raw_email_id) + raw_email.data = load_file_fixture("raw_emails/%d.email" % [raw_email_id]) + end +end + +# Rebuild the current xapian index def rebuild_xapian_index(terms = true, values = true, texts = true, dropfirst = true) if dropfirst begin @@ -110,16 +123,35 @@ def rebuild_xapian_index(terms = true, values = true, texts = true, dropfirst = ActsAsXapian.writable_db.close end parse_all_incoming_messages - verbose = false # safe_rebuild=true, which involves forking to avoid memory leaks, doesn't work well with rspec. # unsafe is significantly faster, and we can afford possible memory leaks while testing. - safe_rebuild = false - ActsAsXapian.rebuild_index(["PublicBody", "User", "InfoRequestEvent"].map{|m| m.constantize}, verbose, terms, values, texts, safe_rebuild) + models = [PublicBody, User, InfoRequestEvent] + ActsAsXapian.rebuild_index(models, verbose=false, terms, values, texts, safe_rebuild=false) +end + +# Create a clean xapian index based on the fixture files and the raw_email data. +def create_fixtures_xapian_index + load_raw_emails_data + rebuild_xapian_index end def update_xapian_index - verbose = false - ActsAsXapian.update_index(flush_to_disk=false, verbose) + ActsAsXapian.update_index(flush_to_disk=false, verbose=false) +end + +# Copy the xapian index created in create_fixtures_xapian_index to a temporary +# copy at the same level and point xapian at the copy +def get_fixtures_xapian_index() + # Create a base index for the fixtures if not already created + $existing_xapian_db ||= create_fixtures_xapian_index + # Store whatever the xapian db path is originally + $original_xapian_path ||= ActsAsXapian.db_path + path_array = $original_xapian_path.split(File::Separator) + path_array.pop + temp_path = File.join(path_array, 'test.temp') + FileUtils.remove_entry_secure(temp_path, force=true) + FileUtils.cp_r($original_xapian_path, temp_path) + ActsAsXapian.db_path = temp_path end # Validate an entire HTML page @@ -200,22 +232,10 @@ def safe_mock_model(model, args = {}) mock end -def load_raw_emails_data - raw_emails_yml = File.join(Spec::Runner.configuration.fixture_path, "raw_emails.yml") - for raw_email_id in YAML::load_file(raw_emails_yml).map{|k,v| v["id"]} do - raw_email = RawEmail.find(raw_email_id) - raw_email.data = load_file_fixture("raw_emails/%d.email" % [raw_email_id]) - end -end - def get_fixture_mail(filename) MailHandler.mail_from_raw_email(load_file_fixture(filename)) end -def parse_all_incoming_messages - IncomingMessage.find(:all).each{|x| x.parse_raw_email!} -end - def load_test_categories PublicBodyCategories.add(:en, [ "Local and regional", 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 d5c0e89c6..374fcd65b 100644 --- a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb +++ b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb @@ -16,7 +16,7 @@ begin require 'xapian' $acts_as_xapian_bindings_available = true rescue LoadError - STDERR.puts "acts_as_xapian: No Ruby bindings for Xapian installed" + STDERR.puts "acts_as_xapian: No Ruby bindings for Xapian installed" $acts_as_xapian_bindings_available = false end @@ -46,6 +46,9 @@ module ActsAsXapian def ActsAsXapian.db @@db end + def ActsAsXapian.db_path=(db_path) + @@db_path = db_path + end def ActsAsXapian.db_path @@db_path end @@ -110,14 +113,14 @@ module ActsAsXapian end # Opens / reopens the db for reading - # XXX we perhaps don't need to rebuild database and enquire and queryparser - + # XXX we perhaps don't need to rebuild database and enquire and queryparser - # but db.reopen wasn't enough by itself, so just do everything it's easier. def ActsAsXapian.readable_init 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? - + prepare_environment - + # We need to reopen the database each time, so Xapian gets changes to it. # Calling reopen() does not always pick up changes for reasons that I can # only speculate about at the moment. (It is easy to reproduce this by @@ -126,7 +129,7 @@ module ActsAsXapian if !@@db.nil? @@db.close end - + # basic Xapian objects begin @@db = Xapian::Database.new(@@db_path) @@ -188,7 +191,7 @@ module ActsAsXapian # If making acts_as_xapian generic, would really need to make the :terms have # another option that lets people choose non-boolean for terms that need it # (i.e. searching explicitly within a free text field) - @@query_parser.add_boolean_prefix(term[2], term[1]) + @@query_parser.add_boolean_prefix(term[2], term[1]) end end if options[:values] @@ -198,9 +201,9 @@ module ActsAsXapian # date types are special, mark them so the first model they're seen for if !@@values_by_number.include?(value[1]) - if value[3] == :date + if value[3] == :date value_range = Xapian::DateValueRangeProcessor.new(value[1]) - elsif value[3] == :string + elsif value[3] == :string value_range = Xapian::StringValueRangeProcessor.new(value[1]) elsif value[3] == :number value_range = Xapian::NumberValueRangeProcessor.new(value[1]) @@ -212,7 +215,7 @@ module ActsAsXapian # stop it being garbage collected, as # add_valuerangeprocessor ref is outside Ruby's GC - @@value_ranges_store.push(value_range) + @@value_ranges_store.push(value_range) end @@values_by_number[value[1]] = value[2] @@ -230,7 +233,7 @@ module ActsAsXapian # again XXX reopen it each time, xapian_spec.rb needs this so database # gets written twice correctly. # return unless @@writable_db.nil? - + prepare_environment full_path = @@db_path + suffix @@ -246,7 +249,7 @@ module ActsAsXapian ###################################################################### # Search with a query or for similar models - + # Base class for Search and Similar below class QueryBase attr_accessor :offset @@ -271,12 +274,12 @@ module ActsAsXapian # Set self.query before calling this def initialize_query(options) #raise options.to_yaml - + self.runtime += Benchmark::realtime { offset = options[:offset] || 0; offset = offset.to_i limit = options[:limit] raise "please specifiy maximum number of results to return with parameter :limit" if not limit - limit = limit.to_i + limit = limit.to_i sort_by_prefix = options[:sort_by_prefix] || nil sort_by_ascending = options[:sort_by_ascending].nil? ? true : options[:sort_by_ascending] collapse_by_prefix = options[:collapse_by_prefix] || nil @@ -313,7 +316,7 @@ module ActsAsXapian tries += 1 delay *= 2 delay = MSET_MAX_DELAY if delay > MSET_MAX_DELAY - + ActsAsXapian.db.reopen() retry else @@ -336,7 +339,7 @@ module ActsAsXapian for t in self.query.terms term = t.term #x = x + term.to_yaml + term.size.to_s + term[0..0] + "*" - if term.size >= 2 && term[0..0] == 'Z' + if term.size >= 2 && term[0..0] == 'Z' # normal terms begin Z (for stemmed), then have no capital letter prefix if term[1..1] == term[1..1].downcase ret = true @@ -372,8 +375,8 @@ module ActsAsXapian # Pull out all the results iter = self.matches._begin while not iter.equals(self.matches._end) - docs.push({:data => iter.document.data, - :percent => iter.percent, + docs.push({:data => iter.document.data, + :percent => iter.percent, :weight => iter.weight, :collapse_count => iter.collapse_count}) iter.next @@ -403,14 +406,14 @@ module ActsAsXapian end # now get them in right order again results = [] - docs.each do |doc| + docs.each do |doc| k = doc[:data].split('-') model_instance = chash[[k[0], k[1].to_i]] if model_instance results << { :model => model_instance, - :percent => doc[:percent], - :weight => doc[:weight], - :collapse_count => doc[:collapse_count] } + :percent => doc[:percent], + :weight => doc[:weight], + :collapse_count => doc[:collapse_count] } end end self.cached_results = results @@ -428,7 +431,7 @@ module ActsAsXapian # essential to make sure the classes have been loaded, and thus # acts_as_xapian called on them, so we know the fields for the query # parser. - + # model_classes - model classes to search within, e.g. [PublicBody, # User]. Can take a single model class, or you can express the model # class names in strings if you like. @@ -443,7 +446,7 @@ module ActsAsXapian new_model_classes.push(model_class) end model_classes = new_model_classes - + # Set things up self.initialize_db @@ -518,7 +521,7 @@ module ActsAsXapian # object. This explains what exactly it does, which is to exclude # terms in the existing query. # http://thread.gmane.org/gmane.comp.search.xapian.general/3673/focus=3681 - eset = ActsAsXapian.enquire.eset(40, selection) + eset = ActsAsXapian.enquire.eset(40, selection) # Do main search for them self.important_terms = [] @@ -548,8 +551,8 @@ module ActsAsXapian ###################################################################### # Index - - # Offline indexing job queue model, create with migration made + + # Offline indexing job queue model, create with migration made # using "script/generate acts_as_xapian" as described in ../README.txt class ActsAsXapianJob < ActiveRecord::Base end @@ -561,7 +564,7 @@ module ActsAsXapian # logging in the database that it has been. def ActsAsXapian.update_index(flush = false, verbose = false) # STDOUT.puts("start of ActsAsXapian.update_index") if verbose - + # Before calling writable_init we have to make sure every model class has been initialized. # i.e. has had its class code loaded, so acts_as_xapian has been called inside it, and # we have the info from acts_as_xapian. @@ -621,17 +624,17 @@ module ActsAsXapian STDERR.puts(detail.backtrace.join("\n") + "\nFAILED ActsAsXapian.update_index job #{id} #{$!} " + (job.nil? ? "" : "model " + job.model + " id " + job.model_id.to_s)) end end - # We close the database when we're finished to remove the lock file. Since writable_init - # reopens it and recreates the environment every time we don't need to do further cleanup + # We close the database when we're finished to remove the lock file. Since writable_init + # reopens it and recreates the environment every time we don't need to do further cleanup ActsAsXapian.writable_db.flush ActsAsXapian.writable_db.close end - + def ActsAsXapian._is_xapian_db(path) is_db = File.exist?(File.join(path, "iamflint")) || File.exist?(File.join(path, "iamchert")) return is_db end - + # You must specify *all* the models here, this totally rebuilds the Xapian # database. You'll want any readers to reopen the database after this. # @@ -672,7 +675,7 @@ module ActsAsXapian end ActsAsXapian.writable_db.flush ActsAsXapian.writable_db.close - end + end # Rename into place temp_path = old_path + ".tmp" @@ -728,7 +731,7 @@ module ActsAsXapian model.xapian_index(terms, values, texts) end ActsAsXapian.writable_db.flush - ActsAsXapian.writable_db.close + ActsAsXapian.writable_db.close # database connection won't survive a fork, so shut it down ActiveRecord::Base.connection.disconnect! # brutal exit, so other shutdown code not run (for speed and safety) @@ -741,7 +744,7 @@ module ActsAsXapian ###################################################################### # Instance methods that get injected into your model. - + module InstanceMethods # Used internally def xapian_document_term @@ -755,7 +758,7 @@ module ActsAsXapian else values = [] for locale in self.translations.map{|x| x.locale} - self.class.with_locale(locale) do + self.class.with_locale(locale) do values << single_xapian_value(field, type=type) end end @@ -866,7 +869,7 @@ module ActsAsXapian end end end - + for term in terms_to_index value = xapian_value(term[0]) if value.kind_of?(Array) @@ -877,11 +880,11 @@ module ActsAsXapian doc.add_term(term[1] + value) end end - + if values - doc.clear_values + doc.clear_values for value in values_to_index - doc.add_value(value[1], xapian_value(value[0], value[3])) + doc.add_value(value[1], xapian_value(value[0], value[3])) end end if texts @@ -889,7 +892,7 @@ module ActsAsXapian for text in texts_to_index ActsAsXapian.term_generator.increase_termpos # stop phrases spanning different text fields # XXX the "1" here is a weight that could be varied for a boost function - ActsAsXapian.term_generator.index_text(xapian_value(text, nil, true), 1) + ActsAsXapian.term_generator.index_text(xapian_value(text, nil, true), 1) end end @@ -914,13 +917,13 @@ module ActsAsXapian job.save! end end - + # Allow reindexing to be skipped if a flag is set def xapian_mark_needs_index_if_reindex return true if (self.respond_to?(:no_xapian_reindex) && self.no_xapian_reindex == true) xapian_mark_needs_index end - + def xapian_mark_needs_destroy model = self.class.base_class.to_s model_id = self.id @@ -937,7 +940,7 @@ module ActsAsXapian ###################################################################### # Main entry point, add acts_as_xapian to your model. - + module ActsMethods # See top of this file for docs def acts_as_xapian(options) @@ -957,7 +960,7 @@ module ActsAsXapian after_destroy :xapian_mark_needs_destroy end end - + end # Reopen ActiveRecord and include the acts_as_xapian method |