meilisearch-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/Gemfile +30 -0
- data/LICENSE +21 -0
- data/README.md +668 -0
- data/Rakefile +17 -0
- data/lib/meilisearch-rails.rb +921 -0
- data/lib/meilisearch/configuration.rb +15 -0
- data/lib/meilisearch/ms_job.rb +9 -0
- data/lib/meilisearch/pagination.rb +19 -0
- data/lib/meilisearch/pagination/kaminari.rb +40 -0
- data/lib/meilisearch/pagination/will_paginate.rb +19 -0
- data/lib/meilisearch/railtie.rb +11 -0
- data/lib/meilisearch/tasks/meilisearch.rake +19 -0
- data/lib/meilisearch/utilities.rb +48 -0
- data/lib/meilisearch/version.rb +3 -0
- data/meilisearch-rails.gemspec +46 -0
- data/spec/spec_helper.rb +52 -0
- data/spec/utilities_spec.rb +30 -0
- metadata +90 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
module MeiliSearch
|
2
|
+
module Configuration
|
3
|
+
def configuration
|
4
|
+
@@configuration || raise(NotConfigured, "Please configure MeiliSearch. Set MeiliSearch.configuration = {meilisearch_host: 'YOUR_MEILISEARCH_HOST', meilisearch_api_key: 'YOUR_API_KEY'}")
|
5
|
+
end
|
6
|
+
|
7
|
+
def configuration=(configuration)
|
8
|
+
@@configuration = configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
def client
|
12
|
+
::MeiliSearch::Client.new(@@configuration[:meilisearch_host], @@configuration[:meilisearch_api_key])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module MeiliSearch
|
2
|
+
module Pagination
|
3
|
+
|
4
|
+
autoload :WillPaginate, 'meilisearch/pagination/will_paginate'
|
5
|
+
autoload :Kaminari, 'meilisearch/pagination/kaminari'
|
6
|
+
|
7
|
+
def self.create(results, total_hits, options = {})
|
8
|
+
return results if MeiliSearch.configuration[:pagination_backend].nil?
|
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
|
13
|
+
rescue NameError
|
14
|
+
raise(BadConfiguration, "Unknown pagination backend")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
unless defined? Kaminari
|
2
|
+
raise(MeiliSearch::BadConfiguration, "MeiliSearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
|
3
|
+
end
|
4
|
+
|
5
|
+
require "kaminari/models/array_extension"
|
6
|
+
|
7
|
+
module MeiliSearch
|
8
|
+
module Pagination
|
9
|
+
class Kaminari < ::Kaminari::PaginatableArray
|
10
|
+
|
11
|
+
def initialize(array, options)
|
12
|
+
super(array, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def limit(num)
|
16
|
+
# noop
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def offset(num)
|
21
|
+
# noop
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def create(results, total_hits, options = {})
|
27
|
+
offset = ((options[:page] - 1) * options[:per_page])
|
28
|
+
array = new results, :limit => options[:per_page], :offset => offset, :total_count => total_hits
|
29
|
+
if array.empty? and !results.empty?
|
30
|
+
# since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
|
31
|
+
# otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
|
32
|
+
results = ([nil] * offset) + results
|
33
|
+
array = new results, :offset => offset, :limit => options[:per_page], :total_count => total_hits
|
34
|
+
end
|
35
|
+
array
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
begin
|
2
|
+
require 'will_paginate/collection'
|
3
|
+
rescue LoadError
|
4
|
+
raise(MeiliSearch::BadConfiguration, "MeiliSearch: Please add 'will_paginate' to your Gemfile to use will_paginate pagination backend")
|
5
|
+
end
|
6
|
+
|
7
|
+
module MeiliSearch
|
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
|
+
start = (options[:page] - 1) * options[:per_page]
|
13
|
+
paginated_results = results[start, options[:per_page]]
|
14
|
+
pager.replace paginated_results
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
namespace :meilisearch do
|
2
|
+
|
3
|
+
desc "Reindex all models"
|
4
|
+
task :reindex => :environment do
|
5
|
+
MeiliSearch::Utilities.reindex_all_models
|
6
|
+
end
|
7
|
+
|
8
|
+
desc "Set settings to all indexes"
|
9
|
+
task :set_all_settings => :environment do
|
10
|
+
MeiliSearch::Utilities.set_settings_all_models
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Clear all indexes"
|
14
|
+
task :clear_indexes => :environment do
|
15
|
+
puts "clearing all indexes"
|
16
|
+
MeiliSearch::Utilities.clear_all_indexes
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module MeiliSearch
|
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
|
+
MeiliSearch.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.ms_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.ms_set_settings
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), 'lib', 'meilisearch', 'version')
|
4
|
+
|
5
|
+
require 'date'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "meilisearch-rails"
|
9
|
+
s.version = MeiliSearch::VERSION
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.authors = ["Meili"]
|
13
|
+
s.date = Date.today
|
14
|
+
s.description = "MeiliSearch integration for Ruby on Rails. See https://github.com/meilisearch/MeiliSearch"
|
15
|
+
s.email = "bonjour@meilisearch.com"
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE",
|
18
|
+
"README.md"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"meilisearch-rails.gemspec",
|
27
|
+
"lib/meilisearch-rails.rb",
|
28
|
+
"lib/meilisearch/ms_job.rb",
|
29
|
+
"lib/meilisearch/configuration.rb",
|
30
|
+
"lib/meilisearch/pagination.rb",
|
31
|
+
"lib/meilisearch/pagination/kaminari.rb",
|
32
|
+
"lib/meilisearch/pagination/will_paginate.rb",
|
33
|
+
"lib/meilisearch/railtie.rb",
|
34
|
+
"lib/meilisearch/tasks/meilisearch.rake",
|
35
|
+
"lib/meilisearch/utilities.rb",
|
36
|
+
"lib/meilisearch/version.rb",
|
37
|
+
"spec/spec_helper.rb",
|
38
|
+
"spec/utilities_spec.rb"
|
39
|
+
]
|
40
|
+
s.homepage = "http://github.com/meilisearch/meilisearch-rails"
|
41
|
+
s.licenses = ["MIT"]
|
42
|
+
s.require_paths = ["lib"]
|
43
|
+
s.summary = "MeiliSearch integration for Ruby on Rails."
|
44
|
+
s.add_dependency(%q<json>, [">= 1.5.1"])
|
45
|
+
s.add_dependency(%q<meilisearch>, [">= 0.15.3"])
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require 'timeout'
|
4
|
+
require 'dotenv/load'
|
5
|
+
require 'faker'
|
6
|
+
Bundler.setup :test
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
10
|
+
|
11
|
+
require 'meilisearch-rails'
|
12
|
+
require 'rspec'
|
13
|
+
require 'rails/all'
|
14
|
+
|
15
|
+
raise "missing MEILISEARCH_HOST or MEILISEARCH_API_KEY environment variables" if ENV['MEILISEARCH_HOST'].nil? || ENV['MEILISEARCH_API_KEY'].nil?
|
16
|
+
|
17
|
+
Thread.current[:meilisearch_hosts] = nil
|
18
|
+
|
19
|
+
RSpec.configure do |c|
|
20
|
+
c.mock_with :rspec
|
21
|
+
c.filter_run :focus => true
|
22
|
+
c.run_all_when_everything_filtered = true
|
23
|
+
c.formatter = 'documentation'
|
24
|
+
|
25
|
+
c.around(:each) do |example|
|
26
|
+
Timeout::timeout(120) {
|
27
|
+
example.run
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
# Remove all indexes setup in this run in local or CI
|
32
|
+
c.after(:suite) do
|
33
|
+
safe_index_list.each do |index|
|
34
|
+
MeiliSearch.client.delete_index(index['name'])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# A unique prefix for your test run in local or CI
|
40
|
+
SAFE_INDEX_PREFIX = "rails_#{SecureRandom.hex(8)}".freeze
|
41
|
+
|
42
|
+
# avoid concurrent access to the same index in local or CI
|
43
|
+
def safe_index_uid(name)
|
44
|
+
"#{SAFE_INDEX_PREFIX}_#{name}"
|
45
|
+
end
|
46
|
+
|
47
|
+
# get a list of safe indexes in local or CI
|
48
|
+
def safe_index_list
|
49
|
+
list = MeiliSearch.client.indexes()
|
50
|
+
list = list.select { |index| index["name"].include?(SAFE_INDEX_PREFIX) }
|
51
|
+
list.sort_by { |index| index["primary"] || "" }
|
52
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
MeiliSearch.configuration = { :meilisearch_host => ENV['MEILISEARCH_HOST'], :meilisearch_api_key => ENV['MEILISEARCH_API_KEY'] }
|
4
|
+
|
5
|
+
describe MeiliSearch::Utilities do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@included_in = MeiliSearch.instance_variable_get :@included_in
|
9
|
+
MeiliSearch.instance_variable_set :@included_in, []
|
10
|
+
|
11
|
+
class Dummy
|
12
|
+
include MeiliSearch
|
13
|
+
|
14
|
+
def self.model_name
|
15
|
+
"Dummy"
|
16
|
+
end
|
17
|
+
|
18
|
+
meilisearch
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
after(:each) do
|
23
|
+
MeiliSearch.instance_variable_set :@included_in, @included_in
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should get the models where MeiliSearch module was included" do
|
27
|
+
(MeiliSearch::Utilities.get_model_classes - [Dummy]).should == []
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meilisearch-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Meili
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.5.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.5.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: meilisearch
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.15.3
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.15.3
|
41
|
+
description: MeiliSearch integration for Ruby on Rails. See https://github.com/meilisearch/MeiliSearch
|
42
|
+
email: bonjour@meilisearch.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files:
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
48
|
+
files:
|
49
|
+
- ".rspec"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/meilisearch-rails.rb
|
55
|
+
- lib/meilisearch/configuration.rb
|
56
|
+
- lib/meilisearch/ms_job.rb
|
57
|
+
- lib/meilisearch/pagination.rb
|
58
|
+
- lib/meilisearch/pagination/kaminari.rb
|
59
|
+
- lib/meilisearch/pagination/will_paginate.rb
|
60
|
+
- lib/meilisearch/railtie.rb
|
61
|
+
- lib/meilisearch/tasks/meilisearch.rake
|
62
|
+
- lib/meilisearch/utilities.rb
|
63
|
+
- lib/meilisearch/version.rb
|
64
|
+
- meilisearch-rails.gemspec
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
- spec/utilities_spec.rb
|
67
|
+
homepage: http://github.com/meilisearch/meilisearch-rails
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubygems_version: 3.0.3.1
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: MeiliSearch integration for Ruby on Rails.
|
90
|
+
test_files: []
|