acts-as-tsearch is pretty cool, except for the fact that it uses Ruby (app layer) instead of PL/pgSQL (DB layer) to update the tsvectors that are indexed for full text search. That means that fixture data gets inserted without being full text indexed. D’oh!
Here’s some code that changes that.
(I put this in my environment.rb because I’m not quite at the point of shoving all this into a plugin like I probably should.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# make it so that fixture loading (test data and base data) includes the tsearch2 update_vector class Fixtures class << self # we want to mess with self.instantiate_fixtures def create_fixtures_with_update_vector(fixtures_directory, table_names, class_names = {}) create_fixtures_without_update_vector(fixtures_directory, table_names, class_names) # create a Class instance for each table name fixtures were loaded for, then call update_vector on it table_names.each do |tn| klass_name = (ActiveRecord::Base.pluralize_table_names ? tn.singularize.camelize : tn.camelize) begin klass = Object.const_get(klass_name) # will fail if tn is a habtm table (no corresponding model class) klass.update_vector if klass.respond_to?(:update_vector) rescue nil # if it is a habtm table, there's no need to update a tsearch2 vector for sure end end end alias_method_chain :create_fixtures, :update_vector end end |
(Sorry for the formatting but I like wide lines in my source code and my WP theme doesn’t. Just copy and paste and it should be fine.)