algoliasearch-rails 1.19.1 → 2.2.2

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.
data/LICENSE CHANGED
@@ -1,7 +1,6 @@
1
- The MIT License (MIT)
1
+ MIT License
2
2
 
3
- Copyright (c) 2013 Algolia
4
- http://www.algolia.com/
3
+ Copyright (c) 2013-Present Algolia
5
4
 
6
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
7
6
  of this software and associated documentation files (the "Software"), to deal
@@ -10,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
9
  copies of the Software, and to permit persons to whom the Software is
11
10
  furnished to do so, subject to the following conditions:
12
11
 
13
- The above copyright notice and this permission notice shall be included in
14
- all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
15
14
 
16
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
- THE SOFTWARE.
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,35 +1,50 @@
1
- # Algolia Search API Client for Rails
2
-
3
- [Algolia Search](https://www.algolia.com) is a hosted full-text, numerical, and faceted search engine capable of delivering realtime results from the first keystroke.
4
-
5
- [![Build Status](https://travis-ci.org/algolia/algoliasearch-rails.svg?branch=master)](https://travis-ci.org/algolia/algoliasearch-rails) [![Gem Version](https://badge.fury.io/rb/algoliasearch-rails.svg)](http://badge.fury.io/rb/algoliasearch-rails) [![Code Climate](https://codeclimate.com/github/algolia/algoliasearch-rails.svg)](https://codeclimate.com/github/algolia/algoliasearch-rails) ![ActiveRecord](https://img.shields.io/badge/ActiveRecord-yes-blue.svg?style=flat-square) ![Mongoid](https://img.shields.io/badge/Mongoid-yes-blue.svg?style=flat-square) ![Sequel](https://img.shields.io/badge/Sequel-yes-blue.svg?style=flat-square)
1
+ <p align="center">
2
+ <a href="https://www.algolia.com">
3
+ <img alt="Algolia for Rails" src="https://raw.githubusercontent.com/algolia/algoliasearch-client-common/master/banners/rails.png"/>
4
+ </a>
5
+ </p>
6
+
7
+ <h4 align="center">The perfect starting point to integrate <a href="https://algolia.com" target="_blank">Algolia</a> within your Rails project</h4>
8
+
9
+ <p align="center">
10
+ <a href="https://circleci.com/gh/algolia/algoliasearch-rails"><img src="https://circleci.com/gh/algolia/algoliasearch-rails.svg?style=shield" alt="CircleCI" /></a>
11
+ <a href="http://badge.fury.io/rb/algoliasearch-rails"><img src="https://badge.fury.io/rb/algoliasearch-rails.svg" alt="Gem Version"/></a>
12
+ <a href="https://codeclimate.com/github/algolia/algoliasearch-rails"><img src="https://codeclimate.com/github/algolia/algoliasearch-rails.svg" alt="Code Climate"/></a>
13
+ <img src="https://img.shields.io/badge/ActiveRecord-yes-blue.svg?style=flat-square" alt="ActiveRecord"/>
14
+ <img src="https://img.shields.io/badge/Mongoid-yes-blue.svg?style=flat-square" alt="Mongoid"/>
15
+ <img src="https://img.shields.io/badge/Sequel-yes-blue.svg?style=flat-square" alt="Sequel"/>
16
+ </p>
17
+
18
+ <p align="center">
19
+ <a href="https://www.algolia.com/doc/framework-integration/rails/getting-started/setup/?language=ruby" target="_blank">Documentation</a> •
20
+ <a href="https://discourse.algolia.com" target="_blank">Community Forum</a> •
21
+ <a href="http://stackoverflow.com/questions/tagged/algolia" target="_blank">Stack Overflow</a> •
22
+ <a href="https://github.com/algolia/algoliasearch-rails/issues" target="_blank">Report a bug</a> •
23
+ <a href="https://www.algolia.com/doc/framework-integration/rails/troubleshooting/faq/" target="_blank">FAQ</a> •
24
+ <a href="https://www.algolia.com/support" target="_blank">Support</a>
25
+ </p>
6
26
 
7
27
 
8
28
  This gem let you easily integrate the Algolia Search API to your favorite ORM. It's based on the [algoliasearch-client-ruby](https://github.com/algolia/algoliasearch-client-ruby) gem.
9
- Rails 3.x, 4.x and 5.x are all supported.
10
-
11
- You might be interested in the sample Ruby on Rails application providing a `autocomplete.js`-based auto-completion and `instantsearch.js`-based instant search results page: [algoliasearch-rails-example](https://github.com/algolia/algoliasearch-rails-example/).
29
+ Rails 5.x and 6.x are supported.
12
30
 
31
+ You might be interested in the sample Ruby on Rails application providing a `autocomplete.js`-based auto-completion and `InstantSearch.js`-based instant search results page: [algoliasearch-rails-example](https://github.com/algolia/algoliasearch-rails-example/).
13
32
 
14
33
 
15
34
 
16
35
  ## API Documentation
17
36
 
18
- You can find the full reference on [Algolia's website](https://www.algolia.com/doc/api-client/rails/).
37
+ You can find the full reference on [Algolia's website](https://www.algolia.com/doc/framework-integration/rails/).
19
38
 
20
39
 
21
- ## Table of Contents
22
-
23
40
 
24
41
  1. **[Setup](#setup)**
25
-
26
42
  * [Install](#install)
27
43
  * [Configuration](#configuration)
28
44
  * [Timeouts](#timeouts)
29
45
  * [Notes](#notes)
30
46
 
31
47
  1. **[Usage](#usage)**
32
-
33
48
  * [Index Schema](#index-schema)
34
49
  * [Relevancy](#relevancy)
35
50
  * [Indexing](#indexing)
@@ -43,13 +58,12 @@ You can find the full reference on [Algolia's website](https://www.algolia.com/d
43
58
  * [Geo-Search](#geo-search)
44
59
 
45
60
  1. **[Options](#options)**
46
-
47
61
  * [Auto-indexing &amp; asynchronism](#auto-indexing--asynchronism)
48
62
  * [Custom index name](#custom-index-name)
49
63
  * [Per-environment indices](#per-environment-indices)
50
64
  * [Custom attribute definition](#custom-attribute-definition)
51
65
  * [Nested objects/relations](#nested-objectsrelations)
52
- * [Custom `objectID`](#custom-objectid)
66
+ * [Custom <code>objectID</code>](#custom-objectid)
53
67
  * [Restrict indexing to a subset of your data](#restrict-indexing-to-a-subset-of-your-data)
54
68
  * [Sanitizer](#sanitizer)
55
69
  * [UTF-8 Encoding](#utf-8-encoding)
@@ -57,7 +71,6 @@ You can find the full reference on [Algolia's website](https://www.algolia.com/d
57
71
  * [Configuration example](#configuration-example)
58
72
 
59
73
  1. **[Indices](#indices)**
60
-
61
74
  * [Manual indexing](#manual-indexing)
62
75
  * [Manual removal](#manual-removal)
63
76
  * [Reindexing](#reindexing)
@@ -68,9 +81,10 @@ You can find the full reference on [Algolia's website](https://www.algolia.com/d
68
81
  * [Target multiple indices](#target-multiple-indices)
69
82
 
70
83
  1. **[Testing](#testing)**
71
-
72
84
  * [Notes](#notes)
73
85
 
86
+ 1. **[Troubleshooting](#troubleshooting)**
87
+ * [Frequently asked questions](#frequently-asked-questions)
74
88
 
75
89
 
76
90
 
@@ -113,7 +127,7 @@ You can configure a various timeout thresholds by setting the following options
113
127
  ```ruby
114
128
  AlgoliaSearch.configuration = {
115
129
  application_id: 'YourApplicationID',
116
- api_key: 'YourAPIKey'
130
+ api_key: 'YourAPIKey',
117
131
  connect_timeout: 2,
118
132
  receive_timeout: 30,
119
133
  send_timeout: 30,
@@ -124,7 +138,7 @@ AlgoliaSearch.configuration = {
124
138
 
125
139
  ## Notes
126
140
 
127
- This gem makes intensive use of Rails' callbacks to trigger the indexing tasks. If you're using methods bypassing `after_validation`, `before_save` or `after_commit` callbacks, it will not index your changes. For example: `update_attribute` doesn't perform validations checks, to perform validations when updating use `update_attributes`.
141
+ This gem makes extensive use of Rails' callbacks to trigger the indexing tasks. If you're using methods bypassing `after_validation`, `before_save` or `after_commit` callbacks, it will not index your changes. For example: `update_attribute` doesn't perform validations checks, to perform validations when updating use `update_attributes`.
128
142
 
129
143
  All methods injected by the `AlgoliaSearch` module are prefixed by `algolia_` and aliased to the associated short names if they aren't already defined.
130
144
 
@@ -135,6 +149,7 @@ Contact.algolia_search("jon doe") # <=> Contact.search("jon doe")
135
149
  ```
136
150
 
137
151
 
152
+
138
153
  # Usage
139
154
 
140
155
 
@@ -148,7 +163,7 @@ class Contact < ActiveRecord::Base
148
163
  include AlgoliaSearch
149
164
 
150
165
  algoliasearch do
151
- attribute :first_name, :last_name, :email
166
+ attributes :first_name, :last_name, :email
152
167
  end
153
168
  end
154
169
  ```
@@ -291,8 +306,8 @@ class Contact < ActiveRecord::Base
291
306
  attribute :first_name, :last_name, :email
292
307
 
293
308
  # default search parameters stored in the index settings
294
- minWordSizeForApprox1 4
295
- minWordSizeForApprox2 8
309
+ minWordSizefor1Typo 4
310
+ minWordSizefor2Typos 8
296
311
  hitsPerPage 42
297
312
  end
298
313
  end
@@ -300,7 +315,7 @@ end
300
315
 
301
316
  ```ruby
302
317
  # dynamical search parameters
303
- p Contact.raw_search("jon doe", { :hitsPerPage => 5, :page => 2 })
318
+ p Contact.raw_search('jon doe', { hitsPerPage: 5, page: 2 })
304
319
  ```
305
320
 
306
321
  ## Backend Pagination
@@ -375,7 +390,7 @@ end
375
390
  ```
376
391
 
377
392
  ```ruby
378
- hits = Contact.search("jon doe", { :facets => '*' })
393
+ hits = Contact.search('jon doe', { facets: '*' })
379
394
  p hits # ORM-compliant array of objects
380
395
  p hits.facets # extra method added to retrieve facets
381
396
  p hits.facets['company'] # facet values+count of facet 'company'
@@ -383,7 +398,7 @@ p hits.facets['zip_code'] # facet values+count of facet 'zip_code'
383
398
  ```
384
399
 
385
400
  ```ruby
386
- raw_json = Contact.raw_search("jon doe", { :facets => '*' })
401
+ raw_json = Contact.raw_search('jon doe', { facets: '*' })
387
402
  p raw_json['facets']
388
403
  ```
389
404
 
@@ -409,7 +424,7 @@ Product.search_for_facet_values('category', 'phone', {
409
424
  ## Group by
410
425
 
411
426
  More info on distinct for grouping can be found
412
- [here](https://www.algolia.com/doc/guides/search/distinct#distinct-for-grouping).
427
+ [here](https://www.algolia.com/doc/guides/managing-results/refine-results/grouping/).
413
428
 
414
429
  ```ruby
415
430
  class Contact < ActiveRecord::Base
@@ -442,13 +457,14 @@ end
442
457
  At query time, specify <code>{ aroundLatLng: "37.33, -121.89", aroundRadius: 50000 }</code> as search parameters to restrict the result set to 50KM around San Jose.
443
458
 
444
459
 
460
+
445
461
  # Options
446
462
 
447
463
 
448
464
 
449
465
  ## Auto-indexing & asynchronism
450
466
 
451
- Each time a record is saved; it will be - asynchronously - indexed. On the other hand, each time a record is destroyed, it will be - asynchronously - removed from the index. That means that a network call with the ADD/DELETE operation is sent **synchronously** to the Algolia API but then the engine will **asynchronously** process the operation (so if you do a search just after, the results may not reflect it yet).
467
+ Each time a record is saved, it will be *asynchronously* indexed. On the other hand, each time a record is destroyed, it will be - asynchronously - removed from the index. That means that a network call with the ADD/DELETE operation is sent **synchronously** to the Algolia API but then the engine will **asynchronously** process the operation (so if you do a search just after, the results may not reflect it yet).
452
468
 
453
469
  You can disable auto-indexing and auto-removing setting the following options:
454
470
 
@@ -501,7 +517,7 @@ class MySidekiqWorker
501
517
  if remove
502
518
  # the record has likely already been removed from your database so we cannot
503
519
  # use ActiveRecord#find to load it
504
- index = Algolia::Index.new("index_name")
520
+ index = AlgoliaSearch.client.init_index("index_name")
505
521
  index.delete_object(id)
506
522
  else
507
523
  # the record should be present
@@ -531,8 +547,16 @@ end
531
547
 
532
548
  class MySidekiqWorker
533
549
  def perform(id, remove)
534
- c = Contact.find(id)
535
- remove ? c.remove_from_index! : c.index!
550
+ if remove
551
+ # the record has likely already been removed from your database so we cannot
552
+ # use ActiveRecord#find to load it
553
+ index = AlgoliaSearch.client.init_index("index_name")
554
+ index.delete_object(id)
555
+ else
556
+ # the record should be present
557
+ c = Contact.find(id)
558
+ c.index!
559
+ end
536
560
  end
537
561
  end
538
562
  ```
@@ -645,6 +669,8 @@ end
645
669
 
646
670
  ## Nested objects/relations
647
671
 
672
+ ### Defining the relationship
673
+
648
674
  You can easily embed nested objects defining an extra attribute returning any JSON-compliant object (an array or a hash or a combination of both).
649
675
 
650
676
  ```ruby
@@ -670,6 +696,82 @@ class Profile < ActiveRecord::Base
670
696
  end
671
697
  ```
672
698
 
699
+ ### Propagating the change from a nested child
700
+
701
+ #### With ActiveRecord
702
+
703
+ With ActiveRecord, we'll be using `touch` and `after_touch` to achieve this.
704
+
705
+ ```ruby
706
+ # app/models/app.rb
707
+ class App < ApplicationRecord
708
+ include AlgoliaSearch
709
+
710
+ belongs_to :author, class_name: :User
711
+ after_touch :index!
712
+
713
+ algoliasearch do
714
+ attribute :title
715
+ attribute :author do
716
+ author.as_json
717
+ end
718
+ end
719
+ end
720
+
721
+ # app/models/user.rb
722
+ class User < ApplicationRecord
723
+ # If your association uses belongs_to
724
+ # - use `touch: true`
725
+ # - do not define an `after_save` hook
726
+ has_many :apps, foreign_key: :author_id
727
+
728
+ after_save { apps.each(&:touch) }
729
+ end
730
+ ```
731
+
732
+ #### With Sequel
733
+
734
+ With Sequel, you can use the `touch` plugin to propagate the changes:
735
+
736
+ ```ruby
737
+ # app/models/app.rb
738
+ class App < Sequel::Model
739
+ include AlgoliaSearch
740
+
741
+ many_to_one :author, class: :User
742
+
743
+ plugin :timestamps
744
+ plugin :touch
745
+
746
+ algoliasearch do
747
+ attribute :title
748
+ attribute :author do
749
+ author.to_hash
750
+ end
751
+ end
752
+ end
753
+
754
+ # app/models/user.rb
755
+ class User < Sequel::Model
756
+ one_to_many :apps, key: :author_id
757
+
758
+ plugin :timestamps
759
+ # Can't use the associations since it won't trigger the after_save
760
+ plugin :touch
761
+
762
+ # Define the associations that need to be touched here
763
+ # Less performant, but allows for the after_save hook to trigger
764
+ def touch_associations
765
+ apps.map(&:touch)
766
+ end
767
+
768
+ def touch
769
+ super
770
+ touch_associations
771
+ end
772
+ end
773
+ ```
774
+
673
775
  ## Custom `objectID`
674
776
 
675
777
  By default, the `objectID` is based on your record's `id`. You can change this behavior specifying the `:id` option (be sure to use a uniq field).
@@ -849,6 +951,7 @@ end
849
951
  ```
850
952
 
851
953
 
954
+
852
955
  # Indices
853
956
 
854
957
 
@@ -885,6 +988,8 @@ Contact.reindex
885
988
 
886
989
  **Notes**: if you're using an index-specific API key, ensure you're allowing both `<INDEX_NAME>` and `<INDEX_NAME>.tmp`.
887
990
 
991
+ **Warning:** You should not use such an atomic reindexing operation while scoping/filtering the model because this operation **replaces the entire index**, keeping the filtered objects only. ie: Don't do `MyModel.where(...).reindex` but do `MyModel.where(...).reindex!` (with the trailing `!`)!!!
992
+
888
993
  ### Regular reindexing
889
994
 
890
995
  To reindex all your objects in place (without temporary index and therefore without deleting removed objects), use the `reindex!` class method:
@@ -1028,13 +1133,14 @@ Book.search 'foo bar', index: 'Book_by_editor'
1028
1133
  ```
1029
1134
 
1030
1135
 
1136
+
1031
1137
  # Testing
1032
1138
 
1033
1139
 
1034
1140
 
1035
1141
  ## Notes
1036
1142
 
1037
- To run the specs, please set the <code>ALGOLIA_APPLICATION_ID</code> and <code>ALGOLIA_API_KEY</code> environment variables. Since the tests are creating and removing indexes, DO NOT use your production account.
1143
+ To run the specs, please set the <code>ALGOLIA_APPLICATION_ID</code> and <code>ALGOLIA_API_KEY</code> environment variables. Since the tests are creating and removing indices, DO NOT use your production account.
1038
1144
 
1039
1145
  You may want to disable all indexing (add, update & delete operations) API calls, you can set the `disable_indexing` option:
1040
1146
 
@@ -1042,40 +1148,24 @@ You may want to disable all indexing (add, update & delete operations) API calls
1042
1148
  class User < ActiveRecord::Base
1043
1149
  include AlgoliaSearch
1044
1150
 
1045
- algoliasearch :per_environment => true, :disable_indexing => Rails.env.test? do
1151
+ algoliasearch per_environment: true, disable_indexing: Rails.env.test? do
1046
1152
  end
1047
1153
  end
1048
1154
 
1049
1155
  class User < ActiveRecord::Base
1050
1156
  include AlgoliaSearch
1051
1157
 
1052
- algoliasearch :per_environment => true, :disable_indexing => Proc.new { Rails.env.test? || more_complex_condition } do
1158
+ algoliasearch per_environment: true, disable_indexing: Proc.new { Rails.env.test? || more_complex_condition } do
1053
1159
  end
1054
1160
  end
1055
1161
  ```
1056
1162
 
1057
- Or you may want to mock Algolia's API calls. We provide a [WebMock](https://github.com/bblimke/webmock) sample configuration that you can use including `algolia/webmock`:
1058
-
1059
- ```ruby
1060
- require 'algolia/webmock'
1061
1163
 
1062
- describe 'With a mocked client' do
1164
+ ## Troubleshooting
1063
1165
 
1064
- before(:each) do
1065
- WebMock.enable!
1066
- end
1067
-
1068
- it "shouldn't perform any API calls here" do
1069
- User.create(name: 'My Indexed User') # mocked, no API call performed
1070
- User.search('').should == {} # mocked, no API call performed
1071
- end
1072
-
1073
- after(:each) do
1074
- WebMock.disable!
1075
- end
1076
-
1077
- end
1078
- ```
1166
+ Encountering an issue? Before reaching out to support, we recommend heading to our [FAQ](https://www.algolia.com/doc/api-client/troubleshooting/faq/ruby/) where you will find answers for the most common issues and gotchas with the client.
1079
1167
 
1168
+ ## Use the Dockerfile
1080
1169
 
1170
+ If you want to contribute to this project without installing all its dependencies, you can use our Docker image. Please check our [dedicated guide](DOCKER_README.MD) to learn more.
1081
1171
 
@@ -1,10 +1,12 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
- VERSION = File.read(File.join(File.dirname(__FILE__), 'VERSION')).strip
3
+ require File.join(File.dirname(__FILE__), 'lib', 'algoliasearch', 'version')
4
+
5
+ require 'date'
4
6
 
5
7
  Gem::Specification.new do |s|
6
8
  s.name = "algoliasearch-rails"
7
- s.version = VERSION
9
+ s.version = AlgoliaSearch::VERSION
8
10
 
9
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
12
  s.authors = ["Algolia"]
@@ -12,21 +14,19 @@ Gem::Specification.new do |s|
12
14
  s.description = "AlgoliaSearch integration to your favorite ORM"
13
15
  s.email = "contact@algolia.com"
14
16
  s.extra_rdoc_files = [
15
- "ChangeLog",
17
+ "CHANGELOG.MD",
16
18
  "LICENSE",
17
19
  "README.md"
18
20
  ]
19
21
  s.files = [
20
22
  ".document",
21
23
  ".rspec",
22
- ".travis.yml",
23
- "ChangeLog",
24
+ "CHANGELOG.MD",
24
25
  "Gemfile",
25
26
  "Gemfile.lock",
26
27
  "LICENSE",
27
28
  "README.md",
28
29
  "Rakefile",
29
- "VERSION",
30
30
  "algoliasearch-rails.gemspec",
31
31
  "lib/algoliasearch-rails.rb",
32
32
  "lib/algoliasearch/algolia_job.rb",
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
37
37
  "lib/algoliasearch/railtie.rb",
38
38
  "lib/algoliasearch/tasks/algoliasearch.rake",
39
39
  "lib/algoliasearch/utilities.rb",
40
+ "lib/algoliasearch/version.rb",
40
41
  "spec/spec_helper.rb",
41
42
  "spec/utilities_spec.rb",
42
43
  "vendor/assets/javascripts/algolia/algoliasearch.angular.js",
@@ -77,19 +78,18 @@ Gem::Specification.new do |s|
77
78
 
78
79
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
79
80
  s.add_runtime_dependency(%q<json>, [">= 1.5.1"])
80
- s.add_runtime_dependency(%q<algoliasearch>, ["~> 1.12.4"])
81
+ s.add_runtime_dependency(%q<algolia>, ["< 3.0.0"])
81
82
  s.add_development_dependency(%q<will_paginate>, [">= 2.3.15"])
82
83
  s.add_development_dependency(%q<kaminari>, [">= 0"])
83
- s.add_development_dependency "travis"
84
84
  s.add_development_dependency "rake"
85
85
  s.add_development_dependency "rdoc"
86
86
  else
87
87
  s.add_dependency(%q<json>, [">= 1.5.1"])
88
- s.add_dependency(%q<algoliasearch>, ["~> 1.12.1"])
88
+ s.add_dependency(%q<algolia>, ["< 3.0.0"])
89
89
  end
90
90
  else
91
91
  s.add_dependency(%q<json>, [">= 1.5.1"])
92
- s.add_dependency(%q<algoliasearch>, ["~> 1.12.1"])
92
+ s.add_dependency(%q<algolia>, ["< 3.0.0"])
93
93
  end
94
94
  end
95
95
 
@@ -1,12 +1,38 @@
1
1
  module AlgoliaSearch
2
2
  module Configuration
3
+ def initialize
4
+ @client = nil
5
+ end
6
+
3
7
  def configuration
4
8
  @@configuration || raise(NotConfigured, "Please configure AlgoliaSearch. Set AlgoliaSearch.configuration = {application_id: 'YOUR_APPLICATION_ID', api_key: 'YOUR_API_KEY'}")
5
9
  end
6
10
 
7
11
  def configuration=(configuration)
8
- @@configuration = configuration
9
- Algolia.init @@configuration
12
+ @@configuration = configuration.merge(
13
+ :user_agent => "Algolia for Rails (#{AlgoliaSearch::VERSION}); Rails (#{Rails::VERSION::STRING})",
14
+ :symbolize_keys => false
15
+ )
16
+ end
17
+
18
+ def client_opts
19
+ @@opts ||= {}
20
+ end
21
+
22
+ def client_opts=(opts)
23
+ @@opts = opts
24
+ end
25
+
26
+ def client
27
+ if @client.nil?
28
+ setup_client
29
+ end
30
+
31
+ @client
32
+ end
33
+
34
+ def setup_client
35
+ @client = Algolia::Search::Client.new(Algolia::Search::Config.new(@@configuration), client_opts)
10
36
  end
11
37
  end
12
38
  end
@@ -9,7 +9,7 @@ module AlgoliaSearch
9
9
  class Kaminari < ::Kaminari::PaginatableArray
10
10
 
11
11
  def initialize(array, options)
12
- super(array, options)
12
+ super(array, **options)
13
13
  end
14
14
 
15
15
  def limit(num)
@@ -1,9 +1,14 @@
1
1
  namespace :algoliasearch do
2
-
2
+
3
3
  desc "Reindex all models"
4
4
  task :reindex => :environment do
5
5
  AlgoliaSearch::Utilities.reindex_all_models
6
6
  end
7
+
8
+ desc "Set settings to all indexes"
9
+ task :set_all_settings => :environment do
10
+ AlgoliaSearch::Utilities.set_settings_all_models
11
+ end
7
12
 
8
13
  desc "Clear all indexes"
9
14
  task :clear_indexes => :environment do
@@ -2,7 +2,11 @@ module AlgoliaSearch
2
2
  module Utilities
3
3
  class << self
4
4
  def get_model_classes
5
- Rails.application.eager_load! if Rails.application # Ensure all models are loaded (not necessary in production when cache_classes is true).
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
6
10
  AlgoliaSearch.instance_variable_get :@included_in
7
11
  end
8
12
 
@@ -20,11 +24,22 @@ module AlgoliaSearch
20
24
  puts ''
21
25
 
22
26
  klasses.each do |klass|
23
- say "#{klass}:"
27
+ puts klass
28
+ puts "Reindexing #{klass.count} records..."
29
+ klass.algolia_reindex
30
+ end
31
+ end
32
+
33
+ def set_settings_all_models
34
+ klasses = get_model_classes
24
35
 
25
- say "Reindexing #{klass.count} records...", true
36
+ puts ''
37
+ puts "Pushing settings for #{klasses.count} models: #{klasses.to_sentence}."
38
+ puts ''
26
39
 
27
- klass.algolia_reindex
40
+ klasses.each do |klass|
41
+ puts "Pushing #{klass} settings..."
42
+ klass.algolia_set_settings
28
43
  end
29
44
  end
30
45
  end
@@ -0,0 +1,3 @@
1
+ module AlgoliaSearch
2
+ VERSION = '2.2.2'
3
+ end