acts_as_tenant 0.4.3 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +124 -25
- data/Rakefile +5 -3
- data/lib/acts_as_tenant/configuration.rb +6 -19
- data/lib/acts_as_tenant/controller_extensions/filter.rb +13 -0
- data/lib/acts_as_tenant/controller_extensions/subdomain.rb +20 -0
- data/lib/acts_as_tenant/controller_extensions/subdomain_or_domain.rb +20 -0
- data/lib/acts_as_tenant/controller_extensions.rb +10 -59
- data/lib/acts_as_tenant/errors.rb +3 -4
- data/lib/acts_as_tenant/model_extensions.rb +59 -136
- data/lib/acts_as_tenant/sidekiq.rb +11 -7
- data/lib/acts_as_tenant/tenant_helper.rb +7 -0
- data/lib/acts_as_tenant/test_tenant_middleware.rb +15 -0
- data/lib/acts_as_tenant/version.rb +1 -1
- data/lib/acts_as_tenant.rb +112 -14
- data/spec/acts_as_tenant/configuration_spec.rb +8 -19
- data/spec/acts_as_tenant/sidekiq_spec.rb +14 -19
- data/spec/{acts_as_tenant/tenant_by_filter_spec.rb → controllers/filter_spec.rb} +7 -12
- data/spec/controllers/subdomain_or_domain_spec.rb +55 -0
- data/spec/controllers/subdomain_spec.rb +49 -0
- data/spec/dummy/.ruby-version +1 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +2 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +2 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/javascript/packs/application.js +15 -0
- data/spec/dummy/app/jobs/application_job.rb +7 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/mailers/user_mailer.rb +5 -0
- data/spec/dummy/app/models/account.rb +4 -0
- data/spec/dummy/app/models/aliased_task.rb +4 -0
- data/spec/dummy/app/models/article.rb +3 -0
- data/spec/dummy/app/models/comment.rb +5 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/custom_counter_cache_task.rb +4 -0
- data/spec/dummy/app/models/custom_foreign_key_task.rb +4 -0
- data/spec/dummy/app/models/custom_primary_key_task.rb +4 -0
- data/spec/dummy/app/models/global_project.rb +6 -0
- data/spec/dummy/app/models/global_project_with_conditions.rb +6 -0
- data/spec/dummy/app/models/global_project_with_if.rb +6 -0
- data/spec/dummy/app/models/manager.rb +4 -0
- data/spec/dummy/app/models/polymorphic_tenant_comment.rb +5 -0
- data/spec/dummy/app/models/project.rb +8 -0
- data/spec/dummy/app/models/task.rb +7 -0
- data/spec/dummy/app/models/unique_task.rb +5 -0
- data/spec/dummy/app/models/unscoped_model.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +33 -0
- data/spec/dummy/config/application.rb +19 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +19 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +62 -0
- data/spec/dummy/config/environments/production.rb +112 -0
- data/spec/dummy/config/environments/test.rb +49 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +12 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +40 -0
- data/spec/dummy/config/puma.rb +38 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/db/schema.rb +84 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/tmp/development_secret.txt +1 -0
- data/spec/fixtures/accounts.yml +10 -0
- data/spec/fixtures/custom_primary_key_tasks.yml +2 -0
- data/spec/fixtures/global_projects.yml +13 -0
- data/spec/fixtures/projects.yml +10 -0
- data/spec/helpers/tenant_helper_spec.rb +16 -0
- data/spec/middlewares/test_tenant_middleware_spec.rb +86 -0
- data/spec/models/model_extensions_spec.rb +413 -0
- data/spec/spec_helper.rb +9 -13
- metadata +205 -41
- data/.gitignore +0 -7
- data/.travis.yml +0 -4
- data/CHANGELOG.md +0 -112
- data/Gemfile +0 -4
- data/_config.yml +0 -1
- data/acts_as_tenant.gemspec +0 -32
- data/docs/blog_post.md +0 -67
- data/rails/init.rb +0 -2
- data/spec/active_record_helper.rb +0 -22
- data/spec/active_record_models.rb +0 -116
- data/spec/acts_as_tenant/model_extensions_spec.rb +0 -420
- data/spec/acts_as_tenant/tenant_by_subdomain_or_domain.rb +0 -46
- data/spec/acts_as_tenant/tenant_by_subdomain_spec.rb +0 -32
- data/spec/database.yml +0 -3
@@ -1,109 +1,34 @@
|
|
1
1
|
module ActsAsTenant
|
2
|
-
@@tenant_klass = nil
|
3
|
-
@@models_with_global_records = []
|
4
|
-
|
5
|
-
def self.set_tenant_klass(klass)
|
6
|
-
@@tenant_klass = klass
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.tenant_klass
|
10
|
-
@@tenant_klass
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.models_with_global_records
|
14
|
-
@@models_with_global_records
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.add_global_record_model model
|
18
|
-
@@models_with_global_records.push(model)
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.fkey
|
22
|
-
"#{@@tenant_klass.to_s}_id"
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.current_tenant=(tenant)
|
26
|
-
RequestStore.store[:current_tenant] = tenant
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.current_tenant
|
30
|
-
RequestStore.store[:current_tenant] || self.default_tenant
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.unscoped=(unscoped)
|
34
|
-
RequestStore.store[:acts_as_tenant_unscoped] = unscoped
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.unscoped
|
38
|
-
RequestStore.store[:acts_as_tenant_unscoped]
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.unscoped?
|
42
|
-
!!unscoped
|
43
|
-
end
|
44
|
-
|
45
|
-
class << self
|
46
|
-
attr_accessor :default_tenant
|
47
|
-
end
|
48
|
-
|
49
|
-
def self.with_tenant(tenant, &block)
|
50
|
-
if block.nil?
|
51
|
-
raise ArgumentError, "block required"
|
52
|
-
end
|
53
|
-
|
54
|
-
old_tenant = self.current_tenant
|
55
|
-
self.current_tenant = tenant
|
56
|
-
value = block.call
|
57
|
-
return value
|
58
|
-
|
59
|
-
ensure
|
60
|
-
self.current_tenant = old_tenant
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.without_tenant(&block)
|
64
|
-
if block.nil?
|
65
|
-
raise ArgumentError, "block required"
|
66
|
-
end
|
67
|
-
|
68
|
-
old_tenant = current_tenant
|
69
|
-
old_unscoped = unscoped
|
70
|
-
|
71
|
-
self.current_tenant = nil
|
72
|
-
self.unscoped = true
|
73
|
-
value = block.call
|
74
|
-
return value
|
75
|
-
|
76
|
-
ensure
|
77
|
-
self.current_tenant = old_tenant
|
78
|
-
self.unscoped = old_unscoped
|
79
|
-
end
|
80
|
-
|
81
2
|
module ModelExtensions
|
82
|
-
|
83
|
-
base.extend(ClassMethods)
|
84
|
-
end
|
3
|
+
extend ActiveSupport::Concern
|
85
4
|
|
86
|
-
|
87
|
-
def acts_as_tenant(tenant = :account, options
|
5
|
+
class_methods do
|
6
|
+
def acts_as_tenant(tenant = :account, **options)
|
88
7
|
ActsAsTenant.set_tenant_klass(tenant)
|
89
8
|
|
90
9
|
ActsAsTenant.add_global_record_model(self) if options[:has_global_records]
|
91
10
|
|
92
11
|
# Create the association
|
93
|
-
valid_options = options.slice(:foreign_key, :class_name, :inverse_of, :optional)
|
12
|
+
valid_options = options.slice(:foreign_key, :class_name, :inverse_of, :optional, :primary_key, :counter_cache)
|
94
13
|
fkey = valid_options[:foreign_key] || ActsAsTenant.fkey
|
95
|
-
|
14
|
+
pkey = valid_options[:primary_key] || ActsAsTenant.pkey
|
15
|
+
polymorphic_type = valid_options[:foreign_type] || ActsAsTenant.polymorphic_type
|
16
|
+
belongs_to tenant, **valid_options
|
96
17
|
|
97
18
|
default_scope lambda {
|
98
19
|
if ActsAsTenant.configuration.require_tenant && ActsAsTenant.current_tenant.nil? && !ActsAsTenant.unscoped?
|
99
20
|
raise ActsAsTenant::Errors::NoTenantSet
|
100
21
|
end
|
22
|
+
|
101
23
|
if ActsAsTenant.current_tenant
|
102
|
-
keys = [ActsAsTenant.current_tenant.
|
24
|
+
keys = [ActsAsTenant.current_tenant.send(pkey)].compact
|
103
25
|
keys.push(nil) if options[:has_global_records]
|
104
|
-
|
26
|
+
|
27
|
+
query_criteria = {fkey.to_sym => keys}
|
28
|
+
query_criteria[polymorphic_type.to_sym] = ActsAsTenant.current_tenant.class.to_s if options[:polymorphic]
|
29
|
+
where(query_criteria)
|
105
30
|
else
|
106
|
-
|
31
|
+
all
|
107
32
|
end
|
108
33
|
}
|
109
34
|
|
@@ -111,26 +36,30 @@ module ActsAsTenant
|
|
111
36
|
# - new instances should have the tenant set
|
112
37
|
# - validate that associations belong to the tenant, currently only for belongs_to
|
113
38
|
#
|
114
|
-
before_validation
|
39
|
+
before_validation proc { |m|
|
115
40
|
if ActsAsTenant.current_tenant
|
116
|
-
|
41
|
+
if options[:polymorphic]
|
42
|
+
m.send("#{fkey}=".to_sym, ActsAsTenant.current_tenant.class.to_s) if m.send(fkey.to_s).nil?
|
43
|
+
m.send("#{polymorphic_type}=".to_sym, ActsAsTenant.current_tenant.class.to_s) if m.send(polymorphic_type.to_s).nil?
|
44
|
+
else
|
45
|
+
m.send "#{fkey}=".to_sym, ActsAsTenant.current_tenant.send(pkey)
|
46
|
+
end
|
117
47
|
end
|
118
|
-
}, :
|
48
|
+
}, on: :create
|
119
49
|
|
120
|
-
polymorphic_foreign_keys = reflect_on_all_associations(:belongs_to).select
|
50
|
+
polymorphic_foreign_keys = reflect_on_all_associations(:belongs_to).select { |a|
|
121
51
|
a.options[:polymorphic]
|
122
|
-
|
52
|
+
}.map { |a| a.foreign_key }
|
123
53
|
|
124
54
|
reflect_on_all_associations(:belongs_to).each do |a|
|
125
55
|
unless a == reflect_on_association(tenant) || polymorphic_foreign_keys.include?(a.foreign_key)
|
126
|
-
association_class = a.options[:class_name].nil? ? a.name.to_s.classify.constantize : a.options[:class_name].constantize
|
127
56
|
validates_each a.foreign_key.to_sym do |record, attr, value|
|
128
|
-
primary_key = if
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
record.errors.add attr, "association is invalid [ActsAsTenant]" unless value.nil? ||
|
57
|
+
primary_key = if a.respond_to?(:active_record_primary_key)
|
58
|
+
a.active_record_primary_key
|
59
|
+
else
|
60
|
+
a.primary_key
|
61
|
+
end.to_sym
|
62
|
+
record.errors.add attr, "association is invalid [ActsAsTenant]" unless value.nil? || a.klass.where(primary_key => value).any?
|
134
63
|
end
|
135
64
|
end
|
136
65
|
end
|
@@ -139,27 +68,19 @@ module ActsAsTenant
|
|
139
68
|
# - Rewrite the accessors to make tenant immutable
|
140
69
|
# - Add an override to prevent unnecessary db hits
|
141
70
|
# - Add a helper method to verify if a model has been scoped by AaT
|
142
|
-
to_include = Module.new
|
71
|
+
to_include = Module.new {
|
143
72
|
define_method "#{fkey}=" do |integer|
|
144
|
-
write_attribute(
|
73
|
+
write_attribute(fkey.to_s, integer)
|
145
74
|
raise ActsAsTenant::Errors::TenantIsImmutable if send("#{fkey}_changed?") && persisted? && !send("#{fkey}_was").nil?
|
146
75
|
integer
|
147
76
|
end
|
148
77
|
|
149
|
-
define_method "#{ActsAsTenant.tenant_klass
|
78
|
+
define_method "#{ActsAsTenant.tenant_klass}=" do |model|
|
150
79
|
super(model)
|
151
80
|
raise ActsAsTenant::Errors::TenantIsImmutable if send("#{fkey}_changed?") && persisted? && !send("#{fkey}_was").nil?
|
152
81
|
model
|
153
82
|
end
|
154
|
-
|
155
|
-
define_method "#{ActsAsTenant.tenant_klass.to_s}" do
|
156
|
-
if !ActsAsTenant.current_tenant.nil? && send(fkey) == ActsAsTenant.current_tenant.id
|
157
|
-
return ActsAsTenant.current_tenant
|
158
|
-
else
|
159
|
-
super()
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
83
|
+
}
|
163
84
|
include to_include
|
164
85
|
|
165
86
|
class << self
|
@@ -169,36 +90,38 @@ module ActsAsTenant
|
|
169
90
|
end
|
170
91
|
end
|
171
92
|
|
172
|
-
def validates_uniqueness_to_tenant(fields, args ={})
|
93
|
+
def validates_uniqueness_to_tenant(fields, args = {})
|
173
94
|
raise ActsAsTenant::Errors::ModelNotScopedByTenant unless respond_to?(:scoped_by_tenant?)
|
95
|
+
|
174
96
|
fkey = reflect_on_association(ActsAsTenant.tenant_klass).foreign_key
|
175
|
-
|
176
|
-
|
177
|
-
|
97
|
+
|
98
|
+
validation_args = args.clone
|
99
|
+
validation_args[:scope] = if args[:scope]
|
100
|
+
Array(args[:scope]) << fkey
|
178
101
|
else
|
179
|
-
|
102
|
+
fkey
|
180
103
|
end
|
181
104
|
|
182
|
-
|
105
|
+
# validating within tenant scope
|
106
|
+
validates_uniqueness_of(fields, validation_args)
|
183
107
|
|
184
108
|
if ActsAsTenant.models_with_global_records.include?(self)
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
end
|
109
|
+
arg_if = args.delete(:if)
|
110
|
+
arg_condition = args.delete(:conditions)
|
111
|
+
|
112
|
+
# if tenant is not set (instance is global) - validating globally
|
113
|
+
global_validation_args = args.merge(
|
114
|
+
if: ->(instance) { instance[fkey].blank? && (arg_if.blank? || arg_if.call(instance)) }
|
115
|
+
)
|
116
|
+
validates_uniqueness_of(fields, global_validation_args)
|
117
|
+
|
118
|
+
# if tenant is set (instance is not global) and records can be global - validating within records with blank tenant
|
119
|
+
blank_tenant_validation_args = args.merge({
|
120
|
+
conditions: -> { arg_condition.blank? ? where(fkey => nil) : arg_condition.call.where(fkey => nil) },
|
121
|
+
if: ->(instance) { instance[fkey].present? && (arg_if.blank? || arg_if.call(instance)) }
|
122
|
+
})
|
123
|
+
|
124
|
+
validates_uniqueness_of(fields, blank_tenant_validation_args)
|
202
125
|
end
|
203
126
|
end
|
204
127
|
end
|
@@ -2,11 +2,13 @@ module ActsAsTenant::Sidekiq
|
|
2
2
|
# Get the current tenant and store in the message to be sent to Sidekiq.
|
3
3
|
class Client
|
4
4
|
def call(worker_class, msg, queue, redis_pool)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
if ActsAsTenant.current_tenant.present?
|
6
|
+
msg["acts_as_tenant"] ||=
|
7
|
+
{
|
8
|
+
"class" => ActsAsTenant.current_tenant.class.name,
|
9
|
+
"id" => ActsAsTenant.current_tenant.id
|
10
|
+
}
|
11
|
+
end
|
10
12
|
|
11
13
|
yield
|
12
14
|
end
|
@@ -15,8 +17,8 @@ module ActsAsTenant::Sidekiq
|
|
15
17
|
# Pull the tenant out and run the current thread with it.
|
16
18
|
class Server
|
17
19
|
def call(worker_class, msg, queue)
|
18
|
-
if msg.has_key?(
|
19
|
-
account = msg[
|
20
|
+
if msg.has_key?("acts_as_tenant")
|
21
|
+
account = msg["acts_as_tenant"]["class"].constantize.find msg["acts_as_tenant"]["id"]
|
20
22
|
ActsAsTenant.with_tenant account do
|
21
23
|
yield
|
22
24
|
end
|
@@ -40,6 +42,8 @@ Sidekiq.configure_server do |config|
|
|
40
42
|
config.server_middleware do |chain|
|
41
43
|
if defined?(Sidekiq::Middleware::Server::RetryJobs)
|
42
44
|
chain.insert_before Sidekiq::Middleware::Server::RetryJobs, ActsAsTenant::Sidekiq::Server
|
45
|
+
elsif defined?(Sidekiq::Batch::Server)
|
46
|
+
chain.insert_before Sidekiq::Batch::Server, ActsAsTenant::Sidekiq::Server
|
43
47
|
else
|
44
48
|
chain.add ActsAsTenant::Sidekiq::Server
|
45
49
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActsAsTenant
|
2
|
+
class TestTenantMiddleware
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
previously_set_test_tenant = ActsAsTenant.test_tenant
|
9
|
+
ActsAsTenant.test_tenant = nil
|
10
|
+
@app.call(env)
|
11
|
+
ensure
|
12
|
+
ActsAsTenant.test_tenant = previously_set_test_tenant
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/acts_as_tenant.rb
CHANGED
@@ -1,27 +1,125 @@
|
|
1
1
|
require "request_store"
|
2
2
|
|
3
|
-
#$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
|
-
|
5
3
|
require "acts_as_tenant/version"
|
6
4
|
require "acts_as_tenant/errors"
|
7
|
-
require "acts_as_tenant/configuration"
|
8
|
-
require "acts_as_tenant/controller_extensions"
|
9
|
-
require "acts_as_tenant/model_extensions"
|
10
5
|
|
11
|
-
|
6
|
+
module ActsAsTenant
|
7
|
+
autoload :Configuration, "acts_as_tenant/configuration"
|
8
|
+
autoload :ControllerExtensions, "acts_as_tenant/controller_extensions"
|
9
|
+
autoload :ModelExtensions, "acts_as_tenant/model_extensions"
|
10
|
+
autoload :TenantHelper, "acts_as_tenant/tenant_helper"
|
11
|
+
|
12
|
+
@@configuration = nil
|
13
|
+
@@tenant_klass = nil
|
14
|
+
@@models_with_global_records = []
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_accessor :test_tenant
|
18
|
+
attr_writer :default_tenant
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.configure
|
22
|
+
@@configuration = Configuration.new
|
23
|
+
yield configuration if block_given?
|
24
|
+
configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.configuration
|
28
|
+
@@configuration || configure
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.set_tenant_klass(klass)
|
32
|
+
@@tenant_klass = klass
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.tenant_klass
|
36
|
+
@@tenant_klass
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.models_with_global_records
|
40
|
+
@@models_with_global_records
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.add_global_record_model model
|
44
|
+
@@models_with_global_records.push(model)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.fkey
|
48
|
+
"#{@@tenant_klass}_id"
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.pkey
|
52
|
+
ActsAsTenant.configuration.pkey
|
53
|
+
end
|
12
54
|
|
13
|
-
|
14
|
-
|
55
|
+
def self.polymorphic_type
|
56
|
+
"#{@@tenant_klass}_type"
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.current_tenant=(tenant)
|
60
|
+
RequestStore.store[:current_tenant] = tenant
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.current_tenant
|
64
|
+
RequestStore.store[:current_tenant] || test_tenant || default_tenant
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.unscoped=(unscoped)
|
68
|
+
RequestStore.store[:acts_as_tenant_unscoped] = unscoped
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.unscoped
|
72
|
+
RequestStore.store[:acts_as_tenant_unscoped]
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.unscoped?
|
76
|
+
!!unscoped
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.default_tenant
|
80
|
+
@default_tenant unless unscoped
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.with_tenant(tenant, &block)
|
84
|
+
if block.nil?
|
85
|
+
raise ArgumentError, "block required"
|
86
|
+
end
|
87
|
+
|
88
|
+
old_tenant = current_tenant
|
89
|
+
self.current_tenant = tenant
|
90
|
+
value = block.call
|
91
|
+
value
|
92
|
+
ensure
|
93
|
+
self.current_tenant = old_tenant
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.without_tenant(&block)
|
97
|
+
if block.nil?
|
98
|
+
raise ArgumentError, "block required"
|
99
|
+
end
|
100
|
+
|
101
|
+
old_tenant = current_tenant
|
102
|
+
old_unscoped = unscoped
|
103
|
+
|
104
|
+
self.current_tenant = nil
|
105
|
+
self.unscoped = true
|
106
|
+
value = block.call
|
107
|
+
value
|
108
|
+
ensure
|
109
|
+
self.current_tenant = old_tenant
|
110
|
+
self.unscoped = old_unscoped
|
111
|
+
end
|
15
112
|
end
|
16
113
|
|
17
|
-
|
18
|
-
|
114
|
+
ActiveSupport.on_load(:active_record) do |base|
|
115
|
+
base.include ActsAsTenant::ModelExtensions
|
19
116
|
end
|
20
117
|
|
21
|
-
|
22
|
-
|
118
|
+
ActiveSupport.on_load(:action_controller) do |base|
|
119
|
+
base.extend ActsAsTenant::ControllerExtensions
|
120
|
+
base.include ActsAsTenant::TenantHelper
|
23
121
|
end
|
24
122
|
|
25
|
-
|
123
|
+
ActiveSupport.on_load(:action_view) do |base|
|
124
|
+
base.include ActsAsTenant::TenantHelper
|
26
125
|
end
|
27
|
-
|
@@ -1,28 +1,17 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe ActsAsTenant::Configuration do
|
4
|
-
|
5
|
-
before do
|
6
|
-
ActsAsTenant.configure
|
7
|
-
end
|
4
|
+
after { ActsAsTenant.configure }
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
6
|
+
it "provides defaults" do
|
7
|
+
expect(ActsAsTenant.configuration.require_tenant).not_to be_truthy
|
12
8
|
end
|
13
9
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'stores config' do
|
20
|
-
ActsAsTenant.configure do |config|
|
21
|
-
config.require_tenant = true
|
22
|
-
end
|
23
|
-
|
24
|
-
expect(ActsAsTenant.configuration.require_tenant).to eq(true)
|
10
|
+
it "stores config" do
|
11
|
+
ActsAsTenant.configure do |config|
|
12
|
+
config.require_tenant = true
|
25
13
|
end
|
26
14
|
|
15
|
+
expect(ActsAsTenant.configuration.require_tenant).to eq(true)
|
27
16
|
end
|
28
17
|
end
|
@@ -1,28 +1,27 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "spec_helper"
|
2
|
+
require "sidekiq"
|
3
|
+
require "acts_as_tenant/sidekiq"
|
4
4
|
|
5
5
|
describe ActsAsTenant::Sidekiq do
|
6
|
-
after { ActsAsTenant.current_tenant = nil }
|
7
6
|
let(:account) { Account.new(id: 1234) }
|
8
|
-
let(:message) { {
|
7
|
+
let(:message) { {"acts_as_tenant" => {"class" => "Account", "id" => 1234}} }
|
9
8
|
|
10
9
|
describe ActsAsTenant::Sidekiq::Client do
|
11
10
|
subject { ActsAsTenant::Sidekiq::Client.new }
|
12
11
|
|
13
|
-
it
|
12
|
+
it "saves tenant if present" do
|
14
13
|
ActsAsTenant.current_tenant = account
|
15
14
|
|
16
15
|
msg = {}
|
17
|
-
subject.call(nil, msg, nil, nil) {
|
16
|
+
subject.call(nil, msg, nil, nil) {}
|
18
17
|
expect(msg).to eq message
|
19
18
|
end
|
20
19
|
|
21
|
-
it
|
20
|
+
it "does not set tenant if not present" do
|
22
21
|
expect(ActsAsTenant.current_tenant).to be_nil
|
23
22
|
|
24
23
|
msg = {}
|
25
|
-
subject.call(nil, msg, nil, nil) {
|
24
|
+
subject.call(nil, msg, nil, nil) {}
|
26
25
|
expect(msg).not_to eq message
|
27
26
|
end
|
28
27
|
end
|
@@ -30,7 +29,7 @@ describe ActsAsTenant::Sidekiq do
|
|
30
29
|
describe ActsAsTenant::Sidekiq::Server do
|
31
30
|
subject { ActsAsTenant::Sidekiq::Server.new }
|
32
31
|
|
33
|
-
it
|
32
|
+
it "restores tenant if tenant saved" do
|
34
33
|
expect(Account).to receive(:find).with(1234).once { account }
|
35
34
|
|
36
35
|
msg = message
|
@@ -40,7 +39,7 @@ describe ActsAsTenant::Sidekiq do
|
|
40
39
|
expect(ActsAsTenant.current_tenant).to be_nil
|
41
40
|
end
|
42
41
|
|
43
|
-
it
|
42
|
+
it "runs without tenant if no tenant saved" do
|
44
43
|
expect(Account).not_to receive(:find)
|
45
44
|
|
46
45
|
msg = {}
|
@@ -51,13 +50,9 @@ describe ActsAsTenant::Sidekiq do
|
|
51
50
|
end
|
52
51
|
end
|
53
52
|
|
54
|
-
|
55
|
-
|
56
|
-
it 'includes ActsAsTenant client' do
|
57
|
-
expect(Sidekiq.client_middleware.exists?(ActsAsTenant::Sidekiq::Client)).to eq(true)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
# unable to test server configuration
|
53
|
+
it "includes ActsAsTenant client middleware" do
|
54
|
+
expect(Sidekiq.client_middleware.exists?(ActsAsTenant::Sidekiq::Client)).to eq(true)
|
62
55
|
end
|
56
|
+
|
57
|
+
# unable to test server configuration
|
63
58
|
end
|
@@ -1,33 +1,28 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
#Setup test specific ApplicationController
|
4
|
-
class Account
|
5
|
-
attr_accessor :name
|
6
|
-
end
|
7
|
-
|
8
3
|
class ApplicationController2 < ActionController::Base
|
9
4
|
include Rails.application.routes.url_helpers
|
10
5
|
set_current_tenant_through_filter
|
11
6
|
before_action :your_method_that_finds_the_current_tenant
|
12
7
|
|
13
8
|
def your_method_that_finds_the_current_tenant
|
14
|
-
current_account = Account.new
|
15
|
-
current_account.name = 'account1'
|
9
|
+
current_account = Account.new(name: "account1")
|
16
10
|
set_current_tenant(current_account)
|
17
11
|
end
|
18
|
-
|
19
12
|
end
|
20
13
|
|
21
14
|
# Start testing
|
22
|
-
describe ApplicationController2, :
|
15
|
+
describe ApplicationController2, type: :controller do
|
23
16
|
controller do
|
24
17
|
def index
|
25
|
-
|
18
|
+
# Exercise current_tenant helper method
|
19
|
+
render plain: current_tenant.name
|
26
20
|
end
|
27
21
|
end
|
28
22
|
|
29
|
-
it
|
23
|
+
it "Finds the correct tenant using the filter command" do
|
30
24
|
get :index
|
31
|
-
expect(ActsAsTenant.current_tenant.name).to eq
|
25
|
+
expect(ActsAsTenant.current_tenant.name).to eq "account1"
|
26
|
+
expect(response.body).to eq "account1"
|
32
27
|
end
|
33
28
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
class DomainController < ActionController::Base
|
4
|
+
include Rails.application.routes.url_helpers
|
5
|
+
set_current_tenant_by_subdomain_or_domain
|
6
|
+
end
|
7
|
+
|
8
|
+
describe DomainController, type: :controller do
|
9
|
+
let(:account) { accounts(:with_domain) }
|
10
|
+
|
11
|
+
controller do
|
12
|
+
def index
|
13
|
+
# Exercise current_tenant helper method
|
14
|
+
render plain: current_tenant.name
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "finds the correct tenant with a example1.com" do
|
19
|
+
@request.host = account.domain
|
20
|
+
get :index
|
21
|
+
expect(ActsAsTenant.current_tenant).to eq account
|
22
|
+
expect(response.body).to eq account.name
|
23
|
+
end
|
24
|
+
|
25
|
+
it "finds the correct tenant with a subdomain.example.com" do
|
26
|
+
@request.host = "#{account.subdomain}.example.com"
|
27
|
+
get :index
|
28
|
+
expect(ActsAsTenant.current_tenant).to eq account
|
29
|
+
expect(response.body).to eq account.name
|
30
|
+
end
|
31
|
+
|
32
|
+
it "finds the correct tenant with a www.subdomain.example.com" do
|
33
|
+
@request.host = "www.#{account.subdomain}.example.com"
|
34
|
+
get :index
|
35
|
+
expect(ActsAsTenant.current_tenant).to eq account
|
36
|
+
end
|
37
|
+
|
38
|
+
it "ignores case when finding tenant by subdomain" do
|
39
|
+
@request.host = "#{account.subdomain.upcase}.example.com"
|
40
|
+
get :index
|
41
|
+
expect(ActsAsTenant.current_tenant).to eq account
|
42
|
+
end
|
43
|
+
|
44
|
+
context "overriding subdomain lookup" do
|
45
|
+
after { controller.subdomain_lookup = :last }
|
46
|
+
|
47
|
+
it "allows overriding the subdomain lookup" do
|
48
|
+
controller.subdomain_lookup = :first
|
49
|
+
@request.host = "#{account.subdomain}.another.example.com"
|
50
|
+
get :index
|
51
|
+
expect(ActsAsTenant.current_tenant).to eq account
|
52
|
+
expect(response.body).to eq(account.subdomain)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|