diff --git a/lib/acts_as_searchable.rb b/lib/acts_as_searchable.rb deleted file mode 100644 index feb07ea..0000000 --- a/lib/acts_as_searchable.rb +++ /dev/null @@ -1,87 +0,0 @@ -module ActsAsSearchable - - module ClassMethods - ACTS_AS_SEARCHABLE_ENABLED = true unless defined? ACTS_AS_SEARCHABLE_ENABLED - - def acts_as_searchable(options = {}) - return if !ACTS_AS_SEARCHABLE_ENABLED - - if options[:fields] - options[:fields] << {:schema_name => :string} - else - options[:additional_fields] ||= [] - options[:additional_fields] << {:schema_name => :string} - end - - acts_as_solr options - extend FindByContents - send :include, InstanceMethods - end - - module InstanceMethods - def schema_name - self.class.schema_name - end - - # replace solr_id from vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/instance_methods.rb - # to include schema_name - def solr_id - id = "#{self.class.name}:#{record_id(self)}" - id.insert(0, "#{schema_name}:") unless schema_name.blank? - id - end - end - - module FindByContents - - def schema_name - (Noosfero::MultiTenancy.on? and ActiveRecord::Base.postgresql?) ? ActiveRecord::Base.connection.schema_search_path : '' - end - - def find_by_contents(query, pg_options = {}, options = {}, db_options = {}) - pg_options[:page] ||= 1 - pg_options[:per_page] ||= 20 - options[:page] = pg_options[:page].to_i - options[:per_page] = pg_options[:per_page].to_i - options[:scores] ||= true - options[:filter_queries] ||= [] - options[:filter_queries] << "schema_name:\"#{schema_name}\"" unless schema_name.blank? - all_facets_enabled = options.delete(:all_facets) - options[:per_page] = options.delete(:extra_limit) if options[:extra_limit] - results = [] - facets = all_facets = {} - - solr_result = find_by_solr(query, options) - if all_facets_enabled - options[:facets][:browse] = nil - all_facets = find_by_solr(query, options.merge(:per_page => 0)).facets - end - - if !solr_result.nil? - facets = options.include?(:facets) ? solr_result.facets : [] - - if db_options.empty? - results = solr_result - else - ids = solr_result.results.map{ |r| r[:id].to_i } - if ids.empty? - ids << -1 - end - - if db_options[:conditions] - db_options[:conditions] = sanitize_sql_for_conditions(db_options[:conditions]) + " and #{table_name}.id in (#{ids.join(', ')})" - else - db_options[:conditions] = "#{table_name}.id in (#{ids.join(', ')})" - end - - results = find(:all, db_options) - end - end - - {:results => results, :facets => facets, :all_facets => all_facets} - end - end - end -end - -ActiveRecord::Base.send(:extend, ActsAsSearchable::ClassMethods) diff --git a/plugins/solr/dependencies.rb b/plugins/solr/dependencies.rb index 5dd1865..11a671d 100644 --- a/plugins/solr/dependencies.rb +++ b/plugins/solr/dependencies.rb @@ -1,4 +1,4 @@ require 'rubygems' require 'active_record' -require 'acts_as_searchable' +require "#{File.dirname(__FILE__)}/lib/acts_as_searchable" require "#{File.dirname(__FILE__)}/lib/acts_as_faceted" diff --git a/plugins/solr/test/unit/acts_as_searchable_test.rb b/plugins/solr/test/unit/acts_as_searchable_test.rb new file mode 100644 index 0000000..b8b04da --- /dev/null +++ b/plugins/solr/test/unit/acts_as_searchable_test.rb @@ -0,0 +1,46 @@ +require File.dirname(__FILE__) + '/../test_helper' +require "#{File.dirname(__FILE__)}../../lib/acts_as_searchable" + +class ActsAsSearchableTest < ActiveSupport::TestCase + + def setup + @test_model = Class.new ActiveRecord::Base + end + + def silent + # http://mentalized.net/journal/2010/04/02/suppress_warnings_from_ruby/ + original_verbosity = $VERBOSE + $VERBOSE = nil + result = yield + $VERBOSE = original_verbosity + return result + end + + should 'be enabled by default' do + assert ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED, true + end + + should 'not be searchable when disabled' do + # suppress warning about already initialized constant + silent { ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED = false } + + @test_model.expects(:acts_as_solr).never + @test_model.acts_as_searchable + end + + should 'correctly pass options to search engine' do + options = {:fields => [{:name => :text}]} + @test_model.expects(:acts_as_solr).with(options) + @test_model.acts_as_searchable options + end + + should 'always include schema name as a field' do + @test_model.expects(:acts_as_solr).with(has_entry(:fields, [{:field1 => :text}, {:schema_name => :string}])) + @test_model.acts_as_searchable :fields => [{:field1 => :text}] + # ...even with no fields + @test_model = Class.new ActiveRecord::Base + @test_model.expects(:acts_as_solr).with(has_entry(:additional_fields, [{:schema_name => :string}])) + @test_model.acts_as_searchable + end + +end diff --git a/test/unit/acts_as_searchable_test.rb b/test/unit/acts_as_searchable_test.rb deleted file mode 100644 index 497a353..0000000 --- a/test/unit/acts_as_searchable_test.rb +++ /dev/null @@ -1,46 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' -require 'acts_as_searchable' - -class ActsAsSearchableTest < ActiveSupport::TestCase - - def setup - @test_model = Class.new ActiveRecord::Base - end - - def silent - # http://mentalized.net/journal/2010/04/02/suppress_warnings_from_ruby/ - original_verbosity = $VERBOSE - $VERBOSE = nil - result = yield - $VERBOSE = original_verbosity - return result - end - - should 'be enabled by default' do - assert ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED, true - end - - should 'not be searchable when disabled' do - # suppress warning about already initialized constant - silent { ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED = false } - - @test_model.expects(:acts_as_solr).never - @test_model.acts_as_searchable - end - - should 'correctly pass options to search engine' do - options = {:fields => [{:name => :text}]} - @test_model.expects(:acts_as_solr).with(options) - @test_model.acts_as_searchable options - end - - should 'always include schema name as a field' do - @test_model.expects(:acts_as_solr).with(has_entry(:fields, [{:field1 => :text}, {:schema_name => :string}])) - @test_model.acts_as_searchable :fields => [{:field1 => :text}] - # ...even with no fields - @test_model = Class.new ActiveRecord::Base - @test_model.expects(:acts_as_solr).with(has_entry(:additional_fields, [{:schema_name => :string}])) - @test_model.acts_as_searchable - end - -end -- libgit2 0.21.2