meilisearch-rails 0.4.1 → 0.5.2

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: ea22a400fc0f909f793e231f12a61b47f1d751d0c5932f71f2503001bf57c39e
4
- data.tar.gz: b05b67044feec7c8199252026794cd33247045adb62bde0f368a82f2597dd27f
3
+ metadata.gz: 2541e903db43e95ae8290c07308159dc9d724949dc1ffa27214df3e52cd37fe6
4
+ data.tar.gz: 8b4e32b34e9e3c4fbae0e5c21efa20d87e611ebada16889ba01ba236343b0f6d
5
5
  SHA512:
6
- metadata.gz: e1d8b8475cf7f5777ed9c897b342bb4283ad3b00ab12b12060e664acecb973fcb87566aaf5b6fb4a98324bfadeec389bc765cb83563b37d9bce8da088a573e53
7
- data.tar.gz: ba4c38cc90384fbcb0623891e57629b7ba3472d7695f108d6623846a534af81c669b1f8b63af7003998c50449c20385b4f0f7a85d9f6eb5ef25daaf13948e3f0
6
+ metadata.gz: 1eba16d0d03695d817998c5249a1ad33d343df4816332a63c7e7b220d42cd2734b9d1ed90e1893a12de6b67d5ed4b1f4e4ca8cad21f3d3e206f8e108f3ffc07f
7
+ data.tar.gz: 53ffa781649f9370e7fe35aa5b7263f8792a7f04ede7c28da6e2f6d85b015ae2fcef236affeea659b11c538385a39ca50fb13dab7857d50e8e76067b813f7dfc
data/Gemfile CHANGED
@@ -1,14 +1,13 @@
1
- source 'http://rubygems.org'
1
+ source 'https://rubygems.org'
2
2
 
3
- gem 'json', '~> 2.5', '>= 2.5.1'
4
- gem 'meilisearch', '~> 0.18.0'
3
+ gemspec
5
4
 
6
5
  gem 'rubysl', '~> 2.0', platform: :rbx if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
7
6
 
8
7
  group :development do
9
- gem 'rubocop', '~> 1.22'
10
- gem 'rubocop-rails'
11
- gem 'rubocop-rspec'
8
+ gem 'rubocop', '1.27.0'
9
+ gem 'rubocop-rails', '2.13.2'
10
+ gem 'rubocop-rspec', '2.9.0'
12
11
  end
13
12
 
14
13
  group :test do
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 MeiliSearch
3
+ Copyright (c) 2021-2022 Meili SAS
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  <p align="center">
2
- <img src="https://res.cloudinary.com/meilisearch/image/upload/v1587402338/SDKs/meilisearch_rails.svg" alt="MeiliSearch-Rails" width="200" height="200" />
2
+ <img src="https://raw.githubusercontent.com/meilisearch/integration-guides/main/assets/logos/meilisearch_rails.svg" alt="Meilisearch-Rails" width="200" height="200" />
3
3
  </p>
4
4
 
5
- <h1 align="center">MeiliSearch Rails</h1>
5
+ <h1 align="center">Meilisearch Rails</h1>
6
6
 
7
7
  <h4 align="center">
8
- <a href="https://github.com/meilisearch/MeiliSearch">MeiliSearch</a> |
8
+ <a href="https://github.com/meilisearch/meilisearch">Meilisearch</a> |
9
9
  <a href="https://docs.meilisearch.com">Documentation</a> |
10
10
  <a href="https://slack.meilisearch.com">Slack</a> |
11
11
  <a href="https://roadmap.meilisearch.com/tabs/1-under-consideration">Roadmap</a> |
@@ -19,21 +19,21 @@
19
19
  <a href="https://app.bors.tech/repositories/33032"><img src="https://bors.tech/images/badge_small.svg" alt="Bors enabled"></a>
20
20
  </p>
21
21
 
22
- <p align="center">⚡ The MeiliSearch integration for Ruby on Rails 💎</p>
22
+ <p align="center">⚡ The Meilisearch integration for Ruby on Rails 💎</p>
23
23
 
24
- **MeiliSearch Rails** is the MeiliSearch integration for Ruby on Rails developers.
24
+ **Meilisearch Rails** is the Meilisearch integration for Ruby on Rails developers.
25
25
 
26
- **MeiliSearch** is an open-source search engine. [Discover what MeiliSearch is!](https://github.com/meilisearch/MeiliSearch)
26
+ **Meilisearch** is an open-source search engine. [Discover what Meilisearch is!](https://github.com/meilisearch/meilisearch)
27
27
 
28
28
  ## Table of Contents <!-- omit in toc -->
29
29
 
30
30
  - [📖 Documentation](#-documentation)
31
- - [🤖 Compatibility with MeiliSearch](#-compatibility-with-meilisearch)
31
+ - [🤖 Compatibility with Meilisearch](#-compatibility-with-meilisearch)
32
32
  - [🚀 Getting Started](#-getting-started)
33
33
  - [⚙️ Settings](#️-settings)
34
34
  - [🔍 Custom search](#-custom-search)
35
35
  - [🪛 Options](#-options)
36
- - [MeiliSearch configuration & environment](#meilisearch-configuration--environment)
36
+ - [Meilisearch configuration & environment](#meilisearch-configuration--environment)
37
37
  - [Index configuration](#index-configuration)
38
38
  - [Custom attribute definition](#custom-attribute-definition)
39
39
  - [Custom primary key](#custom-primary-key)
@@ -54,11 +54,11 @@
54
54
 
55
55
  The whole usage of this gem is detailed in this README.
56
56
 
57
- To learn more about MeiliSearch, check out our [Documentation](https://docs.meilisearch.com/learn/tutorials/getting_started.html) or our [API References](https://docs.meilisearch.com/reference/api/).
57
+ To learn more about Meilisearch, check out our [Documentation](https://docs.meilisearch.com/learn/tutorials/getting_started.html) or our [API References](https://docs.meilisearch.com/reference/api/).
58
58
 
59
- ## 🤖 Compatibility with MeiliSearch
59
+ ## 🤖 Compatibility with Meilisearch
60
60
 
61
- This package only guarantees the compatibility with the [version v0.25.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.25.0).
61
+ This package only guarantees the compatibility with the [version v0.27.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.27.0).
62
62
 
63
63
  ## 🔧 Installation <!-- omit in toc -->
64
64
 
@@ -76,18 +76,18 @@ source 'https://rubygems.org'
76
76
  gem 'meilisearch-rails'
77
77
  ```
78
78
 
79
- ### Run MeiliSearch <!-- omit in toc -->
79
+ ### Run Meilisearch <!-- omit in toc -->
80
80
 
81
- There are many easy ways to [download and run a MeiliSearch instance](https://docs.meilisearch.com/reference/features/installation.html#download-and-launch).
81
+ There are many easy ways to [download and run a Meilisearch instance](https://docs.meilisearch.com/reference/features/installation.html#download-and-launch).
82
82
 
83
83
  For example, if you use Docker:
84
84
 
85
85
  ```bash
86
- docker pull getmeili/meilisearch:latest # Fetch the latest version of MeiliSearch image from Docker Hub
87
- docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey
86
+ docker pull getmeili/meilisearch:latest # Fetch the latest version of Meilisearch image from Docker Hub
87
+ docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest meilisearch --master-key=masterKey
88
88
  ```
89
89
 
90
- NB: you can also download MeiliSearch from **Homebrew** or **APT**.
90
+ NB: you can also download Meilisearch from **Homebrew** or **APT**.
91
91
 
92
92
  ## 🚀 Getting Started
93
93
 
@@ -96,9 +96,9 @@ NB: you can also download MeiliSearch from **Homebrew** or **APT**.
96
96
  Create a new file `config/initializers/meilisearch.rb` to setup your `MEILISEARCH_HOST` and `MEILISEARCH_API_KEY`
97
97
 
98
98
  ```ruby
99
- MeiliSearch.configuration = {
100
- meilisearch_host: 'YourMeiliSearchHost', # example: http://localhost:7700
101
- meilisearch_api_key: 'YourMeiliSearchAPIKey',
99
+ MeiliSearch::Rails.configuration = {
100
+ meilisearch_host: 'YourMeilisearchHost', # example: http://localhost:7700
101
+ meilisearch_api_key: 'YourMeilisearchAPIKey',
102
102
  }
103
103
  ```
104
104
 
@@ -110,11 +110,11 @@ The following code will create a `Book` index and add search capabilities to you
110
110
 
111
111
  ```ruby
112
112
  class Book < ActiveRecord::Base
113
- include MeiliSearch
113
+ include MeiliSearch::Rails
114
114
 
115
115
  meilisearch do
116
- attribute :title, :author # only the attributes 'title', and 'author' will be sent to MeiliSearch
117
- # all attributes will be sent to MeiliSearch if block is left empty
116
+ attribute :title, :author # only the attributes 'title', and 'author' will be sent to Meilisearch
117
+ # all attributes will be sent to Meilisearch if block is left empty
118
118
  end
119
119
  end
120
120
  ```
@@ -128,7 +128,7 @@ We **strongly recommend the use of front-end search** through our [JavaScript AP
128
128
  Search returns ORM-compliant objects reloaded from your database.
129
129
 
130
130
  ```ruby
131
- # MeiliSearch is typo-tolerant:
131
+ # Meilisearch is typo-tolerant:
132
132
  hits = Book.search('harry pottre')
133
133
  hits.each do |hit|
134
134
  puts hit.title
@@ -146,9 +146,9 @@ This gem supports:
146
146
  Specify the `:pagination_backend` in the configuration file:
147
147
 
148
148
  ```ruby
149
- MeiliSearch.configuration = {
150
- meilisearch_host: 'YourMeiliSearchHost',
151
- meilisearch_api_key: 'YourMeiliSearchAPIKey',
149
+ MeiliSearch::Rails.configuration = {
150
+ meilisearch_host: 'YourMeilisearchHost',
151
+ meilisearch_api_key: 'YourMeilisearchAPIKey',
152
152
  pagination_backend: :kaminari #:will_paginate
153
153
  }
154
154
  ```
@@ -178,13 +178,13 @@ Book.search('harry potter', hitsPerPage: 10)
178
178
 
179
179
  #### Extra Configuration <!-- omit in toc -->
180
180
 
181
- Requests made to MeiliSearch may timeout and retry. To adapt the behavior to
181
+ Requests made to Meilisearch may timeout and retry. To adapt the behavior to
182
182
  your needs, you can change the parameters during configuration:
183
183
 
184
184
  ```ruby
185
- MeiliSearch.configuration = {
186
- meilisearch_host: 'YourMeiliSearchHost',
187
- meilisearch_api_key: 'YourMeiliSearchAPIKey',
185
+ MeiliSearch::Rails.configuration = {
186
+ meilisearch_host: 'YourMeilisearchHost',
187
+ meilisearch_api_key: 'YourMeilisearchAPIKey',
188
188
  timeout: 2,
189
189
  max_retries: 1,
190
190
  }
@@ -196,11 +196,12 @@ You can configure the index settings by adding them inside the `meilisearch` blo
196
196
 
197
197
  ```ruby
198
198
  class Book < ApplicationRecord
199
- include MeiliSearch
199
+ include MeiliSearch::Rails
200
200
 
201
201
  meilisearch do
202
202
  searchable_attributes [:title, :author, :publisher, :description]
203
203
  filterable_attributes [:genre]
204
+ sortable_attributes [:title]
204
205
  ranking_rules [
205
206
  'proximity',
206
207
  'typo',
@@ -232,9 +233,20 @@ Book.search('Harry', attributesToHighlight: ['*'])
232
233
  👉 Don't forget that `attributes_to_highlight`, `attributes_to_crop`, and
233
234
  `crop_length` can be set up in the `meilisearch` block of your model.
234
235
 
236
+ ## 🔍 Sorted search
237
+
238
+ As an example of how to use the sort option, here is how you could achieve
239
+ returning all books sorted by title in ascending order:
240
+
241
+ ```ruby
242
+ Book.search('*', sort: ['title:asc'])
243
+ ```
244
+
245
+ 👉 Don't forget to set up the `sortable_attributes` option in the `meilisearch` block of your model.
246
+
235
247
  ## 🪛 Options
236
248
 
237
- ### MeiliSearch configuration & environment
249
+ ### Meilisearch configuration & environment
238
250
 
239
251
  #### Custom index_uid <!-- omit in toc -->
240
252
 
@@ -242,7 +254,7 @@ By default, the **index_uid** will be the class name, e.g. `Book`. You can custo
242
254
 
243
255
  ```ruby
244
256
  class Book < ActiveRecord::Base
245
- include MeiliSearch
257
+ include MeiliSearch::Rails
246
258
 
247
259
  meilisearch index_uid: 'MyCustomUID' do
248
260
  end
@@ -255,7 +267,7 @@ You can suffix the index UID with the current Rails environment using the follow
255
267
 
256
268
  ```ruby
257
269
  class Book < ActiveRecord::Base
258
- include MeiliSearch
270
+ include MeiliSearch::Rails
259
271
 
260
272
  meilisearch per_environment: true do # The index UID will be "Book_#{Rails.env}"
261
273
  end
@@ -272,7 +284,7 @@ You can add a custom attribute by using the `add_attribute` option or by using a
272
284
 
273
285
  ```ruby
274
286
  class Author < ApplicationRecord
275
- include MeiliSearch
287
+ include MeiliSearch::Rails
276
288
 
277
289
  meilisearch do
278
290
  attribute :first_name, :last_name
@@ -304,7 +316,7 @@ Note that the primary key must have a **unique value**.
304
316
 
305
317
  ```ruby
306
318
  class Book < ActiveRecord::Base
307
- include MeiliSearch
319
+ include MeiliSearch::Rails
308
320
 
309
321
  meilisearch primary_key: 'ISBN' do
310
322
  end
@@ -317,7 +329,7 @@ As soon as you use those constraints, `add_documents` and `delete_documents` cal
317
329
 
318
330
  ```ruby
319
331
  class Book < ActiveRecord::Base
320
- include MeiliSearch
332
+ include MeiliSearch::Rails
321
333
 
322
334
  meilisearch if: :published?, unless: :premium? do
323
335
  end
@@ -341,7 +353,7 @@ You can index a record in several indexes using the `add_index` option:
341
353
 
342
354
  ```ruby
343
355
  class Book < ActiveRecord::Base
344
- include MeiliSearch
356
+ include MeiliSearch::Rails
345
357
 
346
358
  PUBLIC_INDEX_UID = 'Books'
347
359
  SECURED_INDEX_UID = 'PrivateBooks'
@@ -369,7 +381,7 @@ You may want to share an index between several models. You'll need to ensure you
369
381
 
370
382
  ```ruby
371
383
  class Cat < ActiveRecord::Base
372
- include MeiliSearch
384
+ include MeiliSearch::Rails
373
385
 
374
386
  meilisearch index_uid: 'Animals', primary_key: :ms_id do
375
387
  end
@@ -381,7 +393,7 @@ class Cat < ActiveRecord::Base
381
393
  end
382
394
 
383
395
  class Dog < ActiveRecord::Base
384
- include MeiliSearch
396
+ include MeiliSearch::Rails
385
397
 
386
398
  meilisearch index_uid: 'Animals', primary_key: :ms_id do
387
399
  end
@@ -399,7 +411,7 @@ You can configure the auto-indexing & auto-removal process to use a queue to per
399
411
 
400
412
  ```ruby
401
413
  class Book < ActiveRecord::Base
402
- include MeiliSearch
414
+ include MeiliSearch::Rails
403
415
 
404
416
  meilisearch enqueue: true do # ActiveJob will be triggered using a `meilisearch` queue
405
417
  end
@@ -414,7 +426,7 @@ With **ActiveJob**:
414
426
 
415
427
  ```ruby
416
428
  class Book < ActiveRecord::Base
417
- include MeiliSearch
429
+ include MeiliSearch::Rails
418
430
 
419
431
  meilisearch enqueue: :trigger_job do
420
432
  attribute :title, :author, :description
@@ -430,7 +442,7 @@ class MyActiveJob < ApplicationJob
430
442
  if remove
431
443
  # The record has likely already been removed from your database so we cannot
432
444
  # use ActiveRecord#find to load it.
433
- # We access the underlying MeiliSearch index object.
445
+ # We access the underlying Meilisearch index object.
434
446
  Book.index.delete_document(id)
435
447
  else
436
448
  # The record should be present.
@@ -444,23 +456,23 @@ With [**Sidekiq**](https://github.com/mperham/sidekiq):
444
456
 
445
457
  ```ruby
446
458
  class Book < ActiveRecord::Base
447
- include MeiliSearch
459
+ include MeiliSearch::Rails
448
460
 
449
- meilisearch enqueue: :trigger_sidekiq_worker do
461
+ meilisearch enqueue: :trigger_sidekiq_job do
450
462
  attribute :title, :author, :description
451
463
  end
452
464
 
453
- def self.trigger_sidekiq_worker(record, remove)
454
- MySidekiqWorker.perform_async(record.id, remove)
465
+ def self.trigger_sidekiq_job(record, remove)
466
+ MySidekiqJob.perform_async(record.id, remove)
455
467
  end
456
468
  end
457
469
 
458
- class MySidekiqWorker
470
+ class MySidekiqJob
459
471
  def perform(id, remove)
460
472
  if remove
461
473
  # The record has likely already been removed from your database so we cannot
462
474
  # use ActiveRecord#find to load it.
463
- # We access the underlying MeiliSearch index object.
475
+ # We access the underlying Meilisearch index object.
464
476
  Book.index.delete_document(id)
465
477
  else
466
478
  # The record should be present.
@@ -474,7 +486,7 @@ With [**DelayedJob**](https://github.com/collectiveidea/delayed_job):
474
486
 
475
487
  ```ruby
476
488
  class Book < ActiveRecord::Base
477
- include MeiliSearch
489
+ include MeiliSearch::Rails
478
490
 
479
491
  meilisearch enqueue: :trigger_delayed_job do
480
492
  attribute :title, :author, :description
@@ -498,7 +510,7 @@ Extend a change to a related record.
498
510
 
499
511
  ```ruby
500
512
  class Author < ActiveRecord::Base
501
- include MeiliSearch
513
+ include MeiliSearch::Rails
502
514
 
503
515
  has_many :books
504
516
  # If your association uses belongs_to
@@ -508,7 +520,7 @@ class Author < ActiveRecord::Base
508
520
  end
509
521
 
510
522
  class Book < ActiveRecord::Base
511
- include MeiliSearch
523
+ include MeiliSearch::Rails
512
524
 
513
525
  belongs_to :author
514
526
  after_touch :index!
@@ -527,7 +539,7 @@ With **Sequel**, you can use the `touch` plugin to propagate changes.
527
539
  ```ruby
528
540
  # app/models/author.rb
529
541
  class Author < Sequel::Model
530
- include MeiliSearch
542
+ include MeiliSearch::Rails
531
543
 
532
544
  one_to_many :books
533
545
 
@@ -549,7 +561,7 @@ end
549
561
 
550
562
  # app/models/book.rb
551
563
  class Book < Sequel::Model
552
- include MeiliSearch
564
+ include MeiliSearch::Rails
553
565
 
554
566
  many_to_one :author
555
567
  after_touch :index!
@@ -572,7 +584,7 @@ You can strip all HTML tags from your attributes with the `sanitize` option.
572
584
 
573
585
  ```ruby
574
586
  class Book < ActiveRecord::Base
575
- include MeiliSearch
587
+ include MeiliSearch::Rails
576
588
 
577
589
  meilisearch sanitize: true do
578
590
  end
@@ -585,7 +597,7 @@ You can force the UTF-8 encoding of all your attributes using the `force_utf8_en
585
597
 
586
598
  ```ruby
587
599
  class Book < ActiveRecord::Base
588
- include MeiliSearch
600
+ include MeiliSearch::Rails
589
601
 
590
602
  meilisearch force_utf8_encoding: true do
591
603
  end
@@ -633,11 +645,11 @@ index = Book.index
633
645
 
634
646
  #### Exceptions <!-- omit in toc -->
635
647
 
636
- You can disable exceptions that could be raised while trying to reach MeiliSearch's API by using the `raise_on_failure` option:
648
+ You can disable exceptions that could be raised while trying to reach Meilisearch's API by using the `raise_on_failure` option:
637
649
 
638
650
  ```ruby
639
651
  class Book < ActiveRecord::Base
640
- include MeiliSearch
652
+ include MeiliSearch::Rails
641
653
 
642
654
  # Only raise exceptions in development environment.
643
655
  meilisearch raise_on_failure: Rails.env.development? do
@@ -653,7 +665,7 @@ You can force indexing and removing to be synchronous by setting the following o
653
665
 
654
666
  ```ruby
655
667
  class Book < ActiveRecord::Base
656
- include MeiliSearch
668
+ include MeiliSearch::Rails
657
669
 
658
670
  meilisearch synchronous: true do
659
671
  end
@@ -667,7 +679,7 @@ You can disable auto-indexing and auto-removing setting the following options:
667
679
 
668
680
  ```ruby
669
681
  class Book < ActiveRecord::Base
670
- include MeiliSearch
682
+ include MeiliSearch::Rails
671
683
 
672
684
  meilisearch auto_index: false, auto_remove: false do
673
685
  end
@@ -695,4 +707,4 @@ The provided features and the code base is inspired by [algoliasearch-rails](htt
695
707
 
696
708
  <hr>
697
709
 
698
- **MeiliSearch** provides and maintains many **SDKs and Integration tools** like this one. We want to provide everyone with an **amazing search experience for any kind of project**. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the [integration-guides](https://github.com/meilisearch/integration-guides) repository.
710
+ **Meilisearch** provides and maintains many **SDKs and Integration tools** like this one. We want to provide everyone with an **amazing search experience for any kind of project**. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the [integration-guides](https://github.com/meilisearch/integration-guides) repository.
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ Rake::RDocTask.new do |rdoc|
7
7
  version = File.exist?('VERSION') ? File.read('VERSION') : ''
8
8
 
9
9
  rdoc.rdoc_dir = 'rdoc'
10
- rdoc.title = "MeiliSearch Rails #{version}"
10
+ rdoc.title = "Meilisearch Rails #{version}"
11
11
  rdoc.rdoc_files.include('README*')
12
12
  rdoc.rdoc_files.include('lib/**/*.rb')
13
13
  end
@@ -0,0 +1,23 @@
1
+ module MeiliSearch
2
+ module Rails
3
+ module Configuration
4
+ def configuration
5
+ raise NotConfigured if @_config.blank?
6
+
7
+ @_config
8
+ end
9
+
10
+ def configuration=(configuration)
11
+ @_config = configuration
12
+ end
13
+
14
+ def client
15
+ ::MeiliSearch::Client.new(
16
+ configuration[:meilisearch_host] || 'http://localhost:7700',
17
+ configuration[:meilisearch_api_key],
18
+ configuration.slice(:timeout, :max_retries)
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ module MeiliSearch
2
+ module Rails
3
+ class NoBlockGiven < StandardError; end
4
+
5
+ class BadConfiguration < StandardError; end
6
+
7
+ class NotConfigured < StandardError
8
+ def message
9
+ 'Please configure Meilisearch. Set MeiliSearch::Rails.configuration = ' \
10
+ "{meilisearch_host: 'YOUR_MEILISEARCH_HOST', meilisearch_api_key: 'YOUR_API_KEY'}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module MeiliSearch
2
+ module Rails
3
+ class MSJob < ::ActiveJob::Base
4
+ queue_as :meilisearch
5
+
6
+ def perform(record, method)
7
+ record.send(method)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,48 @@
1
+ unless defined? Kaminari
2
+ raise(MeiliSearch::BadConfiguration,
3
+ "Meilisearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
4
+ end
5
+
6
+ require 'kaminari/models/array_extension'
7
+
8
+ module MeiliSearch
9
+ module Rails
10
+ module Pagination
11
+ class Kaminari < ::Kaminari::PaginatableArray
12
+ def initialize(array, options)
13
+ if RUBY_VERSION >= '3'
14
+ super(array, **options)
15
+ else
16
+ super(array, options)
17
+ end
18
+ end
19
+
20
+ def limit(_num)
21
+ # noop
22
+ self
23
+ end
24
+
25
+ def offset(_num)
26
+ # noop
27
+ self
28
+ end
29
+
30
+ class << self
31
+ def create(results, total_hits, options = {})
32
+ offset = ((options[:page] - 1) * options[:per_page])
33
+ array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
34
+
35
+ if array.empty? && !results.empty?
36
+ # since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
37
+ # otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
38
+ results = ([nil] * offset) + results
39
+ array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
40
+ end
41
+
42
+ array
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ begin
2
+ require 'will_paginate/collection'
3
+ rescue LoadError
4
+ raise(MeiliSearch::BadConfiguration,
5
+ "MeiliSearch: Please add 'will_paginate' to your Gemfile to use will_paginate pagination backend")
6
+ end
7
+
8
+ module MeiliSearch
9
+ module Rails
10
+ module Pagination
11
+ class WillPaginate
12
+ def self.create(results, total_hits, options = {})
13
+ ::WillPaginate::Collection.create(options[:page], options[:per_page], total_hits) do |pager|
14
+ start = (options[:page] - 1) * options[:per_page]
15
+ paginated_results = results[start, options[:per_page]]
16
+ pager.replace paginated_results
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ module MeiliSearch
2
+ module Rails
3
+ module Pagination
4
+ autoload :WillPaginate, 'meilisearch/rails/pagination/will_paginate'
5
+ autoload :Kaminari, 'meilisearch/rails/pagination/kaminari'
6
+
7
+ def self.create(results, total_hits, options = {})
8
+ return results if MeiliSearch::Rails.configuration[:pagination_backend].nil?
9
+
10
+ begin
11
+ backend = MeiliSearch::Rails.configuration[:pagination_backend].to_s.classify
12
+
13
+ ::MeiliSearch::Rails::Pagination.const_get(backend).create(results, total_hits, options)
14
+ rescue NameError
15
+ raise(BadConfiguration, 'Unknown pagination backend')
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+
3
+ module MeiliSearch
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ rake_tasks do
7
+ load 'meilisearch/rails/tasks/meilisearch.rake'
8
+ end
9
+ end
10
+
11
+ class Engine < ::Rails::Engine
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ namespace :meilisearch do
2
+ desc 'Reindex all models'
3
+ task reindex: :environment do
4
+ puts 'Reindexing all Meilisearch models'
5
+
6
+ MeiliSearch::Rails::Utilities.reindex_all_models
7
+ end
8
+
9
+ desc 'Set settings to all indexes'
10
+ task set_all_settings: :environment do
11
+ puts 'Set settings in all Meilisearch models'
12
+
13
+ MeiliSearch::Rails::Utilities.set_settings_all_models
14
+ end
15
+
16
+ desc 'Clear all indexes'
17
+ task clear_indexes: :environment do
18
+ puts 'Clearing indexes from all Meilisearch models'
19
+
20
+ MeiliSearch::Rails::Utilities.clear_all_indexes
21
+ end
22
+ end