meilisearch-rails 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: affe6d35bf6c1c626570b32ae605e3b1e826c16c86e87d9ea4cc2e67cb4c1512
4
- data.tar.gz: b950799c5e8296d5fcbc2e194480cbbc4fa5a60b704708db55859f7d0a6d8e4c
3
+ metadata.gz: 849d3f11092d279468f2303406fac8ed52849490cd5f044d0f111c2731593f76
4
+ data.tar.gz: 41ec2f87a63e74bb6c4237435490b381f80def5dee0f8393c3a40cd583386a03
5
5
  SHA512:
6
- metadata.gz: 7a84a8fbbb00122c28c90c5573f1e5a93a3012a89f516ea35df68eba294aea20b7e169906400ad27b17d281359a2d3fb5933eda1338aecb2941e206b21b00002
7
- data.tar.gz: c735267b627ef2100fc0cdd52bffa8d323d634b865c5ef5ba870d3839f8e2a0d03244e5478a056a26295fbd827c6ecbbd40a2994df8805fa194925add4c8533a
6
+ metadata.gz: c6fb870cced8f9e395f5bbc23404331d932b8ebe92921f9867ce53e4b682a114d4a39a0d319a44395fe9d2e4f5907752ca923febee4503b4fa23df437f2237ab
7
+ data.tar.gz: d74ded33afca229deb79ed57ab820c0737f1bb2b3f258aafa4155a3d29515e24a0b9cb5680d106d0ebd9e39892f9a9e45aed8a50b934669558b8c9c11d1eddb5
data/Gemfile CHANGED
@@ -29,6 +29,7 @@ group :test do
29
29
  gem 'jdbc-sqlite3', platform: :jruby
30
30
  gem 'rspec', '~> 3.0'
31
31
  gem 'simplecov', require: 'false'
32
+ gem 'threads'
32
33
 
33
34
  gem 'byebug'
34
35
  gem 'dotenv', '~> 2.7', '>= 2.7.6'
data/README.md CHANGED
@@ -254,6 +254,42 @@ Book.search('*', sort: ['title:asc'])
254
254
 
255
255
  ### Meilisearch configuration & environment
256
256
 
257
+ #### Deactivate Meilisearch in certain moments
258
+
259
+ By default HTTP connections to the Meilisearch URL is always active, but sometimes you want to disable the HTTP requests in a particular moment or environment.<br>
260
+ you have multiple ways to achieve this.
261
+
262
+ By adding `active: false` in the configuration initializer:
263
+
264
+ ```ruby
265
+ MeiliSearch::Rails.configuration = {
266
+ meilisearch_host: 'YourMeilisearchHost',
267
+ meilisearch_api_key: 'YourMeilisearchAPIKey',
268
+ active: false
269
+ }
270
+ ```
271
+
272
+ Or you can disable programmatically:
273
+
274
+ ```ruby
275
+ MeiliSearch::Rails.deactivate! # all the following HTTP calls will be dismissed.
276
+
277
+ # or you can pass a block to it:
278
+
279
+ MeiliSearch::Rails.deactivate! do
280
+ # every Meilisearch call here will be dismissed, no error will be raised.
281
+ # after the block, Meilisearch state will be active.
282
+ end
283
+ ```
284
+
285
+ You can also activate if you deactivated earlier:
286
+
287
+ ```ruby
288
+ MeiliSearch::Rails.activate!
289
+ ```
290
+
291
+ :warning: These calls are persistent, so prefer to use the method with the block. This way, you will not forget to activate it afterward.
292
+
257
293
  #### Custom index_uid <!-- omit in toc -->
258
294
 
259
295
  By default, the **index_uid** will be the class name, e.g. `Book`. You can customize the index_uid by using the `index_uid:` option.
@@ -11,7 +11,39 @@ module MeiliSearch
11
11
  @_config = configuration
12
12
  end
13
13
 
14
+ def deactivate!
15
+ semaphore.synchronize do
16
+ @_config.merge!(active: false)
17
+
18
+ return unless block_given?
19
+
20
+ yield
21
+
22
+ @_config.merge!(active: true)
23
+ end
24
+ end
25
+
26
+ def activate!
27
+ semaphore.synchronize do
28
+ @_config.merge!(active: true)
29
+ end
30
+ end
31
+
32
+ def active?
33
+ configuration.fetch(:active, true)
34
+ end
35
+
36
+ def black_hole
37
+ @black_hole ||= NullObject.instance
38
+ end
39
+
40
+ def semaphore
41
+ @semaphore ||= Mutex.new
42
+ end
43
+
14
44
  def client
45
+ return black_hole unless active?
46
+
15
47
  ::MeiliSearch::Client.new(
16
48
  configuration[:meilisearch_host] || 'http://localhost:7700',
17
49
  configuration[:meilisearch_api_key],
@@ -0,0 +1,25 @@
1
+ require 'singleton'
2
+
3
+ module MeiliSearch
4
+ module Rails
5
+ class NullObject
6
+ include Singleton
7
+
8
+ def map
9
+ []
10
+ end
11
+
12
+ def nil?
13
+ true
14
+ end
15
+
16
+ def method_missing(_method, *_args, &_block)
17
+ self
18
+ end
19
+
20
+ def respond_to_missing?(_method_name, _include_private = false)
21
+ false
22
+ end
23
+ end
24
+ end
25
+ end
@@ -40,6 +40,31 @@ module MeiliSearch
40
40
  klass.ms_set_settings
41
41
  end
42
42
  end
43
+
44
+ def indexable?(record, options)
45
+ return false unless options[:if].blank? || constraint_passes?(record, options[:if])
46
+ return false unless options[:unless].blank? || !constraint_passes?(record, options[:unless])
47
+
48
+ true
49
+ end
50
+
51
+ private
52
+
53
+ def constraint_passes?(record, constraint)
54
+ case constraint
55
+ when Symbol
56
+ record.send(constraint)
57
+ when String
58
+ record.send(constraint.to_sym)
59
+ when Enumerable
60
+ # All constraints must pass
61
+ constraint.all? { |inner_constraint| constraint_passes?(record, inner_constraint) }
62
+ else
63
+ raise ArgumentError, "Unknown constraint type: #{constraint} (#{constraint.class})" unless constraint.respond_to?(:call)
64
+
65
+ constraint.call(record)
66
+ end
67
+ end
43
68
  end
44
69
  end
45
70
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module MeiliSearch
4
4
  module Rails
5
- VERSION = '0.7.2'
5
+ VERSION = '0.7.3'
6
6
 
7
7
  def self.qualified_version
8
8
  "Meilisearch Rails (v#{VERSION})"
@@ -1,5 +1,5 @@
1
1
  require 'meilisearch'
2
-
2
+ require 'meilisearch/rails/null_object'
3
3
  require 'meilisearch/rails/version'
4
4
  require 'meilisearch/rails/utilities'
5
5
  require 'meilisearch/rails/errors'
@@ -249,7 +249,10 @@ module MeiliSearch
249
249
  args[0].delete(:attributesToCrop) if args[0][:attributesToCrop]
250
250
  args[0].delete(:cropLength) if args[0][:cropLength]
251
251
  end
252
+
252
253
  SafeIndex.log_or_throw(m, @raise_on_failure) do
254
+ return MeiliSearch::Rails.black_hole unless MeiliSearch::Rails.active?
255
+
253
256
  @index.send(m, *args, &block)
254
257
  end
255
258
  end
@@ -342,7 +345,7 @@ module MeiliSearch
342
345
  end
343
346
  end
344
347
  if options[:enqueue]
345
- raise ArgumentError, 'Cannot use a enqueue if the `synchronous` option if set' if options[:synchronous]
348
+ raise ArgumentError, 'Cannot use a enqueue if the `synchronous` option is set' if options[:synchronous]
346
349
 
347
350
  proc = if options[:enqueue] == true
348
351
  proc do |record, remove|
@@ -452,10 +455,10 @@ module MeiliSearch
452
455
  ms_find_in_batches(batch_size) do |group|
453
456
  if ms_conditional_index?(options)
454
457
  # delete non-indexable documents
455
- ids = group.select { |d| !ms_indexable?(d, options) }.map { |d| ms_primary_key_of(d, options) }
458
+ ids = group.select { |d| !Utilities.indexable?(d, options) }.map { |d| ms_primary_key_of(d, options) }
456
459
  index.delete_documents(ids.select(&:present?))
457
460
  # select only indexable documents
458
- group = group.select { |d| ms_indexable?(d, options) }
461
+ group = group.select { |d| Utilities.indexable?(d, options) }
459
462
  end
460
463
  documents = group.map do |d|
461
464
  attributes = settings.get_attributes(d)
@@ -502,7 +505,7 @@ module MeiliSearch
502
505
 
503
506
  primary_key = ms_primary_key_of(document, options)
504
507
  index = ms_ensure_init(options, settings)
505
- if ms_indexable?(document, options)
508
+ if Utilities.indexable?(document, options)
506
509
  raise ArgumentError, 'Cannot index a record without a primary key' if primary_key.blank?
507
510
 
508
511
  doc = settings.get_attributes(document)
@@ -550,7 +553,7 @@ module MeiliSearch
550
553
 
551
554
  index = ms_ensure_init(options, settings)
552
555
  synchronous || options[:synchronous] ? index.delete_all_documents! : index.delete_all_documents
553
- @ms_indexes[settings] = nil
556
+ @ms_indexes[MeiliSearch::Rails.active?][settings] = nil
554
557
  end
555
558
  nil
556
559
  end
@@ -720,16 +723,16 @@ module MeiliSearch
720
723
  def ms_ensure_init(options = nil, settings = nil, index_settings = nil)
721
724
  raise ArgumentError, 'No `meilisearch` block found in your model.' if meilisearch_settings.nil?
722
725
 
723
- @ms_indexes ||= {}
726
+ @ms_indexes ||= { true => {}, false => {} }
724
727
 
725
728
  options ||= meilisearch_options
726
729
  settings ||= meilisearch_settings
727
730
 
728
- return @ms_indexes[settings] if @ms_indexes[settings]
731
+ return @ms_indexes[MeiliSearch::Rails.active?][settings] if @ms_indexes[MeiliSearch::Rails.active?][settings]
729
732
 
730
- @ms_indexes[settings] = SafeIndex.new(ms_index_uid(options), meilisearch_options[:raise_on_failure], meilisearch_options)
733
+ @ms_indexes[MeiliSearch::Rails.active?][settings] = SafeIndex.new(ms_index_uid(options), meilisearch_options[:raise_on_failure], meilisearch_options)
731
734
 
732
- current_settings = @ms_indexes[settings].settings(getVersion: 1) rescue nil # if the index doesn't exist
735
+ current_settings = @ms_indexes[MeiliSearch::Rails.active?][settings].settings(getVersion: 1) rescue nil # if the index doesn't exist
733
736
 
734
737
  index_settings ||= settings.to_settings
735
738
  index_settings = options[:primary_settings].to_settings.merge(index_settings) if options[:inherit]
@@ -737,10 +740,10 @@ module MeiliSearch
737
740
  options[:check_settings] = true if options[:check_settings].nil?
738
741
 
739
742
  if !ms_indexing_disabled?(options) && options[:check_settings] && meilisearch_settings_changed?(current_settings, index_settings)
740
- @ms_indexes[settings].update_settings(index_settings)
743
+ @ms_indexes[MeiliSearch::Rails.active?][settings].update_settings(index_settings)
741
744
  end
742
745
 
743
- @ms_indexes[settings]
746
+ @ms_indexes[MeiliSearch::Rails.active?][settings]
744
747
  end
745
748
 
746
749
  private
@@ -802,31 +805,6 @@ module MeiliSearch
802
805
  options[:if].present? || options[:unless].present?
803
806
  end
804
807
 
805
- def ms_indexable?(document, options = nil)
806
- options ||= meilisearch_options
807
- if_passes = options[:if].blank? || ms_constraint_passes?(document, options[:if])
808
- unless_passes = options[:unless].blank? || !ms_constraint_passes?(document, options[:unless])
809
- if_passes && unless_passes
810
- end
811
-
812
- def ms_constraint_passes?(document, constraint)
813
- case constraint
814
- when Symbol
815
- document.send(constraint)
816
- when String
817
- document.send(constraint.to_sym)
818
- when Enumerable
819
- # All constraints must pass
820
- constraint.all? { |inner_constraint| ms_constraint_passes?(document, inner_constraint) }
821
- else
822
- unless constraint.respond_to?(:call)
823
- raise ArgumentError, "Unknown constraint type: #{constraint} (#{constraint.class})"
824
- end
825
-
826
- constraint.call(document)
827
- end
828
- end
829
-
830
808
  def ms_indexing_disabled?(options = nil)
831
809
  options ||= meilisearch_options
832
810
  constraint = options[:disable_indexing] || options['disable_indexing']
@@ -902,6 +880,8 @@ module MeiliSearch
902
880
  end
903
881
 
904
882
  def ms_enqueue_index!(synchronous)
883
+ return unless Utilities.indexable?(self, meilisearch_options)
884
+
905
885
  if meilisearch_options[:enqueue]
906
886
  unless self.class.send(:ms_indexing_disabled?, meilisearch_options)
907
887
  meilisearch_options[:enqueue].call(self, false)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meilisearch-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Meili
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-18 00:00:00.000000000 Z
11
+ date: 2022-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: meilisearch
@@ -41,6 +41,7 @@ files:
41
41
  - lib/meilisearch/rails/configuration.rb
42
42
  - lib/meilisearch/rails/errors.rb
43
43
  - lib/meilisearch/rails/ms_job.rb
44
+ - lib/meilisearch/rails/null_object.rb
44
45
  - lib/meilisearch/rails/pagination.rb
45
46
  - lib/meilisearch/rails/pagination/kaminari.rb
46
47
  - lib/meilisearch/rails/pagination/will_paginate.rb