diff --git a/.gitignore b/.gitignore index 1eedfae..04b6d9e 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ tmp .DS_Store .ruby-* .idea/ +*.sqlite3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 11bd28a..4e0812d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 1.2.2 [unreleased] - Added support for phrase matches +- Added support for procs for `index_prefix` option ## 1.2.1 diff --git a/README.md b/README.md index a830065..7f3a06d 100644 --- a/README.md +++ b/README.md @@ -1326,6 +1326,10 @@ product.reindex # don't forget this Product.searchkick_index.refresh # or this ``` +## Multi-Tenancy + +Check out [this great post](https://www.tiagoamaro.com.br/2014/12/11/multi-tenancy-with-searchkick/). + ## Migrating from Tire 1. Change `search` methods to `tire.search` and add index name in existing search calls diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 18dc05b..9fc5070 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -15,7 +15,9 @@ module Searchkick class_variable_set :@@searchkick_options, options.dup class_variable_set :@@searchkick_klass, self class_variable_set :@@searchkick_callbacks, callbacks - class_variable_set :@@searchkick_index, options[:index_name] || [options[:index_prefix], model_name.plural, Searchkick.env].compact.join("_") + class_variable_set :@@searchkick_index, options[:index_name] || + (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env].compact.join("_") }) || + [options[:index_prefix], model_name.plural, Searchkick.env].compact.join("_") class << self def searchkick_search(term = nil, options = {}, &block) diff --git a/test/gemfiles/apartment.gemfile b/test/gemfiles/apartment.gemfile new file mode 100644 index 0000000..2998fa3 --- /dev/null +++ b/test/gemfiles/apartment.gemfile @@ -0,0 +1,8 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in searchkick.gemspec +gemspec path: "../../" + +gem "sqlite3" +gem "activerecord", "~> 4.2.0" +gem "apartment" diff --git a/test/multi_tenancy_test.rb b/test/multi_tenancy_test.rb new file mode 100644 index 0000000..be92205 --- /dev/null +++ b/test/multi_tenancy_test.rb @@ -0,0 +1,20 @@ +require_relative "test_helper" + +class MultiTenancyTest < Minitest::Test + def setup + skip unless defined?(Apartment) + end + + def test_basic + Apartment::Tenant.switch!("tenant1") + store_names ["Product A"], Tenant + Apartment::Tenant.switch!("tenant2") + store_names ["Product B"], Tenant + Apartment::Tenant.switch!("tenant1") + assert_search "product", ["Product A"], {load: false}, Tenant + Apartment::Tenant.switch!("tenant2") + assert_search "product", ["Product B"], {load: false}, Tenant + ensure + Apartment::Tenant.reset + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 3c6dd1d..1c84850 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -156,6 +156,43 @@ else ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks=) + if defined?(Apartment) + class Rails + def self.env + ENV["RACK_ENV"] + end + end + + tenants = ["tenant1", "tenant2"] + Apartment.configure do |config| + config.tenant_names = tenants + config.database_schema_file = false + config.excluded_models = ["Product", "Store", "Animal", "Dog", "Cat"] + end + + class Tenant < ActiveRecord::Base + searchkick index_prefix: -> { Apartment::Tenant.current } + end + + tenants.each do |tenant| + begin + Apartment::Tenant.create(tenant) + rescue Apartment::TenantExists + # do nothing + end + Apartment::Tenant.switch!(tenant) + + ActiveRecord::Migration.create_table :tenants, force: true do |t| + t.string :name + t.timestamps null: true + end + + Tenant.reindex + end + + Apartment::Tenant.reset + end + ActiveRecord::Migration.create_table :products do |t| t.string :name t.integer :store_id -- libgit2 0.21.0