From cb9bdf146ddd54d8373408038d2af51577c30b15 Mon Sep 17 00:00:00 2001 From: Ryan Kopf Date: Sat, 12 Aug 2023 02:55:27 +0900 Subject: [PATCH 1/7] Create main.yml workflow Create workflow for gem testing. --- .github/workflows/main.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..d3c8f06 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,28 @@ +name: Ruby Gem Test + +on: + push: + branches: [ main, test-workflow ] + pull_request: + branches: [ main, test-workflow ] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: ['1.8.7', '1.9.2', '1.9.3', '2.0.0'] + + steps: + - uses: actions/checkout@v2 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + + - name: Set up dependencies + run: bundle install + + - name: Run tests + run: bundle exec rake From 0b47d37b737d6b8cde694ad51213ae90ab9d06bf Mon Sep 17 00:00:00 2001 From: Ryan Kopf Date: Sat, 12 Aug 2023 02:57:55 +0900 Subject: [PATCH 2/7] Remove old rubies --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d3c8f06..80309f9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ['1.8.7', '1.9.2', '1.9.3', '2.0.0'] + ruby-version: ['1.9.3', '2.0.0'] steps: - uses: actions/checkout@v2 From 87ea8c6fb105000c87fc91b25551b7b9b003ac1d Mon Sep 17 00:00:00 2001 From: Ryan Kopf Date: Sat, 12 Aug 2023 07:05:41 +0900 Subject: [PATCH 3/7] Update unit_test_helper.rb Trying to get tests passing. --- test/unit_test_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit_test_helper.rb b/test/unit_test_helper.rb index d32998d..48fa3a0 100644 --- a/test/unit_test_helper.rb +++ b/test/unit_test_helper.rb @@ -4,6 +4,7 @@ require 'bundler/setup' require 'mocha' +require 'mocha/integration/test_unit' # Mock out the required environment variables. # Do this before requiring AAI. From 61c5a9a3cd4c1336b42e9b989a824ed01225539b Mon Sep 17 00:00:00 2001 From: ryankopf Date: Thu, 17 Aug 2023 15:13:15 -0500 Subject: [PATCH 4/7] Get tests working. --- .github/workflows/main.yml | 8 ++- .gitignore | 1 + Gemfile | 11 +-- Rakefile | 8 +++ lib/acts_as_indexed.rb | 2 + lib/acts_as_indexed/class_methods.rb | 71 ++++++++++++++----- lib/acts_as_indexed/configuration.rb | 4 +- lib/will_paginate_search.rb | 4 +- test/integration/acts_as_indexed_test.rb | 12 ++-- test/integration/will_paginate_search_test.rb | 3 +- test/integration_test_helper.rb | 11 +-- test/unit/configuration_test.rb | 6 +- test/unit/search_atom_test.rb | 4 +- test/unit_test_helper.rb | 16 +++-- 14 files changed, 110 insertions(+), 51 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 80309f9..5b6e2f2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ['1.9.3', '2.0.0'] + ruby-version: ['3.1.3', '2.0.0'] steps: - uses: actions/checkout@v2 @@ -21,6 +21,12 @@ jobs: with: ruby-version: ${{ matrix.ruby-version }} + - name: Install Jeweler + run: gem install jeweler + + - name: Install libyaml for sdoc + run: sudo apt-get install libyaml-dev + - name: Set up dependencies run: bundle install diff --git a/.gitignore b/.gitignore index c7be20f..133231f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ coverage index *.gem .bundle +Gemfile.lock diff --git a/Gemfile b/Gemfile index 20be1cc..1ee8d5d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,9 +1,12 @@ source "http://rubygems.org" group :test do + gem "sdoc" gem "rake" - gem "mocha", "~> 0.9.11" - gem "sqlite3", "~> 1.3.5" - gem "activerecord", "~> 3.2.9" - gem "will_paginate", "~> 3.0.5" + gem "minitest" + gem "mocha"#, "~> 0.9.11" + gem "sqlite3"#, "~> 1.3.5" + gem "activerecord"#, "~> 3.2.9" + gem "will_paginate"#, "~> 4.0" + gem "activesupport" end diff --git a/Rakefile b/Rakefile index cddb036..f451499 100644 --- a/Rakefile +++ b/Rakefile @@ -28,6 +28,14 @@ Rake::TestTask.new('test:integration') do |t| t.verbose = true end +desc 'Run temp tests.' +Rake::TestTask.new('test:temp') do |t| + t.libs << 'test' + t.libs << 'lib' + t.pattern = 'test/integration/will_paginate_search_test.rb' + t.verbose = true +end + desc 'Run performance tests.' Rake::TestTask.new('test:performance') do |t| t.libs << 'test' diff --git a/lib/acts_as_indexed.rb b/lib/acts_as_indexed.rb index 4f341f8..071a612 100644 --- a/lib/acts_as_indexed.rb +++ b/lib/acts_as_indexed.rb @@ -1,5 +1,7 @@ require 'active_record' +path = File.expand_path(File.dirname(__FILE__)) +$:.unshift path unless $:.include?(path) require 'acts_as_indexed/class_methods' require 'acts_as_indexed/instance_methods' require 'acts_as_indexed/singleton_methods' diff --git a/lib/acts_as_indexed/class_methods.rb b/lib/acts_as_indexed/class_methods.rb index 97b43f0..b418a3c 100644 --- a/lib/acts_as_indexed/class_methods.rb +++ b/lib/acts_as_indexed/class_methods.rb @@ -34,7 +34,10 @@ def acts_as_indexed(options = {}) named_scope :with_query, lambda { |query| { :conditions => ["#{table_name}.#{primary_key} IN (?)", search_index(query, {}, {:ids_only => true}) ] } } end - cattr_accessor :aai_config, :aai_fields + unless respond_to?(:aai_fields) && respond_to?(:aai_config) + cattr_accessor :aai_config, :aai_fields + end + #cattr_accessor :aai_config, :aai_fields self.aai_fields = options.delete(:fields) raise(ArgumentError, 'no fields specified') if self.aai_fields.nil? || self.aai_fields.empty? @@ -134,25 +137,59 @@ def search_index(query, find_options={}, options={}) return part_query if options[:ids_only] - with_scope :find => find_options do - # Doing the find like this eliminates the possibility of errors occuring - # on either missing records (out-of-sync) or an empty results array. - records = find(:all, :conditions => [ "#{table_name}.#{primary_key} IN (?)", part_query]) - - if find_options.include?(:order) - records # Just return the records without ranking them. + # New 2023 way to find the records. + conditions = ["#{table_name}.#{primary_key} IN (?)", part_query] + query_options = find_options.slice(:limit, :offset, :order) + records = where(conditions) - else - # Results come back in random order from SQL, so order again. - ranked_records = ActiveSupport::OrderedHash.new - records.each do |r| - ranked_records[r] = @query_cache[query][r.id] - end + if find_options[:conditions] + records = records.where(find_options[:conditions]) + end + if find_options[:joins] + records = records.joins(find_options[:joins]) + end + if find_options[:includes] + records = records.includes(find_options[:includes]) + end + if find_options[:limit] + records = records.limit(find_options[:limit]) + end + if find_options[:offset] + records = records.offset(find_options[:offset]) + end + if find_options[:order] + records = records.order(find_options[:order]) + return records # Just return the records without ranking them. + end - sort(ranked_records.to_a).map{ |r| r.first } - end + # Results come back in random order from SQL, so order again. + ranked_records = ActiveSupport::OrderedHash.new + records.each do |r| + ranked_records[r] = @query_cache[query][r.id] end + sort(ranked_records.to_a).map{ |r| r.first } + + # Old way, deprecated and broken. + # with_scope :find => find_options do + # # Doing the find like this eliminates the possibility of errors occuring + # # on either missing records (out-of-sync) or an empty results array. + # records = find(:all, :conditions => [ "#{table_name}.#{primary_key} IN (?)", part_query]) + # + # if find_options.include?(:order) + # records # Just return the records without ranking them. + # + # else + # # Results come back in random order from SQL, so order again. + # ranked_records = ActiveSupport::OrderedHash.new + # records.each do |r| + # ranked_records[r] = @query_cache[query][r.id] + # end + # + # sort(ranked_records.to_a).map{ |r| r.first } + # end + # end + end # Builds an index from scratch for the current model class. @@ -162,7 +199,7 @@ def build_index return if aai_config.index_file.directory? index = new_index - find_in_batches({ :batch_size => 500 }) do |records| + find_in_batches(batch_size: 500) do |records| index.add_records(records) end end diff --git a/lib/acts_as_indexed/configuration.rb b/lib/acts_as_indexed/configuration.rb index 743ce74..c822040 100644 --- a/lib/acts_as_indexed/configuration.rb +++ b/lib/acts_as_indexed/configuration.rb @@ -4,7 +4,7 @@ class Configuration # Sets the location for the index. Specify as an array. The default, for # example, would be set as [Rails.root,'tmp','index]. - attr_accessor :index_file + #attr_accessor :index_file # Tuning value for the index partitioning. Larger values result in quicker # searches, but slower indexing. Default is 3. @@ -18,7 +18,7 @@ class Configuration # Proc that allows you to turn on or off index for a record. # Useful if you don't want an object to be placed in the index, such as a # draft post. - attr_accessor :if_proc + attr_writer :if_proc # Enable or disable case sensitivity. # Set to true to enable. diff --git a/lib/will_paginate_search.rb b/lib/will_paginate_search.rb index c057112..9d58e1f 100644 --- a/lib/will_paginate_search.rb +++ b/lib/will_paginate_search.rb @@ -1,8 +1,8 @@ module ActsAsIndexed - module WillPaginate + module WillPaginate - module Search + module Search def paginate_search(query, options) page = options.fetch(:page) { raise ArgumentError, ":page parameter required" } diff --git a/test/integration/acts_as_indexed_test.rb b/test/integration/acts_as_indexed_test.rb index 39825ed..96b1fd3 100644 --- a/test/integration/acts_as_indexed_test.rb +++ b/test/integration/acts_as_indexed_test.rb @@ -44,7 +44,7 @@ def test_search_returns_post_ids def test_updates_index p = Post.create(:title => 'A special title', :body => 'foo bar bla bla bla') assert find_with_index_ids('title').include?(p.id) - p.update_attributes(:title => 'No longer special') + p.update(:title => 'No longer special') assert !find_with_index_ids('title').include?(p.id) end @@ -185,7 +185,7 @@ def test_should_error_when_ids_only_combined_with_finder_options # all records with that same atom from the index. def test_update_record_bug p = Post.find(6) - assert p.update_attributes(:body => p.body + ' crane') + assert p.update(:body => p.body + ' crane') assert_equal 2, find_with_index_ids('crane').size assert_equal 2, find_with_index_ids('ship').size end @@ -216,7 +216,7 @@ def test_update_if_update assert_equal 1, Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true}).size p = Post.find(6) - assert p.update_attributes(:visible => true) + assert p.update(:visible => true) assert_equal 1, Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true}).size end @@ -227,7 +227,7 @@ def test_update_if_remove assert_equal 1, Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true}).size p = Post.find(6) - assert p.update_attributes(:visible => false) + assert p.update(:visible => false) assert_equal 0, Post.find_with_index('crane',{},{ :no_query_cache => true, :ids_only => true}).size end @@ -238,7 +238,7 @@ def test_update_if_add assert_equal 1, Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true}).size p = Post.find(5) - assert p.update_attributes(:visible => true) + assert p.update(:visible => true) assert_equal 2, Post.find_with_index('crane',{},{ :no_query_cache => true, :ids_only => true}).size end @@ -249,7 +249,7 @@ def test_update_if_not_in assert_equal [6], Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true}) - posts(:wikipedia_article_5).update_attributes(:title => 'A new title') + posts(:wikipedia_article_5).update(:title => 'A new title') assert_equal [6], Post.find_with_index('crane',{},{ :no_query_cache => true, :ids_only => true}) end diff --git a/test/integration/will_paginate_search_test.rb b/test/integration/will_paginate_search_test.rb index 1acadc2..f69c48d 100644 --- a/test/integration/will_paginate_search_test.rb +++ b/test/integration/will_paginate_search_test.rb @@ -1,4 +1,4 @@ -require 'integration_test_helper.rb' +require 'integration_test_helper' class WillPaginateSearchTest < ActiveSupport::TestCase fixtures :posts @@ -10,7 +10,6 @@ def teardown end def test_paginate_search - assert_equal [5, 2, 1, 3, 6, 4], paginate_search(1, 10) assert_equal [5, 2, 1], paginate_search(1, 3) assert_equal [3, 6, 4], paginate_search(2, 3) diff --git a/test/integration_test_helper.rb b/test/integration_test_helper.rb index b3d919a..29153a9 100644 --- a/test/integration_test_helper.rb +++ b/test/integration_test_helper.rb @@ -1,6 +1,5 @@ -require 'test/unit' +require 'minitest/autorun' require 'fileutils' -require 'rubygems' require 'bundler/setup' require 'active_record' @@ -29,19 +28,21 @@ def self.env ActiveRecord::Base.logger = Logger.new(test_path.join('test.log').to_s) ActiveRecord::Base.configurations = YAML::load(IO.read(test_path.join('config', 'database.yml').to_s)) -ActiveRecord::Base.establish_connection(ENV['DB'] || 'sqlite3') +ActiveRecord::Base.establish_connection(ENV['DB'] || :sqlite3) # Load Schema load(test_path.join('db', 'schema.rb').to_s) # Load model. $LOAD_PATH.unshift(test_path.join('fixtures').to_s) +require 'post' class ActiveSupport::TestCase #:nodoc: include ActiveRecord::TestFixtures self.fixture_path = Pathname.new(File.expand_path('../', __FILE__)).join('fixtures').to_s - self.use_transactional_fixtures = true - self.use_instantiated_fixtures = false + self.use_transactional_tests = true + self.use_instantiated_fixtures = false + #fixtures :all def destroy_index FileUtils.rm_rf(index_loc) diff --git a/test/unit/configuration_test.rb b/test/unit/configuration_test.rb index c60089d..19cf229 100644 --- a/test/unit/configuration_test.rb +++ b/test/unit/configuration_test.rb @@ -1,4 +1,4 @@ -require 'unit_test_helper.rb' +require 'unit_test_helper' include ActsAsIndexed class ConfigurationTest < ActiveSupport::TestCase @@ -35,7 +35,7 @@ def test_index_file_depth_should_be_writeable end def test_index_file_depth_should_raise_on_lower_than_1_value - assert_nothing_raised(ArgumentError) { config.index_file_depth = 1 } + assert_nothing_raised { config.index_file_depth = 1 } e = assert_raise(ArgumentError) { config.index_file_depth = 0 } assert_equal 'index_file_depth cannot be less than one (1)', e.message @@ -49,7 +49,7 @@ def test_min_word_size_should_be_writeable end def test_min_word_size_should_raise_on_lower_than_1_value - assert_nothing_raised(ArgumentError) { config.min_word_size = 1 } + assert_nothing_raised { config.min_word_size = 1 } e = assert_raise(ArgumentError) { config.min_word_size = 0 } assert_equal 'min_word_size cannot be less than one (1)', e.message diff --git a/test/unit/search_atom_test.rb b/test/unit/search_atom_test.rb index f7a1324..ffa956e 100644 --- a/test/unit/search_atom_test.rb +++ b/test/unit/search_atom_test.rb @@ -1,7 +1,7 @@ require 'unit_test_helper.rb' include ActsAsIndexed -class SearchAtomTest < ActiveSupport::TestCase +class SearchAtomTest < ActiveSupport::TestCase def test_should_create_a_new_instance assert SearchAtom.new @@ -48,7 +48,7 @@ def test_positions_should_return_positions end def test_positions_should_return_nil - assert_equal nil, build_search_atom.positions(456) + assert_nil build_search_atom.positions(456) end def test_remove_record diff --git a/test/unit_test_helper.rb b/test/unit_test_helper.rb index 48fa3a0..9da0f08 100644 --- a/test/unit_test_helper.rb +++ b/test/unit_test_helper.rb @@ -1,10 +1,11 @@ -require 'test/unit' +require 'minitest/autorun' require 'fileutils' -require 'rubygems' -require 'bundler/setup' -require 'mocha' -require 'mocha/integration/test_unit' +# require 'bundler/setup' +# require 'mocha' +# require 'mocha/integration/test_unit' +require 'mocha/minitest' +require 'active_support/test_case' # Mock out the required environment variables. # Do this before requiring AAI. @@ -17,5 +18,6 @@ def self.env end end -test_path = Pathname.new(File.expand_path('../', __FILE__)) -require test_path.parent.join('lib', 'acts_as_indexed').to_s +# test_path = Pathname.new(File.expand_path('../', __FILE__)) +# require test_path.parent.join('lib', 'acts_as_indexed').to_s +require_relative '../lib/acts_as_indexed' From ccd726a3fa5f3b223b2b50b88fc9780ddc4f9235 Mon Sep 17 00:00:00 2001 From: ryankopf Date: Thu, 17 Aug 2023 15:16:37 -0500 Subject: [PATCH 5/7] Get tests working. --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5b6e2f2..1bfa86d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ['3.1.3', '2.0.0'] + ruby-version: ['3.1.3', '3.0.6', '2.7.8', '2.6.10'] steps: - uses: actions/checkout@v2 From f32e32c6512901bfdf482283905411d951ba1863 Mon Sep 17 00:00:00 2001 From: ryankopf Date: Fri, 18 Aug 2023 12:19:01 -0500 Subject: [PATCH 6/7] Remove orderedhash, update ruby versions to test against. --- .github/workflows/main.yml | 2 +- lib/acts_as_indexed/class_methods.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1bfa86d..8646d1c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ['3.1.3', '3.0.6', '2.7.8', '2.6.10'] + ruby-version: ['3.1.3', '3.0.6'] steps: - uses: actions/checkout@v2 diff --git a/lib/acts_as_indexed/class_methods.rb b/lib/acts_as_indexed/class_methods.rb index b418a3c..8f366fb 100644 --- a/lib/acts_as_indexed/class_methods.rb +++ b/lib/acts_as_indexed/class_methods.rb @@ -163,7 +163,7 @@ def search_index(query, find_options={}, options={}) end # Results come back in random order from SQL, so order again. - ranked_records = ActiveSupport::OrderedHash.new + ranked_records = {} records.each do |r| ranked_records[r] = @query_cache[query][r.id] end From baac065f3b7b1697a7fe97886d36ef50a2d96d4c Mon Sep 17 00:00:00 2001 From: ryankopf Date: Fri, 18 Aug 2023 12:27:30 -0500 Subject: [PATCH 7/7] Remove unused var --- lib/acts_as_indexed/class_methods.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/acts_as_indexed/class_methods.rb b/lib/acts_as_indexed/class_methods.rb index 8f366fb..cc7fbe7 100644 --- a/lib/acts_as_indexed/class_methods.rb +++ b/lib/acts_as_indexed/class_methods.rb @@ -139,7 +139,6 @@ def search_index(query, find_options={}, options={}) # New 2023 way to find the records. conditions = ["#{table_name}.#{primary_key} IN (?)", part_query] - query_options = find_options.slice(:limit, :offset, :order) records = where(conditions) if find_options[:conditions]