meilisearch-rails 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 317b4503e3616fe8f309ac194c2eb85a902222edda8ae58824c53f081280a0de
4
- data.tar.gz: f9ef605f39f533307b5af4d6204c8fd19fb5d49dd507e72010d7d5458b37e066
3
+ metadata.gz: 70b4e934d08277a2bcc5620b34058374678046e775ad390da4df17056b6bc7bf
4
+ data.tar.gz: 85d3c0967197696220dba8c1eb7ca48d602828d1f2ff1a61e0d072aeb225d628
5
5
  SHA512:
6
- metadata.gz: 1f2d3137a16b624704eeadb64d86c32e0c87864abf7af1fb7df19d8acf90d74ee57db5df1707c073c80a73aec99964519bcace556a6ce8f40828d179b74971f4
7
- data.tar.gz: 0a56e1651b062fc45fcde8e4206d2f08c21af07882bfaaeb80d928fe3cf4fa112a5e2102127f7d598120c978076bbacf2c2681c2452cca0406ac5ac15a899d2a
6
+ metadata.gz: 8931ee95f65d9f7e26949c13859f0e3ad84b3149a850b609af7d6002a6ccf332b1f1dfd9aa5b7ebf7223446149cb7e9a678e2ea83c3f6c1919509d4b9c06acdb
7
+ data.tar.gz: 8366b8664261cf4df2c83757c9214e9e2c0eaddfda514faca0ec70453496f0f4bdae545fa6b0dea14d305712e80326ae33700e59319b1bc96425a52ebac3524f
data/Gemfile CHANGED
@@ -15,10 +15,12 @@ end
15
15
  group :test do
16
16
  rails_version = ENV['RAILS_VERSION'] || '7.1'
17
17
  sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
18
+ mongoid_version = ENV['MONGOID_VERSION'] || '9.0'
18
19
 
19
20
  gem 'active_model_serializers'
20
21
  gem 'rails', "~> #{rails_version}"
21
22
  gem 'sequel', sequel_version
23
+ gem 'mongoid', "~> #{mongoid_version}"
22
24
 
23
25
  # remove when deprecate rails 6
24
26
  if Gem::Version.new(rails_version) >= Gem::Version.new('7.0')
data/README.md CHANGED
@@ -9,7 +9,6 @@
9
9
  <a href="https://www.meilisearch.com/pricing?utm_campaign=oss&utm_source=integration&utm_medium=meilisearch-rails">Meilisearch Cloud</a> |
10
10
  <a href="https://docs.meilisearch.com">Documentation</a> |
11
11
  <a href="https://discord.meilisearch.com">Discord</a> |
12
- <a href="https://roadmap.meilisearch.com/tabs/1-under-consideration">Roadmap</a> |
13
12
  <a href="https://www.meilisearch.com">Website</a> |
14
13
  <a href="https://www.meilisearch.com/docs/faq">FAQ</a>
15
14
  </h4>
@@ -20,7 +19,6 @@
20
19
  <img src="https://codecov.io/gh/meilisearch/meilisearch-rails/branch/main/graph/badge.svg?token=9J7LRP11IR"/>
21
20
  </a>
22
21
  <a href="https://github.com/meilisearch/meilisearch-rails/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-informational" alt="License"></a>
23
- <a href="https://ms-bors.herokuapp.com/repositories/68"><img src="https://bors.tech/images/badge_small.svg" alt="Bors enabled"></a>
24
22
  </p>
25
23
 
26
24
  <p align="center">⚡ The Meilisearch integration for Ruby on Rails 💎</p>
@@ -113,7 +111,7 @@ bin/rails meilisearch:install
113
111
 
114
112
  The gem is compatible with [ActiveRecord](https://github.com/rails/rails/tree/master/activerecord), [Mongoid](https://github.com/mongoid/mongoid) and [Sequel](https://github.com/jeremyevans/sequel).
115
113
 
116
- ⚠️ Note that even if you want to use all the default options, you must declare an empty `meilisearch` block in your model.
114
+ ⚠️ Note that even if you want to use all the default options, you must declare an empty `meilisearch` block in your model.
117
115
 
118
116
  #### Add documents <!-- omit in toc -->
119
117
 
@@ -348,7 +346,7 @@ An enumerable `FederatedSearchResult` is returned, which can be iterated through
348
346
 
349
347
 
350
348
  <ul>
351
- <!-- Attack on Titan appears first even though it was specified second,
349
+ <!-- Attack on Titan appears first even though it was specified second,
352
350
  it's ranked higher because it's a closer match -->
353
351
  <li>Attack on Titan</li>
354
352
  <li>Harry Potter and the Philosopher's Stone</li>
@@ -387,7 +385,7 @@ results = Meilisearch::Rails.federated_search(
387
385
 
388
386
  ### Loading records <!-- omit in toc -->
389
387
 
390
- Records are loaded when the `:scope` option is passed (may be a model or a relation),
388
+ Records are loaded when the `:scope` option is passed (may be a model or a relation),
391
389
  or when a hash query is used with models as keys:
392
390
 
393
391
  ```ruby
@@ -601,7 +599,7 @@ Meilisearch::Rails.deactivate! # all the following HTTP calls will be dismissed.
601
599
 
602
600
  Meilisearch::Rails.deactivate! do
603
601
  # every Meilisearch call here will be dismissed, no error will be raised.
604
- # after the block, Meilisearch state will be active.
602
+ # after the block, Meilisearch state will be active.
605
603
  end
606
604
  ```
607
605
 
@@ -687,7 +685,7 @@ class Book < ActiveRecord::Base
687
685
  end
688
686
  ```
689
687
 
690
- You can also set the `primary_key` as a method, this method will be evaluated in runtime, and its return
688
+ You can also set the `primary_key` as a method, this method will be evaluated in runtime, and its return
691
689
  will be used as the reference to the document when Meilisearch needs it.
692
690
 
693
691
  ```rb
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
+ require 'bundler/gem_tasks'
3
4
 
4
5
  require 'rdoc/task'
5
6
 
@@ -22,7 +22,7 @@ namespace :meilisearch do
22
22
 
23
23
  desc 'Create initializer file'
24
24
  task install: :environment do
25
- puts 'Creating initializer file'
25
+ puts 'Writing initializer file at config/initializers/meilisearch.rb'
26
26
 
27
27
  copy_file "#{__dir__}/../templates/initializer.rb", 'config/initializers/meilisearch.rb'
28
28
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Meilisearch
4
4
  module Rails
5
- VERSION = '0.15.0'
5
+ VERSION = '0.17.0'
6
6
 
7
7
  def self.qualified_version
8
8
  "Meilisearch Rails (v#{VERSION})"
@@ -96,7 +96,6 @@ module Meilisearch
96
96
  def warn_searchable_missing_attributes
97
97
  searchables = get_setting(:searchable_attributes)&.map { |searchable| searchable.to_s.split('.').first }
98
98
  attrs = get_setting(:attributes)&.map { |k, _| k.to_s }
99
-
100
99
  if searchables.present? && attrs.present?
101
100
  (searchables - attrs).each do |missing_searchable|
102
101
  warning = <<~WARNING
@@ -110,7 +109,6 @@ module Meilisearch
110
109
 
111
110
  def use_serializer(serializer)
112
111
  @serializer = serializer
113
- # instance_variable_set("@serializer", serializer)
114
112
  end
115
113
 
116
114
  def attribute(*names, &block)
@@ -147,7 +145,6 @@ module Meilisearch
147
145
 
148
146
  def get_default_attributes(document)
149
147
  if mongoid?(document)
150
- # work-around mongoid 2.4's unscoped method, not accepting a block
151
148
  document.attributes
152
149
  elsif sequel?(document)
153
150
  document.to_hash
@@ -158,44 +155,34 @@ module Meilisearch
158
155
  end
159
156
  end
160
157
 
161
- def get_attribute_names(document)
162
- get_attributes(document).keys
158
+ def indexed_field_names(document)
159
+ base_field_names = if @serializer.nil?
160
+ @attributes.present? ? @attributes.keys : get_default_attributes(document).keys
161
+ else
162
+ @serializer.new(document).attributes.keys
163
+ end
164
+ (base_field_names + (@additional_attributes&.keys || [])).uniq
163
165
  end
164
166
 
165
167
  def attributes_to_hash(attributes, document)
166
- if attributes
167
- attributes.to_h { |name, value| [name.to_s, value.call(document)] }
168
- else
169
- {}
170
- end
171
- end
172
-
173
- def get_attributes(document)
174
- # If a serializer is set, we ignore attributes
175
- # everything should be done via the serializer
176
- if !@serializer.nil?
177
- attributes = @serializer.new(document).attributes
178
- elsif @attributes.blank?
179
- attributes = get_default_attributes(document)
180
- # no `attribute ...` have been configured, use the default attributes of the model
181
- elsif active_record?(document)
182
- # at least 1 `attribute ...` has been configured, therefore use ONLY the one configured
183
- document.class.unscoped do
184
- attributes = attributes_to_hash(@attributes, document)
185
- end
186
- else
187
- attributes = attributes_to_hash(@attributes, document)
188
- end
189
-
168
+ attributes ? attributes.to_h { |name, value| [name.to_s, value.call(document)] } : {}
169
+ end
170
+
171
+ def build_document_attributes(document)
172
+ attributes = if !@serializer.nil?
173
+ @serializer.new(document).attributes
174
+ elsif @attributes.present?
175
+ if active_record?(document)
176
+ document.class.unscoped { attributes_to_hash(@attributes, document) }
177
+ else
178
+ attributes_to_hash(@attributes, document)
179
+ end
180
+ else
181
+ get_default_attributes(document)
182
+ end
190
183
  attributes.merge!(attributes_to_hash(@additional_attributes, document)) if @additional_attributes
191
-
192
- if @options[:sanitize]
193
- attributes = sanitize_attributes(attributes)
194
- end
195
-
196
- attributes = encode_attributes(attributes) if @options[:force_utf8_encoding]
197
-
198
- attributes
184
+ attributes = sanitize_attributes(attributes) if @options[:sanitize]
185
+ @options[:force_utf8_encoding] ? encode_attributes(attributes) : attributes
199
186
  end
200
187
 
201
188
  def sanitize_attributes(value)
@@ -356,7 +343,7 @@ module Meilisearch
356
343
  end
357
344
 
358
345
  # these are the class methods added when Meilisearch is included
359
- module ClassMethods
346
+ module ClassMethods # rubocop:disable Metrics/ModuleLength
360
347
  def self.extended(base)
361
348
  class << base
362
349
  alias_method :without_auto_index, :ms_without_auto_index unless method_defined? :without_auto_index
@@ -483,7 +470,7 @@ module Meilisearch
483
470
  end
484
471
  end
485
472
  elsif respond_to?(:after_destroy)
486
- after_destroy_commit { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
473
+ after_commit(on: :destroy) { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
487
474
  end
488
475
  end
489
476
  end
@@ -523,7 +510,7 @@ module Meilisearch
523
510
  group = group.select { |d| Utilities.indexable?(d, options) }
524
511
  end
525
512
  documents = group.map do |d|
526
- attributes = settings.get_attributes(d)
513
+ attributes = settings.build_document_attributes(d)
527
514
  attributes = attributes.to_hash unless attributes.instance_of?(Hash)
528
515
  attributes.merge ms_pk(options) => ms_primary_key_of(d, options)
529
516
  end
@@ -554,7 +541,7 @@ module Meilisearch
554
541
  next if ms_indexing_disabled?(options)
555
542
 
556
543
  index = ms_ensure_init(options, settings)
557
- task = index.add_documents(documents.map { |d| settings.get_attributes(d).merge ms_pk(options) => ms_primary_key_of(d, options) })
544
+ task = index.add_documents(documents.map { |d| settings.build_document_attributes(d).merge ms_pk(options) => ms_primary_key_of(d, options) })
558
545
  index.wait_for_task(task['taskUid']) if synchronous || options[:synchronous]
559
546
  end
560
547
  end
@@ -571,7 +558,7 @@ module Meilisearch
571
558
  if Utilities.indexable?(document, options)
572
559
  raise ArgumentError, 'Cannot index a record without a primary key' if primary_key.blank?
573
560
 
574
- doc = settings.get_attributes(document)
561
+ doc = settings.build_document_attributes(document)
575
562
  doc = doc.merge ms_pk(options) => primary_key
576
563
 
577
564
  if synchronous || options[:synchronous]
@@ -696,6 +683,8 @@ module Meilisearch
696
683
  # respond with a valid database column. The blocks below prevent that from happening.
697
684
  has_virtual_column_as_pk = if defined?(::Sequel::Model) && self < Sequel::Model
698
685
  meilisearch_options[:type].columns.map(&:to_s).exclude?(condition_key.to_s)
686
+ elsif Utilities.mongo_model?(self)
687
+ fields.keys.exclude?(condition_key.to_s)
699
688
  else
700
689
  meilisearch_options[:type].columns.map(&:name).map(&:to_s).exclude?(condition_key.to_s)
701
690
  end
@@ -759,7 +748,7 @@ module Meilisearch
759
748
  next if ms_indexing_disabled?(options)
760
749
  return true if ms_primary_key_changed?(document, options)
761
750
 
762
- settings.get_attribute_names(document).each do |k|
751
+ settings.indexed_field_names(document).each do |k|
763
752
  return true if ms_attribute_changed?(document, k)
764
753
  # return true if !document.respond_to?(changed_method) || document.send(changed_method)
765
754
  end
@@ -784,7 +773,7 @@ module Meilisearch
784
773
 
785
774
  def ms_primary_key_method(options = nil)
786
775
  options ||= meilisearch_options
787
- options[:primary_key] || options[:id] || :id
776
+ options[:primary_key] || options[:id] || (Utilities.mongo_model?(self) ? :_id : :id)
788
777
  end
789
778
 
790
779
  protected
@@ -794,11 +783,11 @@ module Meilisearch
794
783
 
795
784
  @ms_indexes ||= { true => {}, false => {} }
796
785
 
797
- @ms_indexes[Meilisearch::Rails.active?][settings] ||= SafeIndex.new(ms_index_uid(options), meilisearch_options[:raise_on_failure], meilisearch_options)
798
-
799
- update_settings_if_changed(@ms_indexes[Meilisearch::Rails.active?][settings], options, user_configuration)
800
-
801
- @ms_indexes[Meilisearch::Rails.active?][settings]
786
+ @ms_indexes[Meilisearch::Rails.active?][settings] ||= begin
787
+ index = SafeIndex.new(ms_index_uid(options), meilisearch_options[:raise_on_failure], meilisearch_options)
788
+ update_settings_if_changed(index, options, user_configuration)
789
+ index
790
+ end
802
791
  end
803
792
 
804
793
  private
@@ -34,6 +34,6 @@ Gem::Specification.new do |s|
34
34
 
35
35
  s.required_ruby_version = '>= 3.0.0'
36
36
 
37
- s.add_dependency 'meilisearch', '~> 0.31.0'
37
+ s.add_dependency 'meilisearch', '~> 0.32.0'
38
38
  s.add_dependency 'mutex_m', '~> 0.2'
39
39
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meilisearch-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Meili
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-03-25 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: meilisearch
@@ -16,14 +15,14 @@ dependencies:
16
15
  requirements:
17
16
  - - "~>"
18
17
  - !ruby/object:Gem::Version
19
- version: 0.31.0
18
+ version: 0.32.0
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - "~>"
25
24
  - !ruby/object:Gem::Version
26
- version: 0.31.0
25
+ version: 0.32.0
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: mutex_m
29
28
  requirement: !ruby/object:Gem::Requirement
@@ -73,7 +72,6 @@ homepage: https://github.com/meilisearch/meilisearch-rails
73
72
  licenses:
74
73
  - MIT
75
74
  metadata: {}
76
- post_install_message:
77
75
  rdoc_options: []
78
76
  require_paths:
79
77
  - lib
@@ -88,8 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
86
  - !ruby/object:Gem::Version
89
87
  version: '0'
90
88
  requirements: []
91
- rubygems_version: 3.4.19
92
- signing_key:
89
+ rubygems_version: 3.6.9
93
90
  specification_version: 4
94
91
  summary: Meilisearch integration for Ruby on Rails.
95
92
  test_files: []