Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the request object when calling index! rather than nil #378

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ matrix:
env: RAILS_VERSION=5.1 SEQUEL_VERSION=5.0
- rvm: 2.6.0
env: RAILS_VERSION=5.2 SEQUEL_VERSION=5.0
- rvm: 2.7.0
env: RAILS_VERSION=5.2 SEQUEL_VERSION=5.0

before_install:
- wget http://api-key-dealer.herokuapp.com/clients/algolia-keys && chmod +x algolia-keys
Expand Down
12 changes: 12 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# CHANGELOG

## [1.24.1](https://github.com/algolia/algoliasearch-rails/releases/tag/1.24.1) (2020-07-15)

**Fixed**

* Fixes ruby warning 'Proc.new is deprecated'

## [1.24.0](https://github.com/algolia/algoliasearch-rails/releases/tag/1.24.0) (2020-02-21)

**Added**

* Adds indexLanguages in index settings

## [1.23.2](https://github.com/algolia/algoliasearch-rails/releases/tag/1.23.2) (2019-06-26)

**Fixed**
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<a href="https://discourse.algolia.com" target="_blank">Community Forum</a> •
<a href="http://stackoverflow.com/questions/tagged/algolia" target="_blank">Stack Overflow</a> •
<a href="https://github.com/algolia/algoliasearch-rails/issues" target="_blank">Report a bug</a> •
<a href="https://www.algolia.com/doc/framework-integration/rails/troubleshooting/faq/" target="_blank">FAQ</a> •
<a href="https://www.algolia.com/support" target="_blank">Support</a>
</p>

Expand Down Expand Up @@ -82,6 +83,8 @@ You can find the full reference on [Algolia's website](https://www.algolia.com/d
1. **[Testing](#testing)**
* [Notes](#notes)

1. **[Troubleshooting](#troubleshooting)**
* [Frequently asked questions](#frequently-asked-questions)



Expand Down Expand Up @@ -1182,3 +1185,11 @@ end



# Troubleshooting


## Frequently asked questions

Encountering an issue? Before reaching out to support, we recommend heading to our [FAQ](https://www.algolia.com/doc/framework-integration/rails/troubleshooting/faq/) where you will find answers for the most common issues and gotchas with the gem.


17 changes: 9 additions & 8 deletions lib/algoliasearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class IndexSettings
:disableTypoToleranceOnAttributes, :disableTypoToleranceOnWords, :separatorsToIndex,
# Language
:ignorePlurals, :removeStopWords, :camelCaseAttributes, :decompoundedAttributes,
:keepDiacriticsOnCharacters, :queryLanguages,
:keepDiacriticsOnCharacters, :queryLanguages, :indexLanguages,
# Query Rules
:enableRules,
# Query Strategy
Expand All @@ -90,9 +90,9 @@ class IndexSettings
end
end

def initialize(options, block)
def initialize(options, &block)
@options = options
instance_exec(&block) if block
instance_exec(&block) if block_given?
end

def use_serializer(serializer)
Expand Down Expand Up @@ -252,12 +252,14 @@ def to_settings
settings[:slaves] = additional_indexes.select { |opts, s| opts[:slave] }.map do |opts, s|
name = opts[:index_name]
name = "#{name}_#{Rails.env.to_s}" if opts[:per_environment]
name = "virtual(#{name})" if opts[:virtual]
name
end
settings.delete(:slaves) if settings[:slaves].empty?
settings[:replicas] = additional_indexes.select { |opts, s| opts[:replica] }.map do |opts, s|
name = opts[:index_name]
name = "#{name}_#{Rails.env.to_s}" if opts[:per_environment]
name = "virtual(#{name})" if opts[:virtual]
name
end
settings.delete(:replicas) if settings[:replicas].empty?
Expand All @@ -272,7 +274,7 @@ def add_index(index_name, options = {}, &block)
@additional_indexes ||= {}
raise MixedSlavesAndReplicas.new('Cannot mix slaves and replicas in the same configuration (add_slave is deprecated)') if (options[:slave] && @additional_indexes.any? { |opts, _| opts[:replica] }) || (options[:replica] && @additional_indexes.any? { |opts, _| opts[:slave] })
options[:index_name] = index_name
@additional_indexes[options] = IndexSettings.new(options, Proc.new)
@additional_indexes[options] = IndexSettings.new(options, &block)
end

def add_replica(index_name, options = {}, &block)
Expand Down Expand Up @@ -389,7 +391,7 @@ class <<base
end

def algoliasearch(options = {}, &block)
self.algoliasearch_settings = IndexSettings.new(options, block_given? ? Proc.new : nil)
self.algoliasearch_settings = IndexSettings.new(options, &block)
self.algoliasearch_options = { :type => algolia_full_const_get(model_name.to_s), :per_page => algoliasearch_settings.get_setting(:hitsPerPage) || 10, :page => 1 }.merge(options)

attr_accessor :highlight_result, :snippet_result
Expand Down Expand Up @@ -607,14 +609,14 @@ def algolia_index_objects(objects, synchronous = false)
next if algolia_indexing_disabled?(options)
index = algolia_ensure_init(options, settings)
next if options[:slave] || options[:replica]
task = index.save_objects(objects.map { |o| settings.get_attributes(o).merge 'objectID' => algolia_object_id_of(o, options) })
task = index.save_objects(objects.filter { |o| algolia_indexable?(o, options) && !algolia_object_id_of(o, options).blank? }.map { |o| settings.get_attributes(o).merge 'objectID' => algolia_object_id_of(o, options) })
index.wait_task(task["taskID"]) if synchronous || options[:synchronous]
end
end

def algolia_index!(object, synchronous = false)
return if algolia_without_auto_index_scope
algolia_configurations.each do |options, settings|
algolia_configurations.map do |options, settings|
next if algolia_indexing_disabled?(options)
object_id = algolia_object_id_of(object, options)
index = algolia_ensure_init(options, settings)
Expand All @@ -635,7 +637,6 @@ def algolia_index!(object, synchronous = false)
end
end
end
nil
end

def algolia_remove_from_index!(object, synchronous = false)
Expand Down
2 changes: 1 addition & 1 deletion lib/algoliasearch/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AlgoliaSearch
VERSION = '1.23.2'
VERSION = '1.24.1'
end
37 changes: 36 additions & 1 deletion spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
create_table :sub_replicas do |t|
t.string :name
end
create_table :virtual_replicas do |t|
t.string :name
end
unless OLD_RAILS
create_table :enqueued_objects do |t|
t.string :name
Expand Down Expand Up @@ -433,6 +436,19 @@ class SubReplicas < ActiveRecord::Base
end
end

class VirtualReplicas < ActiveRecord::Base
include AlgoliaSearch

algoliasearch :synchronous => true, :force_utf8_encoding => true, :index_name => safe_index_name("VirtualReplica_primary") do
attributesToIndex [:name]
customRanking ["asc(name)"]

add_replica safe_index_name("VirtualReplica_replica"), virtual: true do
customRanking ["desc(name)"]
end
end
end

class WithSlave < ActiveRecord::Base
include AlgoliaSearch

Expand Down Expand Up @@ -538,7 +554,7 @@ class SerializedObject < ActiveRecord::Base
Color.delete_all
end

it "Throw an exception if the data is too big" do
it "should throw an exception if the data is too big" do
expect {
Color.create! :name => 'big' * 100000
}.to raise_error(Algolia::AlgoliaProtocolError)
Expand Down Expand Up @@ -1190,6 +1206,25 @@ class ForwardToReplicasTwo < ActiveRecord::Base
end
end

describe "VirtualReplicas" do
before(:all) do
VirtualReplicas.clear_index!(true)
end

it "setup the replica" do
VirtualReplicas.send(:algolia_configurations).to_a.each do |v|
if v[0][:replica]
expect(v[0][:index_name]).to eq(safe_index_name("VirtualReplica_replica"))
expect(v[0][:virtual]).to eq(true)
expect(v[1].to_settings[:replicas]).to be_nil
else
expect(v[0][:index_name]).to eq(safe_index_name("VirtualReplica_primary"))
expect(v[1].to_settings[:replicas]).to match_array(["virtual(#{safe_index_name("VirtualReplica_replica")})"])
end
end
end
end

describe "WithSlave" do
before(:all) do
WithSlave.clear_index!(true)
Expand Down