Skip to content

Commit 9e06d25

Browse files
committed
Add convenience methods for checking ORM
1 parent 1c27e1e commit 9e06d25

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

lib/meilisearch-rails.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def meilisearch(options = {}, &block)
8888
attr_accessor :formatted
8989

9090
if options[:synchronous] == true
91-
if defined?(::Sequel::Model) && self < Sequel::Model
91+
if ms_config.sequel_model?
9292
class_eval do
9393
copy_after_validation = instance_method(:after_validation)
9494
define_method(:after_validation) do |*args|
@@ -122,7 +122,7 @@ def meilisearch(options = {}, &block)
122122
end
123123
end
124124
unless options[:auto_index] == false
125-
if defined?(::Sequel::Model) && self < Sequel::Model
125+
if ms_config.sequel_model?
126126
class_eval do
127127
copy_after_validation = instance_method(:after_validation)
128128
copy_before_save = instance_method(:before_save)
@@ -169,7 +169,7 @@ def meilisearch(options = {}, &block)
169169
end
170170
end
171171
unless options[:auto_remove] == false
172-
if defined?(::Sequel::Model) && self < Sequel::Model
172+
if ms_config.sequel_model?
173173
class_eval do
174174
copy_after_destroy = instance_method(:after_destroy)
175175

@@ -391,7 +391,7 @@ def ms_search(query, params = {})
391391
# The condition_key must be a valid column otherwise, the `.where` below will not work
392392
# Since we provide a way to customize the primary_key value, `ms_pk(meilisearch_options)` may not
393393
# respond with a valid database column. The blocks below prevent that from happening.
394-
has_virtual_column_as_pk = if defined?(::Sequel::Model) && self < Sequel::Model
394+
has_virtual_column_as_pk = if ms_config.sequel_model?
395395
columns.map(&:to_s).exclude?(condition_key.to_s)
396396
else
397397
columns.map(&:name).map(&:to_s).exclude?(condition_key.to_s)
@@ -591,10 +591,10 @@ def ms_indexing_disabled?(options = nil)
591591
end
592592

593593
def ms_find_in_batches(batch_size, &block)
594-
if (defined?(::ActiveRecord) && ancestors.include?(::ActiveRecord::Base)) || respond_to?(:find_in_batches)
594+
if ms_config.active_record_model? || respond_to?(:find_in_batches)
595595
scope = respond_to?(:meilisearch_import) ? meilisearch_import : all
596596
scope.find_in_batches(batch_size: batch_size, &block)
597-
elsif defined?(::Sequel::Model) && self < Sequel::Model
597+
elsif ms_config.sequel_model?
598598
dataset.extension(:pagination).each_page(batch_size, &block)
599599
else
600600
# don't worry, mongoid has its own underlying cursor/streaming mechanism
@@ -682,7 +682,7 @@ def ms_mark_must_reindex
682682
# ms_must_reindex flag is reset after every commit as part. If we must reindex at any point in
683683
# a transaction, keep flag set until it is explicitly unset
684684
@ms_must_reindex ||=
685-
if defined?(::Sequel::Model) && is_a?(Sequel::Model)
685+
if self.class.ms_config.sequel_model?
686686
new? || self.class.ms_must_reindex?(self)
687687
else
688688
new_record? || self.class.ms_must_reindex?(self)

lib/meilisearch/rails/model_configuration.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ def initialize(model, options = {})
88
parse_options(options)
99
end
1010

11+
def sequel_model?
12+
defined?(::Sequel::Model) && model < Sequel::Model
13+
end
14+
15+
def active_record_model?
16+
defined?(::ActiveRecord) && model.ancestors.include?(::ActiveRecord::Base)
17+
end
18+
1119
private
1220

1321
def parse_options(options)

spec/model_configuration_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require 'spec_helper'
22
require 'support/models/unconfigured_model'
3+
require 'support/sequel_models/book'
4+
require 'support/models/color'
35

46
describe 'Model configuration' do
57
describe 'options' do
@@ -19,4 +21,26 @@
1921
end
2022
end
2123
end
24+
25+
describe '#sequel_model?' do
26+
it 'returns false for activerecord' do
27+
expect(Color.ms_config).not_to be_sequel_model
28+
end
29+
30+
it 'returns true for sequel' do
31+
expect(SequelBook.ms_config).to be_sequel_model
32+
end
33+
34+
# TODO: Add similar methods for mongodb
35+
end
36+
37+
describe '#active_record_model?' do
38+
it 'returns true for activerecord' do
39+
expect(Color.ms_config).to be_active_record_model
40+
end
41+
42+
it 'returns false for sequel' do
43+
expect(SequelBook.ms_config).not_to be_active_record_model
44+
end
45+
end
2246
end

0 commit comments

Comments
 (0)