diff --git a/lib/chewy/index.rb b/lib/chewy/index.rb index 6dcdc1569..552b38467 100644 --- a/lib/chewy/index.rb +++ b/lib/chewy/index.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/chewy/index_spec.rb b/spec/chewy/index_spec.rb index 8d30334e8..89f34c07e 100644 --- a/spec/chewy/index_spec.rb +++ b/spec/chewy/index_spec.rb @@ -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