typesense-rails 1.0.0.rc1

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,29 @@
1
+ unless defined? Pagy
2
+ raise(Typesense::BadConfiguration, "Typesense: Please add 'pagy' to your Gemfile to use Pagy pagination backend")
3
+ end
4
+
5
+ module Typesense
6
+ module Pagination
7
+ class Pagy
8
+
9
+ def self.create(results, total_hits, options = {})
10
+ vars = {
11
+ count: total_hits,
12
+ page: options[:page],
13
+ items: options[:per_page]
14
+ }
15
+
16
+ pagy_version = Gem::Version.new(::Pagy::VERSION)
17
+ pagy = if pagy_version >= Gem::Version.new('9.0')
18
+ ::Pagy.new(**vars)
19
+ else
20
+ ::Pagy.new(vars)
21
+ end
22
+
23
+ [pagy, results]
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'will_paginate/collection'
3
+ rescue LoadError
4
+ raise(Typesense::BadConfiguration, "Typesense: Please add 'will_paginate' to your Gemfile to use will_paginate pagination backend")
5
+ end
6
+
7
+ module Typesense
8
+ module Pagination
9
+ class WillPaginate
10
+ def self.create(results, total_hits, options = {})
11
+ ::WillPaginate::Collection.create(options[:page], options[:per_page], total_hits) do |pager|
12
+ pager.replace results
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module Typesense
2
+ module Pagination
3
+
4
+ autoload :WillPaginate, 'typesense/pagination/will_paginate'
5
+ autoload :Kaminari, 'typesense/pagination/kaminari'
6
+ autoload :Pagy, 'typesense/pagination/pagy'
7
+
8
+ def self.create(results, total_hits, options = {})
9
+ return results if Typesense.configuration[:pagination_backend].nil?
10
+ begin
11
+ backend = Typesense.configuration[:pagination_backend].to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } # classify pagination backend name
12
+ page = Object.const_get(:Typesense).const_get(:Pagination).const_get(backend).create(results, total_hits, options)
13
+ page
14
+ rescue NameError
15
+ raise(BadConfiguration, "Unknown pagination backend")
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+
3
+ module Typesense
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load "typesense/tasks/typesense.rake"
7
+ end
8
+ end
9
+
10
+ class Engine < Rails::Engine
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ namespace :typesense do
2
+ desc "Reindex all models"
3
+ task :reindex => :environment do
4
+ Typesense::Utilities.reindex_all_models
5
+ end
6
+
7
+ desc "Set settings to all indexes"
8
+ task :set_all_settings => :environment do
9
+ Typesense::Utilities.set_settings_all_models
10
+ end
11
+
12
+ desc "Clear all indexes"
13
+ task :clear_indexes => :environment do
14
+ puts "clearing all indexes"
15
+ Typesense::Utilities.clear_all_indexes
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module Typesense
2
+ class TypesenseJob < ::ActiveJob::Base
3
+ queue_as { Typesense.configuration[:queue_name] }
4
+
5
+ def perform(record, method)
6
+ record.send(method)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ module Typesense
2
+ module Utilities
3
+ class << self
4
+ def get_model_classes
5
+ if Rails.application && defined?(Rails.autoloaders) && Rails.autoloaders.zeitwerk_enabled?
6
+ Zeitwerk::Loader.eager_load_all
7
+ elsif Rails.application
8
+ Rails.application.eager_load!
9
+ end
10
+ Typesense.instance_variable_get :@included_in
11
+ end
12
+
13
+ def clear_all_indexes
14
+ get_model_classes.each do |klass|
15
+ klass.clear_index!
16
+ end
17
+ end
18
+
19
+ def reindex_all_models
20
+ klasses = get_model_classes
21
+
22
+ puts ""
23
+ puts "Reindexing #{klasses.count} models: #{klasses.to_sentence}."
24
+ puts ""
25
+
26
+ klasses.each do |klass|
27
+ puts klass
28
+ puts "Reindexing #{klass.count} records..."
29
+ klass.typesense_reindex
30
+ end
31
+ end
32
+
33
+ def set_settings_all_models
34
+ klasses = get_model_classes
35
+
36
+ puts ""
37
+ puts "Pushing settings for #{klasses.count} models: #{klasses.to_sentence}."
38
+ puts ""
39
+
40
+ klasses.each do |klass|
41
+ puts "Pushing #{klass} settings..."
42
+ klass.typesense_set_settings
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Typesense
2
+ GEM_VERSION = "1.0.0.rc1"
3
+ end