meilisearch-rails 0.2.3 → 0.3.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: dff716ea4a13c0f64718942f4988550a36076ae5dc52ac8ec7282000d116a99d
4
- data.tar.gz: 1346eb18ddb24bf718693b7b3efa89d4b0084ecffe9f7ccae1d71956b3b8bf56
3
+ metadata.gz: 9e05e5ed1e5210587bd2797412b0f5509b77151a620b79e4e8047c6c1722c3fb
4
+ data.tar.gz: cfb5a6a39cf21caa0532ae40d005a8c7fd3fff30dbbfa02c90dd1f31f63259df
5
5
  SHA512:
6
- metadata.gz: 38e1657c75e4eec945502ead72001ba56dc194d3ce5217e65f40abc49de34312087aa30d401d342a15c4c0701f0d41d49faf8c94aef5cc40d79ed9bdc345f414
7
- data.tar.gz: 2cd31c3b6b70fb4d2c38977870ab28256de22422c410280e7615e11dfe64dd8eaa98295c7bdf553655b427837b8864ee60930a0aafa39e8f1272af34210c0479
6
+ metadata.gz: 7a3e738a240fcbdc903d3a58acc9d5d68992a5e1adb56401fcd30a03e81c1fddbcc2024bed1338934de84d366ef58cc9e069243ad6ab5ca78c1330414e4a90b9
7
+ data.tar.gz: 88228f931aafcbdd69fa483594c3842aaf7603dac277333107dc8629656e96f2fb0b0b57d84039b81b922f95cc9ef6322fb5745a4626058f135357779e227e0c
data/Gemfile CHANGED
@@ -1,31 +1,38 @@
1
- source "http://rubygems.org"
1
+ source 'http://rubygems.org'
2
2
 
3
3
  gem 'json', '~> 2.5', '>= 2.5.1'
4
- gem 'meilisearch', '~> 0.16.0'
4
+ gem 'meilisearch', '~> 0.17.0'
5
5
 
6
- if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
7
- gem 'rubysl', '~> 2.0', :platform => :rbx
6
+ gem 'rubysl', '~> 2.0', platform: :rbx if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
7
+
8
+ group :development do
9
+ gem 'rubocop', '~> 1.22'
10
+ gem 'rubocop-rails'
11
+ gem 'rubocop-rspec'
8
12
  end
9
13
 
10
14
  group :test do
11
- rails_version = ENV["RAILS_VERSION"] || '5.2'
12
- gem 'rails', "~> #{rails_version}"
15
+ rails_version = ENV['RAILS_VERSION'] || '5.2'
16
+ sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
17
+
13
18
  gem 'active_model_serializers'
19
+ gem 'rails', "~> #{rails_version}"
20
+ gem 'sequel', sequel_version
21
+
14
22
  if Gem::Version.new(rails_version) >= Gem::Version.new('6.0')
15
- gem 'sqlite3', '~> 1.4.0', :platform => [:rbx, :ruby]
23
+ gem 'sqlite3', '~> 1.4.0', platform: %i[rbx ruby]
16
24
  else
17
- gem 'sqlite3', '< 1.4.0', :platform => [:rbx, :ruby]
25
+ gem 'sqlite3', '< 1.4.0', platform: %i[rbx ruby]
18
26
  end
27
+
28
+ gem 'activerecord-jdbc-adapter', platform: :jruby
29
+ gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
30
+ gem 'jdbc-sqlite3', platform: :jruby
19
31
  gem 'rspec', '>= 2.5.0', '< 3.0'
20
- gem 'jdbc-sqlite3', :platform => :jruby
21
- gem 'activerecord-jdbc-adapter', :platform => :jruby
22
- gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
23
32
 
24
- sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
25
- gem 'sequel', sequel_version
33
+ gem 'byebug'
34
+ gem 'dotenv', '~> 2.7', '>= 2.7.6'
26
35
  gem 'faker', '~> 2.17'
27
- gem 'will_paginate', '>= 2.3.15'
28
36
  gem 'kaminari'
29
- gem 'dotenv', '~> 2.7', '>= 2.7.6'
30
- gem 'byebug'
37
+ gem 'will_paginate', '>= 2.3.15'
31
38
  end
data/README.md CHANGED
@@ -29,33 +29,26 @@
29
29
 
30
30
  - [📖 Documentation](#-documentation)
31
31
  - [🤖 Compatibility with MeiliSearch](#-compatibility-with-meilisearch)
32
- - [🔧 Installation](#-installation)
33
- - [🔩 Settings](#-settings)
32
+ - [🚀 Getting Started](#-getting-started)
33
+ - [⚙️ Settings](#️-settings)
34
34
  - [🔍 Custom search](#-custom-search)
35
35
  - [🪛 Options](#-options)
36
- - [MeiliSearch configuration & environment](#meilisearch-configuration-&-environment)
37
- - [Custom index_uid](#custom-index_uid)
38
- - [Per-environment index_uid](#per-environment-index_uid)
36
+ - [MeiliSearch configuration & environment](#meilisearch-configuration--environment)
39
37
  - [Index configuration](#index-configuration)
40
38
  - [Custom attribute definition](#custom-attribute-definition)
41
39
  - [Custom primary key](#custom-primary-key)
42
40
  - [Conditional indexing](#conditional-indexing)
43
- - [Target multiple indexes](#target-multiple-indexes)
44
41
  - [Share a single index](#share-a-single-index)
45
- - [Queues & background jobs](#queues-&-background-jobs)
42
+ - [Queues & background jobs](#queues--background-jobs)
46
43
  - [Relations](#relations)
47
44
  - [Sanitize attributes](#sanitize-attributes)
48
45
  - [UTF-8 encoding](#utf-8-encoding)
49
46
  - [Manual operations](#manual-operations)
50
- - [Indexing & deletion](#indexing-&-deletion)
47
+ - [Indexing & deletion](#indexing--deletion)
51
48
  - [Access the underlying index object](#access-the-underlying-index-object)
52
- - [Development & testing](#development-&-testing)
53
- - [Exceptions](#exceptions)
54
- - [Testing](#testing)
55
- - [Synchronous testing](#synchronous-testing)
56
- - [Disable auto-indexing & auto-removal](#disable-auto-indexing-&-auto-removal)
57
- - [⚙️ Development Workflow and Contributing](#️-development-workflow-and-contributing)
58
- - [👏 Credits](#-credits)
49
+ - [Development & testing](#development--testing)
50
+ - [⚙️ Development workflow & contributing](#️-development-workflow--contributing)
51
+ - [👏 Credits](#--credits)
59
52
 
60
53
  ## 📖 Documentation
61
54
 
@@ -65,9 +58,9 @@ To learn more about MeiliSearch, check out our [Documentation](https://docs.meil
65
58
 
66
59
  ## 🤖 Compatibility with MeiliSearch
67
60
 
68
- This package only guarantees the compatibility with the [version v0.21.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.21.0).
61
+ This package only guarantees the compatibility with the [version v0.24.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.24.0).
69
62
 
70
- ## 🔧 Installation
63
+ ## 🔧 Installation <!-- omit in toc -->
71
64
 
72
65
  This package requires Ruby version 2.6.0 or later and Rails 5.2 or later.
73
66
 
@@ -104,7 +97,7 @@ Create a new file `config/initializers/meilisearch.rb` to setup your `MEILISEARC
104
97
 
105
98
  ```ruby
106
99
  MeiliSearch.configuration = {
107
- meilisearch_host: 'YourMeiliSearchHost',
100
+ meilisearch_host: 'YourMeiliSearchHost', # example: http://localhost:7700
108
101
  meilisearch_api_key: 'YourMeiliSearchAPIKey',
109
102
  }
110
103
  ```
@@ -213,8 +206,9 @@ class Book < ApplicationRecord
213
206
  'typo',
214
207
  'words',
215
208
  'attribute',
209
+ 'sort',
216
210
  'exactness',
217
- 'desc(publication_year)'
211
+ 'publication_year:desc'
218
212
  ]
219
213
  synonyms nyc: ['new york']
220
214
 
@@ -242,7 +236,7 @@ Book.search('Harry', attributesToHighlight: ['*'])
242
236
 
243
237
  ### MeiliSearch configuration & environment
244
238
 
245
- #### Custom index_uid
239
+ #### Custom index_uid <!-- omit in toc -->
246
240
 
247
241
  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.
248
242
 
@@ -255,7 +249,7 @@ class Book < ActiveRecord::Base
255
249
  end
256
250
  ```
257
251
 
258
- #### Index UID according to the environment
252
+ #### Index UID according to the environment <!-- omit in toc -->
259
253
 
260
254
  You can suffix the index UID with the current Rails environment using the following option:
261
255
 
@@ -341,7 +335,7 @@ class Book < ActiveRecord::Base
341
335
  end
342
336
  end
343
337
  ```
344
- ##### Target multiple indexes
338
+ ##### Target multiple indexes <!-- omit in toc -->
345
339
 
346
340
  You can index a record in several indexes using the `add_index` option:
347
341
 
@@ -637,7 +631,7 @@ index = Book.index
637
631
 
638
632
  ### Development & testing
639
633
 
640
- #### Exceptions
634
+ #### Exceptions <!-- omit in toc -->
641
635
 
642
636
  You can disable exceptions that could be raised while trying to reach MeiliSearch's API by using the `raise_on_failure` option:
643
637
 
@@ -651,9 +645,9 @@ class Book < ActiveRecord::Base
651
645
  end
652
646
  ```
653
647
 
654
- #### Testing
648
+ #### Testing <!-- omit in toc -->
655
649
 
656
- ##### Synchronous testing
650
+ ##### Synchronous testing <!-- omit in toc -->
657
651
 
658
652
  You can force indexing and removing to be synchronous by setting the following option:
659
653
 
@@ -667,7 +661,7 @@ end
667
661
  ```
668
662
  🚨 This is only recommended for testing purposes, the gem will call the `wait_for_pending_update` method that will stop your code execution until the asynchronous task has been processed by MeilSearch.
669
663
 
670
- ##### Disable auto-indexing & auto-removal
664
+ ##### Disable auto-indexing & auto-removal <!-- omit in toc -->
671
665
 
672
666
  You can disable auto-indexing and auto-removing setting the following options:
673
667
 
data/Rakefile CHANGED
@@ -2,8 +2,9 @@ require 'rubygems'
2
2
  require 'rake'
3
3
 
4
4
  require 'rdoc/task'
5
+
5
6
  Rake::RDocTask.new do |rdoc|
6
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
7
+ version = File.exist?('VERSION') ? File.read('VERSION') : ''
7
8
 
8
9
  rdoc.rdoc_dir = 'rdoc'
9
10
  rdoc.title = "MeiliSearch Rails #{version}"
@@ -11,7 +12,7 @@ Rake::RDocTask.new do |rdoc|
11
12
  rdoc.rdoc_files.include('lib/**/*.rb')
12
13
  end
13
14
 
14
- require "rspec/core/rake_task"
15
+ require 'rspec/core/rake_task'
15
16
  RSpec::Core::RakeTask.new(:spec)
16
17
 
17
- task :default => :spec
18
+ task default: :spec
@@ -1,16 +1,18 @@
1
1
  module MeiliSearch
2
2
  module Configuration
3
3
  def configuration
4
- @@configuration || raise(NotConfigured, "Please configure MeiliSearch. Set MeiliSearch.configuration = {meilisearch_host: 'YOUR_MEILISEARCH_HOST', meilisearch_api_key: 'YOUR_API_KEY'}")
4
+ raise NotConfigured if @_config.blank?
5
+
6
+ @_config
5
7
  end
6
8
 
7
9
  def configuration=(configuration)
8
- @@configuration = configuration
10
+ @_config = configuration
9
11
  end
10
12
 
11
13
  def client
12
14
  ::MeiliSearch::Client.new(
13
- configuration[:meilisearch_host],
15
+ configuration[:meilisearch_host] || 'http://localhost:7700',
14
16
  configuration[:meilisearch_api_key],
15
17
  configuration.slice(:timeout, :max_retries)
16
18
  )
@@ -0,0 +1,12 @@
1
+ module MeiliSearch
2
+ class NoBlockGiven < StandardError; end
3
+
4
+ class BadConfiguration < StandardError; end
5
+
6
+ class NotConfigured < StandardError
7
+ def message
8
+ 'Please configure MeiliSearch. Set MeiliSearch.configuration = ' \
9
+ "{meilisearch_host: 'YOUR_MEILISEARCH_HOST', meilisearch_api_key: 'YOUR_API_KEY'}"
10
+ end
11
+ end
12
+ end
@@ -1,13 +1,13 @@
1
1
  unless defined? Kaminari
2
- raise(MeiliSearch::BadConfiguration, "MeiliSearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
2
+ raise(MeiliSearch::BadConfiguration,
3
+ "MeiliSearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
3
4
  end
4
5
 
5
- require "kaminari/models/array_extension"
6
+ require 'kaminari/models/array_extension'
6
7
 
7
8
  module MeiliSearch
8
9
  module Pagination
9
10
  class Kaminari < ::Kaminari::PaginatableArray
10
-
11
11
  def initialize(array, options)
12
12
  if RUBY_VERSION >= '3'
13
13
  super(array, **options)
@@ -16,12 +16,12 @@ module MeiliSearch
16
16
  end
17
17
  end
18
18
 
19
- def limit(num)
19
+ def limit(_num)
20
20
  # noop
21
21
  self
22
22
  end
23
23
 
24
- def offset(num)
24
+ def offset(_num)
25
25
  # noop
26
26
  self
27
27
  end
@@ -30,12 +30,14 @@ module MeiliSearch
30
30
  def create(results, total_hits, options = {})
31
31
  offset = ((options[:page] - 1) * options[:per_page])
32
32
  array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
33
- if array.empty? and !results.empty?
33
+
34
+ if array.empty? && !results.empty?
34
35
  # since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
35
36
  # otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
36
37
  results = ([nil] * offset) + results
37
38
  array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
38
39
  end
40
+
39
41
  array
40
42
  end
41
43
  end
@@ -1,14 +1,15 @@
1
1
  begin
2
2
  require 'will_paginate/collection'
3
3
  rescue LoadError
4
- raise(MeiliSearch::BadConfiguration, "MeiliSearch: Please add 'will_paginate' to your Gemfile to use will_paginate pagination backend")
4
+ raise(MeiliSearch::BadConfiguration,
5
+ "MeiliSearch: Please add 'will_paginate' to your Gemfile to use will_paginate pagination backend")
5
6
  end
6
7
 
7
8
  module MeiliSearch
8
9
  module Pagination
9
10
  class WillPaginate
10
11
  def self.create(results, total_hits, options = {})
11
- ::WillPaginate::Collection.create(options[:page], options[:per_page], total_hits) do |pager|
12
+ ::WillPaginate::Collection.create(options[:page], options[:per_page], total_hits) do |pager|
12
13
  start = (options[:page] - 1) * options[:per_page]
13
14
  paginated_results = results[start, options[:per_page]]
14
15
  pager.replace paginated_results
@@ -1,19 +1,18 @@
1
1
  module MeiliSearch
2
2
  module Pagination
3
-
4
3
  autoload :WillPaginate, 'meilisearch/pagination/will_paginate'
5
4
  autoload :Kaminari, 'meilisearch/pagination/kaminari'
6
5
 
7
6
  def self.create(results, total_hits, options = {})
8
7
  return results if MeiliSearch.configuration[:pagination_backend].nil?
8
+
9
9
  begin
10
- backend = MeiliSearch.configuration[:pagination_backend].to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } # classify pagination backend name
11
- page = Object.const_get(:MeiliSearch).const_get(:Pagination).const_get(backend).create(results, total_hits, options)
12
- page
10
+ backend = MeiliSearch.configuration[:pagination_backend].to_s.classify
11
+
12
+ ::MeiliSearch::Pagination.const_get(backend).create(results, total_hits, options)
13
13
  rescue NameError
14
- raise(BadConfiguration, "Unknown pagination backend")
14
+ raise(BadConfiguration, 'Unknown pagination backend')
15
15
  end
16
16
  end
17
-
18
17
  end
19
18
  end
@@ -3,9 +3,10 @@ require 'rails'
3
3
  module MeiliSearch
4
4
  class Railtie < Rails::Railtie
5
5
  rake_tasks do
6
- load "meilisearch/tasks/meilisearch.rake"
6
+ load 'meilisearch/tasks/meilisearch.rake'
7
7
  end
8
8
  end
9
+
9
10
  class Engine < Rails::Engine
10
11
  end
11
12
  end
@@ -1,19 +1,17 @@
1
1
  namespace :meilisearch do
2
-
3
- desc "Reindex all models"
4
- task :reindex => :environment do
2
+ desc 'Reindex all models'
3
+ task reindex: :environment do
5
4
  MeiliSearch::Utilities.reindex_all_models
6
5
  end
7
6
 
8
- desc "Set settings to all indexes"
9
- task :set_all_settings => :environment do
7
+ desc 'Set settings to all indexes'
8
+ task set_all_settings: :environment do
10
9
  MeiliSearch::Utilities.set_settings_all_models
11
10
  end
12
-
13
- desc "Clear all indexes"
14
- task :clear_indexes => :environment do
15
- puts "clearing all indexes"
11
+
12
+ desc 'Clear all indexes'
13
+ task clear_indexes: :environment do
14
+ puts 'clearing all indexes'
16
15
  MeiliSearch::Utilities.clear_all_indexes
17
16
  end
18
-
19
17
  end
@@ -8,25 +8,22 @@ module MeiliSearch
8
8
  Rails.application.eager_load!
9
9
  end
10
10
  klasses = MeiliSearch.instance_variable_get(:@included_in)
11
- (klasses + klasses.map{ |klass| klass.descendants }.flatten).uniq
11
+ (klasses + klasses.map(&:descendants).flatten).uniq
12
12
  end
13
13
 
14
14
  def clear_all_indexes
15
- get_model_classes.each do |klass|
16
- klass.clear_index!
17
- end
15
+ get_model_classes.each(&:clear_index!)
18
16
  end
19
17
 
20
18
  def reindex_all_models
21
19
  klasses = get_model_classes
22
20
 
23
- puts ''
24
- puts "Reindexing #{klasses.count} models: #{klasses.to_sentence}."
25
- puts ''
21
+ Rails.logger.info "\n\nReindexing #{klasses.count} models: #{klasses.to_sentence}.\n"
26
22
 
27
23
  klasses.each do |klass|
28
- puts klass
29
- puts "Reindexing #{klass.count} records..."
24
+ Rails.logger.info klass
25
+ Rails.logger.info "Reindexing #{klass.count} records..."
26
+
30
27
  klass.ms_reindex!
31
28
  end
32
29
  end
@@ -34,16 +31,14 @@ module MeiliSearch
34
31
  def set_settings_all_models
35
32
  klasses = get_model_classes
36
33
 
37
- puts ''
38
- puts "Pushing settings for #{klasses.count} models: #{klasses.to_sentence}."
39
- puts ''
34
+ Rails.logger.info "\n\nPushing settings for #{klasses.count} models: #{klasses.to_sentence}.\n"
40
35
 
41
36
  klasses.each do |klass|
42
- puts "Pushing #{klass} settings..."
37
+ Rails.logger.info "Pushing #{klass} settings..."
38
+
43
39
  klass.ms_set_settings
44
40
  end
45
41
  end
46
42
  end
47
43
  end
48
44
  end
49
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MeiliSearch
2
- VERSION = '0.2.3'
4
+ VERSION = '0.3.0'
3
5
  end