Permalink
Browse files

Added multi-tenancy test

  • Loading branch information...
ankane committed May 3, 2016
1 parent 1f76d06 commit a31489fd1ac4ecb228440f9e895294f523a47f86
Showing with 74 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +1 −0 CHANGELOG.md
  3. +4 −0 README.md
  4. +3 −1 lib/searchkick/model.rb
  5. +8 −0 test/gemfiles/apartment.gemfile
  6. +20 −0 test/multi_tenancy_test.rb
  7. +37 −0 test/test_helper.rb
View
@@ -19,3 +19,4 @@ tmp
.DS_Store
.ruby-*
.idea/
*.sqlite3
View
@@ -1,6 +1,7 @@
## 1.2.2 [unreleased]
- Added support for phrase matches
- Added support for procs for `index_prefix` option
## 1.2.1
View
@@ -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
View
@@ -15,7 +15,9 @@ def searchkick(options = {})
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)
@@ -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"
View
@@ -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
View
@@ -156,6 +156,43 @@ class Cat < Animal
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

0 comments on commit a31489f

Please sign in to comment.