diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1060083..619ba72 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -20,6 +20,7 @@ jobs: matrix: ruby-version: ["3.2", "3.3", "3.4"] rails-version: [6.1, 7.0] + mongodb-version: [8.0] env: RAILS_VERSION: ${{ matrix.rails-version }} name: integration-tests (Rails ${{ matrix.rails-version }} with Ruby ${{ matrix.ruby-version }}) @@ -34,6 +35,10 @@ jobs: bundler-cache: true - name: Meilisearch (latest) setup with Docker run: docker run -d -p 7700:7700 getmeili/meilisearch:latest meilisearch --master-key=masterKey --no-analytics + - name: Start MongoDB + uses: supercharge/mongodb-github-action@1.12.0 + with: + mongodb-version: ${{ matrix.mongodb-version }} - name: Run tests run: bundle exec rspec - name: Upload coverage reports to Codecov diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 418e8ae..5cc9c61 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,12 +1,12 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2025-03-24 19:18:19 UTC using RuboCop version 1.27.0. +# on 2025-03-28 20:11:18 UTC using RuboCop version 1.27.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 1 +# Offense count: 2 # This cop supports safe auto-correction (--auto-correct). # Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include. # Include: **/*.gemfile, **/Gemfile, **/gems.rb @@ -66,10 +66,10 @@ Lint/UnusedMethodArgument: Exclude: - 'lib/meilisearch-rails.rb' -# Offense count: 15 +# Offense count: 17 # Configuration parameters: IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: - Max: 105 + Max: 108 # Offense count: 1 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. @@ -82,10 +82,10 @@ Metrics/BlockLength: Metrics/ClassLength: Max: 171 -# Offense count: 11 +# Offense count: 12 # Configuration parameters: IgnoredMethods. Metrics/CyclomaticComplexity: - Max: 28 + Max: 29 # Offense count: 23 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. @@ -95,12 +95,12 @@ Metrics/MethodLength: # Offense count: 1 # Configuration parameters: CountComments, CountAsOne. Metrics/ModuleLength: - Max: 437 + Max: 439 -# Offense count: 9 +# Offense count: 10 # Configuration parameters: IgnoredMethods. Metrics/PerceivedComplexity: - Max: 35 + Max: 36 # Offense count: 1 Naming/AccessorMethodName: @@ -139,7 +139,7 @@ RSpec/ContextWording: - 'spec/options_spec.rb' - 'spec/system/tech_shop_spec.rb' -# Offense count: 72 +# Offense count: 74 # Configuration parameters: CountAsOne. RSpec/ExampleLength: Max: 16 @@ -167,7 +167,7 @@ RSpec/MultipleDescribes: Exclude: - 'spec/search_spec.rb' -# Offense count: 3 +# Offense count: 5 RSpec/NestedGroups: Max: 4 @@ -241,7 +241,7 @@ Style/StringLiterals: Exclude: - 'spec/ms_clean_up_job_spec.rb' -# Offense count: 20 +# Offense count: 22 # This cop supports safe auto-correction (--auto-correct). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https diff --git a/Gemfile b/Gemfile index d018867..61fe611 100644 --- a/Gemfile +++ b/Gemfile @@ -15,10 +15,12 @@ end group :test do rails_version = ENV['RAILS_VERSION'] || '7.1' sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0' + mongoid_version = ENV['MONGOID_VERSION'] || '9.0' gem 'active_model_serializers' gem 'rails', "~> #{rails_version}" gem 'sequel', sequel_version + gem 'mongoid', "~> #{mongoid_version}" # remove when deprecate rails 6 if Gem::Version.new(rails_version) >= Gem::Version.new('7.0') diff --git a/lib/meilisearch-rails.rb b/lib/meilisearch-rails.rb index 8da8723..3fb47b7 100644 --- a/lib/meilisearch-rails.rb +++ b/lib/meilisearch-rails.rb @@ -483,7 +483,7 @@ def meilisearch(options = {}, &block) end end elsif respond_to?(:after_destroy) - after_destroy_commit { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) } + after_commit(on: :destroy) { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) } end end end @@ -696,6 +696,8 @@ def ms_search(query, params = {}) # respond with a valid database column. The blocks below prevent that from happening. has_virtual_column_as_pk = if defined?(::Sequel::Model) && self < Sequel::Model meilisearch_options[:type].columns.map(&:to_s).exclude?(condition_key.to_s) + elsif Utilities.mongo_model?(self) + fields.keys.exclude?(condition_key.to_s) else meilisearch_options[:type].columns.map(&:name).map(&:to_s).exclude?(condition_key.to_s) end @@ -784,7 +786,7 @@ def ms_must_reindex?(document) def ms_primary_key_method(options = nil) options ||= meilisearch_options - options[:primary_key] || options[:id] || :id + options[:primary_key] || options[:id] || (Utilities.mongo_model?(self) ? :_id : :id) end protected diff --git a/spec/integration/mongoid/record_is_created_spec.rb b/spec/integration/mongoid/record_is_created_spec.rb new file mode 100644 index 0000000..e4f8e38 --- /dev/null +++ b/spec/integration/mongoid/record_is_created_spec.rb @@ -0,0 +1,11 @@ +require 'support/mongo_models/citizen' + +describe 'MongoDB record is created' do + it 'is added to meilisearch' do + john_wick = Citizen.create(name: 'John Wick', age: 40) + + AsyncHelper.await_last_task + + expect(Citizen.search('John')).to eq([john_wick]) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1f92dc5..b6e622b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -31,6 +31,7 @@ require 'sqlite3' unless defined?(JRUBY_VERSION) require 'logger' require 'sequel' +require 'mongoid' require 'active_model_serializers' require 'byebug' @@ -67,5 +68,7 @@ safe_index_list.each do |index| Meilisearch::Rails.client.delete_index(index) end + + Mongoid.default_client.database.drop end end diff --git a/spec/support/mongo_models/citizen.rb b/spec/support/mongo_models/citizen.rb new file mode 100644 index 0000000..aed0968 --- /dev/null +++ b/spec/support/mongo_models/citizen.rb @@ -0,0 +1,11 @@ +class Citizen + include Mongoid::Document + include Mongoid::Timestamps + + field :name, type: String + field :age, type: Integer + + include MeiliSearch::Rails + + meilisearch index_uid: safe_index_uid('Citizen') +end diff --git a/spec/support/mongoid_config.rb b/spec/support/mongoid_config.rb new file mode 100644 index 0000000..e61050f --- /dev/null +++ b/spec/support/mongoid_config.rb @@ -0,0 +1,15 @@ +mongo_db_name = (ENV['MONGODB_DATABASE'] || '_ms_rails_test') + SecureRandom.hex(8) +Mongoid.load_configuration( + { + clients: { + default: { + database: mongo_db_name, + hosts: [ENV['MONGODB_HOST'] || 'localhost:27017'], + options: { + read: { mode: :primary }, + max_pool_size: 1 + } + } + } + } +)