Commit a31489fd1ac4ecb228440f9e895294f523a47f86
1 parent
1f76d066
Exists in
master
and in
21 other branches
Added multi-tenancy test
Showing
7 changed files
with
74 additions
and
1 deletions
Show diff stats
.gitignore
CHANGELOG.md
README.md
... | ... | @@ -1326,6 +1326,10 @@ product.reindex # don't forget this |
1326 | 1326 | Product.searchkick_index.refresh # or this |
1327 | 1327 | ``` |
1328 | 1328 | |
1329 | +## Multi-Tenancy | |
1330 | + | |
1331 | +Check out [this great post](https://www.tiagoamaro.com.br/2014/12/11/multi-tenancy-with-searchkick/). | |
1332 | + | |
1329 | 1333 | ## Migrating from Tire |
1330 | 1334 | |
1331 | 1335 | 1. Change `search` methods to `tire.search` and add index name in existing search calls | ... | ... |
lib/searchkick/model.rb
... | ... | @@ -15,7 +15,9 @@ module Searchkick |
15 | 15 | class_variable_set :@@searchkick_options, options.dup |
16 | 16 | class_variable_set :@@searchkick_klass, self |
17 | 17 | class_variable_set :@@searchkick_callbacks, callbacks |
18 | - class_variable_set :@@searchkick_index, options[:index_name] || [options[:index_prefix], model_name.plural, Searchkick.env].compact.join("_") | |
18 | + class_variable_set :@@searchkick_index, options[:index_name] || | |
19 | + (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env].compact.join("_") }) || | |
20 | + [options[:index_prefix], model_name.plural, Searchkick.env].compact.join("_") | |
19 | 21 | |
20 | 22 | class << self |
21 | 23 | def searchkick_search(term = nil, options = {}, &block) | ... | ... |
... | ... | @@ -0,0 +1,20 @@ |
1 | +require_relative "test_helper" | |
2 | + | |
3 | +class MultiTenancyTest < Minitest::Test | |
4 | + def setup | |
5 | + skip unless defined?(Apartment) | |
6 | + end | |
7 | + | |
8 | + def test_basic | |
9 | + Apartment::Tenant.switch!("tenant1") | |
10 | + store_names ["Product A"], Tenant | |
11 | + Apartment::Tenant.switch!("tenant2") | |
12 | + store_names ["Product B"], Tenant | |
13 | + Apartment::Tenant.switch!("tenant1") | |
14 | + assert_search "product", ["Product A"], {load: false}, Tenant | |
15 | + Apartment::Tenant.switch!("tenant2") | |
16 | + assert_search "product", ["Product B"], {load: false}, Tenant | |
17 | + ensure | |
18 | + Apartment::Tenant.reset | |
19 | + end | |
20 | +end | ... | ... |
test/test_helper.rb
... | ... | @@ -156,6 +156,43 @@ else |
156 | 156 | |
157 | 157 | ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks=) |
158 | 158 | |
159 | + if defined?(Apartment) | |
160 | + class Rails | |
161 | + def self.env | |
162 | + ENV["RACK_ENV"] | |
163 | + end | |
164 | + end | |
165 | + | |
166 | + tenants = ["tenant1", "tenant2"] | |
167 | + Apartment.configure do |config| | |
168 | + config.tenant_names = tenants | |
169 | + config.database_schema_file = false | |
170 | + config.excluded_models = ["Product", "Store", "Animal", "Dog", "Cat"] | |
171 | + end | |
172 | + | |
173 | + class Tenant < ActiveRecord::Base | |
174 | + searchkick index_prefix: -> { Apartment::Tenant.current } | |
175 | + end | |
176 | + | |
177 | + tenants.each do |tenant| | |
178 | + begin | |
179 | + Apartment::Tenant.create(tenant) | |
180 | + rescue Apartment::TenantExists | |
181 | + # do nothing | |
182 | + end | |
183 | + Apartment::Tenant.switch!(tenant) | |
184 | + | |
185 | + ActiveRecord::Migration.create_table :tenants, force: true do |t| | |
186 | + t.string :name | |
187 | + t.timestamps null: true | |
188 | + end | |
189 | + | |
190 | + Tenant.reindex | |
191 | + end | |
192 | + | |
193 | + Apartment::Tenant.reset | |
194 | + end | |
195 | + | |
159 | 196 | ActiveRecord::Migration.create_table :products do |t| |
160 | 197 | t.string :name |
161 | 198 | t.integer :store_id | ... | ... |