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

Refactor/remove memoization of index name prefix #382

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions lib/chewy/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,8 @@ class Index
# UsersIndex.index_name # => 'dudes'
#
def self.index_name(suggest = nil)
if suggest
@index_name = build_index_name(suggest, prefix: default_prefix)
else
@index_name ||= begin
build_index_name(
name.sub(/Index\Z/, '').demodulize.underscore,
prefix: default_prefix
) if name
end
end
@index_name or raise UndefinedIndex
index_name = build_index_name(index_name_stem(suggest), prefix: default_prefix)
index_name or raise UndefinedIndex
end

# Prefix to use
Expand Down Expand Up @@ -164,7 +155,7 @@ def self.scopes

def self.build_index_name *args
options = args.extract_options!
[options[:prefix], args.first || index_name, options[:suffix]].reject(&:blank?).join(?_)
[options[:prefix], args.first || index_name_stem, options[:suffix]].reject(&:blank?).join(?_)
end

def self.settings_hash
Expand All @@ -179,5 +170,16 @@ def self.mappings_hash
def self.index_params
[settings_hash, mappings_hash].inject(:merge)
end

# set or compute index_name_stem
def self.index_name_stem(suggest = nil)
if suggest
@index_name_stem = suggest
elsif name
@index_name_stem || name.sub(/Index\Z/, '').demodulize.underscore
else
@index_name_stem or raise UndefinedIndex
end
end
end
end
9 changes: 9 additions & 0 deletions spec/chewy/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@
specify { expect(DummiesIndex.index_name).to eq('testing_dummies') }
specify { expect(stub_index(:dummies) { index_name :users }.index_name).to eq('testing_users') }
end

context do
before { Chewy.settings = {prefix: 'tenant1'} }
specify {
expect {
Chewy.settings = {prefix: 'tenant2'}
}.to change { DummiesIndex.index_name }.from('tenant1_dummies').to('tenant2_dummies')
}
end
end

describe '.default_prefix' do
Expand Down