Commit 6ff0271273650a90deaabb452d130c34dcbd5f8b
1 parent
c893df1c
Exists in
master
and in
29 other branches
Moving acts as searchable to the plugin
Showing
4 changed files
with
47 additions
and
134 deletions
Show diff stats
lib/acts_as_searchable.rb
@@ -1,87 +0,0 @@ | @@ -1,87 +0,0 @@ | ||
1 | -module ActsAsSearchable | ||
2 | - | ||
3 | - module ClassMethods | ||
4 | - ACTS_AS_SEARCHABLE_ENABLED = true unless defined? ACTS_AS_SEARCHABLE_ENABLED | ||
5 | - | ||
6 | - def acts_as_searchable(options = {}) | ||
7 | - return if !ACTS_AS_SEARCHABLE_ENABLED | ||
8 | - | ||
9 | - if options[:fields] | ||
10 | - options[:fields] << {:schema_name => :string} | ||
11 | - else | ||
12 | - options[:additional_fields] ||= [] | ||
13 | - options[:additional_fields] << {:schema_name => :string} | ||
14 | - end | ||
15 | - | ||
16 | - acts_as_solr options | ||
17 | - extend FindByContents | ||
18 | - send :include, InstanceMethods | ||
19 | - end | ||
20 | - | ||
21 | - module InstanceMethods | ||
22 | - def schema_name | ||
23 | - self.class.schema_name | ||
24 | - end | ||
25 | - | ||
26 | - # replace solr_id from vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/instance_methods.rb | ||
27 | - # to include schema_name | ||
28 | - def solr_id | ||
29 | - id = "#{self.class.name}:#{record_id(self)}" | ||
30 | - id.insert(0, "#{schema_name}:") unless schema_name.blank? | ||
31 | - id | ||
32 | - end | ||
33 | - end | ||
34 | - | ||
35 | - module FindByContents | ||
36 | - | ||
37 | - def schema_name | ||
38 | - (Noosfero::MultiTenancy.on? and ActiveRecord::Base.postgresql?) ? ActiveRecord::Base.connection.schema_search_path : '' | ||
39 | - end | ||
40 | - | ||
41 | - def find_by_contents(query, pg_options = {}, options = {}, db_options = {}) | ||
42 | - pg_options[:page] ||= 1 | ||
43 | - pg_options[:per_page] ||= 20 | ||
44 | - options[:page] = pg_options[:page].to_i | ||
45 | - options[:per_page] = pg_options[:per_page].to_i | ||
46 | - options[:scores] ||= true | ||
47 | - options[:filter_queries] ||= [] | ||
48 | - options[:filter_queries] << "schema_name:\"#{schema_name}\"" unless schema_name.blank? | ||
49 | - all_facets_enabled = options.delete(:all_facets) | ||
50 | - options[:per_page] = options.delete(:extra_limit) if options[:extra_limit] | ||
51 | - results = [] | ||
52 | - facets = all_facets = {} | ||
53 | - | ||
54 | - solr_result = find_by_solr(query, options) | ||
55 | - if all_facets_enabled | ||
56 | - options[:facets][:browse] = nil | ||
57 | - all_facets = find_by_solr(query, options.merge(:per_page => 0)).facets | ||
58 | - end | ||
59 | - | ||
60 | - if !solr_result.nil? | ||
61 | - facets = options.include?(:facets) ? solr_result.facets : [] | ||
62 | - | ||
63 | - if db_options.empty? | ||
64 | - results = solr_result | ||
65 | - else | ||
66 | - ids = solr_result.results.map{ |r| r[:id].to_i } | ||
67 | - if ids.empty? | ||
68 | - ids << -1 | ||
69 | - end | ||
70 | - | ||
71 | - if db_options[:conditions] | ||
72 | - db_options[:conditions] = sanitize_sql_for_conditions(db_options[:conditions]) + " and #{table_name}.id in (#{ids.join(', ')})" | ||
73 | - else | ||
74 | - db_options[:conditions] = "#{table_name}.id in (#{ids.join(', ')})" | ||
75 | - end | ||
76 | - | ||
77 | - results = find(:all, db_options) | ||
78 | - end | ||
79 | - end | ||
80 | - | ||
81 | - {:results => results, :facets => facets, :all_facets => all_facets} | ||
82 | - end | ||
83 | - end | ||
84 | - end | ||
85 | -end | ||
86 | - | ||
87 | -ActiveRecord::Base.send(:extend, ActsAsSearchable::ClassMethods) |
plugins/solr/dependencies.rb
1 | require 'rubygems' | 1 | require 'rubygems' |
2 | require 'active_record' | 2 | require 'active_record' |
3 | -require 'acts_as_searchable' | 3 | +require "#{File.dirname(__FILE__)}/lib/acts_as_searchable" |
4 | require "#{File.dirname(__FILE__)}/lib/acts_as_faceted" | 4 | require "#{File.dirname(__FILE__)}/lib/acts_as_faceted" |
@@ -0,0 +1,46 @@ | @@ -0,0 +1,46 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | +require "#{File.dirname(__FILE__)}../../lib/acts_as_searchable" | ||
3 | + | ||
4 | +class ActsAsSearchableTest < ActiveSupport::TestCase | ||
5 | + | ||
6 | + def setup | ||
7 | + @test_model = Class.new ActiveRecord::Base | ||
8 | + end | ||
9 | + | ||
10 | + def silent | ||
11 | + # http://mentalized.net/journal/2010/04/02/suppress_warnings_from_ruby/ | ||
12 | + original_verbosity = $VERBOSE | ||
13 | + $VERBOSE = nil | ||
14 | + result = yield | ||
15 | + $VERBOSE = original_verbosity | ||
16 | + return result | ||
17 | + end | ||
18 | + | ||
19 | + should 'be enabled by default' do | ||
20 | + assert ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED, true | ||
21 | + end | ||
22 | + | ||
23 | + should 'not be searchable when disabled' do | ||
24 | + # suppress warning about already initialized constant | ||
25 | + silent { ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED = false } | ||
26 | + | ||
27 | + @test_model.expects(:acts_as_solr).never | ||
28 | + @test_model.acts_as_searchable | ||
29 | + end | ||
30 | + | ||
31 | + should 'correctly pass options to search engine' do | ||
32 | + options = {:fields => [{:name => :text}]} | ||
33 | + @test_model.expects(:acts_as_solr).with(options) | ||
34 | + @test_model.acts_as_searchable options | ||
35 | + end | ||
36 | + | ||
37 | + should 'always include schema name as a field' do | ||
38 | + @test_model.expects(:acts_as_solr).with(has_entry(:fields, [{:field1 => :text}, {:schema_name => :string}])) | ||
39 | + @test_model.acts_as_searchable :fields => [{:field1 => :text}] | ||
40 | + # ...even with no fields | ||
41 | + @test_model = Class.new ActiveRecord::Base | ||
42 | + @test_model.expects(:acts_as_solr).with(has_entry(:additional_fields, [{:schema_name => :string}])) | ||
43 | + @test_model.acts_as_searchable | ||
44 | + end | ||
45 | + | ||
46 | +end |
test/unit/acts_as_searchable_test.rb
@@ -1,46 +0,0 @@ | @@ -1,46 +0,0 @@ | ||
1 | -require File.dirname(__FILE__) + '/../test_helper' | ||
2 | -require 'acts_as_searchable' | ||
3 | - | ||
4 | -class ActsAsSearchableTest < ActiveSupport::TestCase | ||
5 | - | ||
6 | - def setup | ||
7 | - @test_model = Class.new ActiveRecord::Base | ||
8 | - end | ||
9 | - | ||
10 | - def silent | ||
11 | - # http://mentalized.net/journal/2010/04/02/suppress_warnings_from_ruby/ | ||
12 | - original_verbosity = $VERBOSE | ||
13 | - $VERBOSE = nil | ||
14 | - result = yield | ||
15 | - $VERBOSE = original_verbosity | ||
16 | - return result | ||
17 | - end | ||
18 | - | ||
19 | - should 'be enabled by default' do | ||
20 | - assert ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED, true | ||
21 | - end | ||
22 | - | ||
23 | - should 'not be searchable when disabled' do | ||
24 | - # suppress warning about already initialized constant | ||
25 | - silent { ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED = false } | ||
26 | - | ||
27 | - @test_model.expects(:acts_as_solr).never | ||
28 | - @test_model.acts_as_searchable | ||
29 | - end | ||
30 | - | ||
31 | - should 'correctly pass options to search engine' do | ||
32 | - options = {:fields => [{:name => :text}]} | ||
33 | - @test_model.expects(:acts_as_solr).with(options) | ||
34 | - @test_model.acts_as_searchable options | ||
35 | - end | ||
36 | - | ||
37 | - should 'always include schema name as a field' do | ||
38 | - @test_model.expects(:acts_as_solr).with(has_entry(:fields, [{:field1 => :text}, {:schema_name => :string}])) | ||
39 | - @test_model.acts_as_searchable :fields => [{:field1 => :text}] | ||
40 | - # ...even with no fields | ||
41 | - @test_model = Class.new ActiveRecord::Base | ||
42 | - @test_model.expects(:acts_as_solr).with(has_entry(:additional_fields, [{:schema_name => :string}])) | ||
43 | - @test_model.acts_as_searchable | ||
44 | - end | ||
45 | - | ||
46 | -end |