Commit 03b3e7b1cdd1cf51e79b0a2b5e6e4470a7ae87c9

Authored by Rodrigo Souto
1 parent 1db24253

Refactoring pg_search_plugin

plugins/pg_search/db/migrate/20130320010053_create_indexes_for_search.rb
... ... @@ -1,19 +0,0 @@
1   -class CreateIndexesForSearch < ActiveRecord::Migration
2   - def self.up
3   - searchables = %w[ article comment qualifier national_region certifier profile license scrap category ]
4   - klasses = searchables.map {|searchable| searchable.camelize.constantize }
5   - klasses.each do |klass|
6   - klass::SEARCHABLE_FIELDS.keys.each do |field|
7   - execute "create index pg_search_plugin_#{klass.name.singularize.downcase}_#{field} on #{klass.table_name} using gin(to_tsvector('simple', \"#{klass.table_name}\".#{field}))"
8   - end
9   - end
10   - end
11   -
12   - def self.down
13   - klasses.each do |klass|
14   - klass::SEARCHABLE_FIELDS.keys.each do |field|
15   - execute "drop index pg_search_plugin_#{klass.name.singularize.downcase}_#{field}"
16   - end
17   - end
18   - end
19   -end
plugins/pg_search/db/migrate/20130320010062_create_indexes_for_search.rb 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +class CreateIndexesForSearch < ActiveRecord::Migration
  2 + def self.up
  3 + searchables = %w[ article comment qualifier national_region certifier profile license scrap category ]
  4 + klasses = searchables.map {|searchable| searchable.camelize.constantize }
  5 + klasses.each do |klass|
  6 + fields = klass.pg_search_plugin_fields
  7 + execute "create index pg_search_plugin_#{klass.name.singularize.downcase} on #{klass.table_name} using gin(to_tsvector('simple', #{fields}))"
  8 + end
  9 + end
  10 +
  11 + def self.down
  12 + klasses.each do |klass|
  13 + execute "drop index pg_search_plugin_#{klass.name.singularize.downcase}"
  14 + end
  15 + end
  16 +end
... ...
plugins/pg_search/lib/ext/active_record.rb
... ... @@ -3,10 +3,13 @@ require_dependency &#39;active_record&#39;
3 3 class ActiveRecord::Base
4 4 def self.pg_search_plugin_search(query)
5 5 if defined?(self::SEARCHABLE_FIELDS)
6   - conditions = self::SEARCHABLE_FIELDS.map {|field, weight| "to_tsvector('simple', \"#{self.table_name}\".#{field}) @@ '#{query}'"}.join(' OR ')
7   - where(conditions)
  6 + where("to_tsvector('simple', #{pg_search_plugin_fields}) @@ to_tsquery('#{query}:*')")
8 7 else
9 8 raise "No searchable fields defined for #{self.name}"
10 9 end
11 10 end
  11 +
  12 + def self.pg_search_plugin_fields
  13 + self::SEARCHABLE_FIELDS.keys.map(&:to_s).sort.map {|f| "coalesce(\"#{table_name}\".#{f}, '')"}.join(" || ' ' || ")
  14 + end
12 15 end
... ...
plugins/pg_search/test/unit/pg_search_plugin_test.rb 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +require 'test_helper'
  2 +
  3 +class PgSearchPluginTest < ActiveSupport::TestCase
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(PgSearchPlugin)
  7 + end
  8 +
  9 + should 'locate profile' do
  10 + profile = fast_create(Profile, :name => 'John', :identifier => 'waterfall')
  11 + assert_includes search(Profile, 'John'), profile
  12 + assert_includes search(Profile, 'john'), profile
  13 + assert_includes search(Profile, 'waterfall'), profile
  14 + assert_includes search(Profile, 'water'), profile
  15 + end
  16 +
  17 + private
  18 +
  19 + def search(scope, query)
  20 + scope.pg_search_plugin_search(query)
  21 + end
  22 +end
... ...