meilisearch-rails 0.2.3 → 0.5.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.
@@ -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
@@ -0,0 +1,46 @@
1
+ module MeiliSearch
2
+ module Rails
3
+ module Utilities
4
+ class << self
5
+ def get_model_classes
6
+ if ::Rails.application && defined?(::Rails.autoloaders) && ::Rails.autoloaders.zeitwerk_enabled?
7
+ Zeitwerk::Loader.eager_load_all
8
+ elsif ::Rails.application
9
+ ::Rails.application.eager_load!
10
+ end
11
+ klasses = MeiliSearch::Rails.instance_variable_get(:@included_in)
12
+ (klasses + klasses.map(&:descendants).flatten).uniq
13
+ end
14
+
15
+ def clear_all_indexes
16
+ get_model_classes.each(&:clear_index!)
17
+ end
18
+
19
+ def reindex_all_models
20
+ klasses = get_model_classes
21
+
22
+ ::Rails.logger.info "\n\nReindexing #{klasses.count} models: #{klasses.to_sentence}.\n"
23
+
24
+ klasses.each do |klass|
25
+ ::Rails.logger.info klass
26
+ ::Rails.logger.info "Reindexing #{klass.count} records..."
27
+
28
+ klass.ms_reindex!
29
+ end
30
+ end
31
+
32
+ def set_settings_all_models
33
+ klasses = get_model_classes
34
+
35
+ ::Rails.logger.info "\n\nPushing settings for #{klasses.count} models: #{klasses.to_sentence}.\n"
36
+
37
+ klasses.each do |klass|
38
+ ::Rails.logger.info "Pushing #{klass} settings..."
39
+
40
+ klass.ms_set_settings
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MeiliSearch
4
+ module Rails
5
+ VERSION = '0.5.0'
6
+ end
7
+ end