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

fix: make sync opt to be selectable for auto setting update logic #425

3 changes: 2 additions & 1 deletion lib/algoliasearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,8 @@ def algolia_ensure_init(options = nil, settings = nil, index_settings = nil)
end

if !algolia_indexing_disabled?(options) && options[:check_settings] && algoliasearch_settings_changed?(current_settings, index_settings)
@algolia_indexes[settings].set_settings!(index_settings)
set_settings_method = options[:synchronous] ? :set_settings! : :set_settings
@algolia_indexes[settings].send(set_settings_method, index_settings)
end

@algolia_indexes[settings]
Expand Down
76 changes: 76 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,82 @@ def public?
end
end

describe 'EnableCheckSettingsSynchronously' do
before(:each) do
# NOTE:
# Redefine below class *each* time to avoid the cache in the class.
# If the cahce is ready, algolia_ensure_init call neither set_settings nor set_settings! ever.
Object.send(:remove_const, :EnableCheckSettingsSynchronously) if Object.constants.include?(:EnableCheckSettingsSynchronously)
class EnableCheckSettingsSynchronously < ActiveRecord::Base
include AlgoliaSearch

algoliasearch :check_settings => true, :synchronous => true do
end
end
end

describe 'has settings changes' do
before(:each) do
allow(EnableCheckSettingsSynchronously).to receive(:algoliasearch_settings_changed?).and_return(true)
end

it 'should call set_setting with wait_task(sync)' do
expect_any_instance_of(Algolia::Search::Index).to receive(:set_settings).and_call_original # wait_task use this return val
expect_any_instance_of(Algolia::Search::Index).to receive(:wait_task)
EnableCheckSettingsSynchronously.send(:algolia_ensure_init)
end
end

describe 'has no settings changes' do
before(:each) do
allow(EnableCheckSettingsSynchronously).to receive(:algoliasearch_settings_changed?).and_return(false)
end

it 'should not call set_setting' do
expect_any_instance_of(Algolia::Search::Index).not_to receive(:set_settings)
EnableCheckSettingsSynchronously.send(:algolia_ensure_init)
end
end
end

describe 'EnableCheckSettingsAsynchronously' do
before(:each) do
# NOTE:
# Redefine below class *each* time to avoid the cache in the class.
# If the cahce is ready, algolia_ensure_init call neither set_settings nor set_settings! ever.
Object.send(:remove_const, :EnableCheckSettingsAsynchronously) if Object.constants.include?(:EnableCheckSettingsAsynchronously)
class EnableCheckSettingsAsynchronously < ActiveRecord::Base
include AlgoliaSearch

algoliasearch :check_settings => true, :synchronous => false do
end
end
end

describe 'has settings changes' do
before(:each) do
allow(EnableCheckSettingsAsynchronously).to receive(:algoliasearch_settings_changed?).and_return(true)
end

it 'should call set_setting without wait_task(sync)' do
expect_any_instance_of(Algolia::Search::Index).to receive(:set_settings)
expect_any_instance_of(Algolia::Search::Index).not_to receive(:wait_task)
EnableCheckSettingsAsynchronously.send(:algolia_ensure_init)
end
end

describe 'has no settings changes' do
before(:each) do
allow(EnableCheckSettingsAsynchronously).to receive(:algoliasearch_settings_changed?).and_return(false)
end

it 'should not call set_setting' do
expect_any_instance_of(Algolia::Search::Index).not_to receive(:set_settings)
EnableCheckSettingsAsynchronously.send(:algolia_ensure_init)
end
end
end

describe 'SequelBook' do
before(:all) do
SequelBook.clear_index!(true)
Expand Down