acts_as_tenant 0.4.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +86 -25
- data/Rakefile +5 -3
- data/lib/acts_as_tenant.rb +112 -14
- data/lib/acts_as_tenant/configuration.rb +6 -19
- data/lib/acts_as_tenant/controller_extensions.rb +10 -59
- 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/errors.rb +3 -4
- data/lib/acts_as_tenant/model_extensions.rb +33 -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/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/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.ru +5 -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/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 +383 -0
- data/spec/spec_helper.rb +9 -13
- metadata +201 -41
- data/.gitignore +0 -7
- data/.travis.yml +0 -4
- data/CHANGELOG.md +0 -119
- 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 -143
- data/spec/acts_as_tenant/model_extensions_spec.rb +0 -476
- 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
@@ -0,0 +1,383 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ActsAsTenant do
|
4
|
+
let(:account) { accounts(:foo) }
|
5
|
+
|
6
|
+
it "can set the current tenant" do
|
7
|
+
ActsAsTenant.current_tenant = :foo
|
8
|
+
expect(ActsAsTenant.current_tenant).to eq(:foo)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "is_scoped_as_tenant should return the correct value when true" do
|
12
|
+
expect(Project.respond_to?(:scoped_by_tenant?)).to eq(true)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "is_scoped_as_tenant should return the correct value when false" do
|
16
|
+
expect(UnscopedModel.respond_to?(:scoped_by_tenant?)).to eq(false)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "tenant_id should be immutable, if already set" do
|
20
|
+
project = account.projects.create!(name: "bar")
|
21
|
+
expect { project.account_id = account.id + 1 }.to raise_error(ActsAsTenant::Errors::TenantIsImmutable)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "setting tenant_id to the same value should not error" do
|
25
|
+
project = account.projects.create!(name: "bar")
|
26
|
+
expect { project.account_id = account.id }.not_to raise_error
|
27
|
+
end
|
28
|
+
|
29
|
+
it "setting tenant_id to a string with same to_i value should not error" do
|
30
|
+
project = account.projects.create!(name: "bar")
|
31
|
+
expect { project.account_id = account.id.to_s }.not_to raise_error
|
32
|
+
end
|
33
|
+
|
34
|
+
it "tenant_id should be mutable, if not already set" do
|
35
|
+
project = projects(:without_account)
|
36
|
+
expect(project.account_id).to be_nil
|
37
|
+
expect { project.account = account }.not_to raise_error
|
38
|
+
end
|
39
|
+
|
40
|
+
it "tenant_id should auto populate after initialization" do
|
41
|
+
ActsAsTenant.current_tenant = account
|
42
|
+
expect(Project.new.account_id).to eq(account.id)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "handles custom foreign_key on tenant model" do
|
46
|
+
ActsAsTenant.current_tenant = account
|
47
|
+
custom_foreign_key_task = CustomForeignKeyTask.create!(name: "foo")
|
48
|
+
expect(custom_foreign_key_task.account).to eq(account)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "handles custom primary_key on tenant model" do
|
52
|
+
ActsAsTenant.current_tenant = account
|
53
|
+
custom_primary_key_task = CustomPrimaryKeyTask.create!
|
54
|
+
expect(custom_primary_key_task.account).to eq(account)
|
55
|
+
expect(CustomPrimaryKeyTask.count).to eq(1)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should correctly increment and decrement the tenants counter_cache column" do
|
59
|
+
ActsAsTenant.current_tenant = account
|
60
|
+
project = CustomCounterCacheTask.create!(name: "bar")
|
61
|
+
expect(account.reload.projects_count).to eq(1)
|
62
|
+
project.destroy
|
63
|
+
expect(account.reload.projects_count).to eq(0)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "does not cache account association" do
|
67
|
+
project = account.projects.first
|
68
|
+
ActsAsTenant.current_tenant = account
|
69
|
+
expect(project.account.name).to eq(account.name)
|
70
|
+
account.update!(name: "Acme")
|
71
|
+
expect(project.account.name).to eq("Acme")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "Querying the tenant from a scoped model without a tenant set" do
|
75
|
+
expect(projects(:foo).account).to_not be_nil
|
76
|
+
end
|
77
|
+
|
78
|
+
it "Querying the tenant from a scoped model with a tenant set" do
|
79
|
+
ActsAsTenant.current_tenant = account
|
80
|
+
expect(projects(:foo).account).to eq(accounts(:foo))
|
81
|
+
expect(projects(:bar).account).to eq(accounts(:bar))
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "scoping models" do
|
85
|
+
it "should scope Project.all to the current tenant if set" do
|
86
|
+
ActsAsTenant.current_tenant = account
|
87
|
+
expect(Project.count).to eq(account.projects.count)
|
88
|
+
expect(Project.all).to eq(account.projects)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should allow unscoping" do
|
92
|
+
ActsAsTenant.current_tenant = account
|
93
|
+
expect(Project.unscoped.count).to be > account.projects.count
|
94
|
+
end
|
95
|
+
|
96
|
+
it "returns nothing with unsaved tenant" do
|
97
|
+
ActsAsTenant.current_tenant = Account.new
|
98
|
+
expect(Project.all.count).to eq(0)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "A tenant model with global records" do
|
103
|
+
before do
|
104
|
+
ActsAsTenant.current_tenant = account
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should return global and tenant projects" do
|
108
|
+
expect(GlobalProject.count).to eq(GlobalProject.unscoped.where(account: [nil, account]).count)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns global records with unsaved tenant" do
|
112
|
+
ActsAsTenant.current_tenant = Account.new
|
113
|
+
expect(GlobalProject.all.count).to eq(GlobalProject.unscoped.where(account: [nil]).count)
|
114
|
+
end
|
115
|
+
|
116
|
+
context "should validate tenant records against global & tenant records" do
|
117
|
+
it "global records are valid" do
|
118
|
+
expect(global_projects(:global).valid?).to be(true)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "allows separate global and tenant records" do
|
122
|
+
expect(GlobalProject.new(name: "foo new").valid?).to be(true)
|
123
|
+
end
|
124
|
+
|
125
|
+
it "is invalid with with duplicate tenant records" do
|
126
|
+
expect(GlobalProject.new(name: "global foo").valid?).to be(false)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "is invalid if tenant record conflicts with global record" do
|
130
|
+
expect(GlobalProject.new(name: "global").valid?).to be(false)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should add the model to ActsAsTenant.models_with_global_records" do
|
135
|
+
expect(ActsAsTenant.models_with_global_records.include?(GlobalProject)).to be_truthy
|
136
|
+
expect(ActsAsTenant.models_with_global_records.include?(Project)).to be_falsy
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Associations
|
141
|
+
context "Associations should be correctly scoped by current tenant" do
|
142
|
+
before do
|
143
|
+
@project = account.projects.create!(name: "foobar")
|
144
|
+
|
145
|
+
# the next line should normally be (nearly) impossible: a task assigned to a tenant project,
|
146
|
+
# but the task has no tenant assigned
|
147
|
+
@task1 = Task.create!(name: "no_tenant", project: @project)
|
148
|
+
|
149
|
+
ActsAsTenant.current_tenant = account
|
150
|
+
@task2 = @project.tasks.create!(name: "baz")
|
151
|
+
|
152
|
+
@project.reload
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should correctly set the tenant on the task created with current_tenant set" do
|
156
|
+
expect(@task2.account).to eq(account)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should filter out the non-tenant task from the project" do
|
160
|
+
expect(@project.tasks.length).to eq(1)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
it "associations can only be made with in-scope objects" do
|
165
|
+
project1 = accounts(:bar).projects.create!(name: "inaccessible_project")
|
166
|
+
ActsAsTenant.current_tenant = account
|
167
|
+
|
168
|
+
project2 = Project.create!(name: "accessible_project")
|
169
|
+
task = project2.tasks.create!(name: "bar")
|
170
|
+
|
171
|
+
expect(task.update(project_id: project1.id)).to eq(false)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "can create and save an AaT-enabled child without it having a parent" do
|
175
|
+
ActsAsTenant.current_tenant = account
|
176
|
+
expect(Task.new(name: "bar").valid?).to eq(true)
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should be possible to use aliased associations" do
|
180
|
+
expect(AliasedTask.create(name: "foo", project_alias: @project2).valid?).to eq(true)
|
181
|
+
end
|
182
|
+
|
183
|
+
describe "It should be possible to use associations with foreign_key from polymorphic" do
|
184
|
+
it "tenanted objects have a polymorphic association" do
|
185
|
+
ActsAsTenant.current_tenant = account
|
186
|
+
expect { Comment.create!(commentable: account.projects.first) }.not_to raise_error
|
187
|
+
end
|
188
|
+
|
189
|
+
context "tenant is polymorphic" do
|
190
|
+
before do
|
191
|
+
@project = Project.create!(name: "polymorphic project")
|
192
|
+
ActsAsTenant.current_tenant = @project
|
193
|
+
@comment = PolymorphicTenantComment.new(account: account)
|
194
|
+
end
|
195
|
+
|
196
|
+
it "populates commentable_type with the current tenant" do
|
197
|
+
expect(@comment.polymorphic_tenant_commentable_id).to eql(@project.id)
|
198
|
+
expect(@comment.polymorphic_tenant_commentable_type).to eql(@project.class.to_s)
|
199
|
+
end
|
200
|
+
|
201
|
+
context "with another type of tenant, same id" do
|
202
|
+
before do
|
203
|
+
@comment.save!
|
204
|
+
@article = Article.create!(id: @project.id, title: "article title")
|
205
|
+
@comment_on_article = @article.polymorphic_tenant_comments.create!
|
206
|
+
end
|
207
|
+
|
208
|
+
it "correctly scopes to the current tenant type" do
|
209
|
+
expect(@comment_on_article).to be_persisted
|
210
|
+
expect(@comment).to be_persisted
|
211
|
+
expect(PolymorphicTenantComment.count).to eql(1)
|
212
|
+
expect(PolymorphicTenantComment.all.first.attributes).to eql(@comment.attributes)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
# Additional default_scopes
|
219
|
+
it "should apply both the tenant scope and the user defined default_scope, including :order" do
|
220
|
+
project1 = Project.create!(name: "inaccessible")
|
221
|
+
Task.create!(name: "no_tenant", project: project1)
|
222
|
+
|
223
|
+
ActsAsTenant.current_tenant = account
|
224
|
+
project2 = Project.create!(name: "accessible")
|
225
|
+
task2 = project2.tasks.create!(name: "bar")
|
226
|
+
task3 = project2.tasks.create!(name: "baz")
|
227
|
+
task4 = project2.tasks.create!(name: "foo")
|
228
|
+
project2.tasks.create!(name: "foobar", completed: true)
|
229
|
+
|
230
|
+
tasks = Task.all
|
231
|
+
|
232
|
+
expect(tasks.length).to eq(3)
|
233
|
+
expect(tasks).to eq([task2, task3, task4])
|
234
|
+
end
|
235
|
+
|
236
|
+
# Validates_uniqueness
|
237
|
+
context "When using validates_uniqueness_to_tenant in a aat model" do
|
238
|
+
before do
|
239
|
+
@name = "existing_name"
|
240
|
+
ActsAsTenant.current_tenant = account
|
241
|
+
Project.create!(name: @name)
|
242
|
+
end
|
243
|
+
|
244
|
+
it "should not be possible to create a duplicate within the same tenant" do
|
245
|
+
expect(Project.new(name: @name).valid?).to eq(false)
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should be possible to create a duplicate in another tenant" do
|
249
|
+
ActsAsTenant.current_tenant = accounts(:bar)
|
250
|
+
expect(Project.create(name: @name).valid?).to eq(true)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
it "handles user defined scopes" do
|
255
|
+
UniqueTask.create!(name: "foo", user_defined_scope: "unique_scope")
|
256
|
+
expect(UniqueTask.create(name: "foo", user_defined_scope: "another_scope")).to be_valid
|
257
|
+
expect(UniqueTask.create(name: "foo", user_defined_scope: "unique_scope")).not_to be_valid
|
258
|
+
end
|
259
|
+
|
260
|
+
context "When using validates_uniqueness_of in a NON-aat model" do
|
261
|
+
it "should not be possible to create duplicates" do
|
262
|
+
UnscopedModel.create!(name: "foo")
|
263
|
+
expect(UnscopedModel.create(name: "foo").valid?).to eq(false)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
# ::with_tenant
|
268
|
+
describe "::with_tenant" do
|
269
|
+
it "should set current_tenant to the specified tenant inside the block" do
|
270
|
+
ActsAsTenant.with_tenant(account) do
|
271
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
it "should reset current_tenant to the previous tenant once exiting the block" do
|
276
|
+
ActsAsTenant.current_tenant = account
|
277
|
+
ActsAsTenant.with_tenant(accounts(:bar)) {}
|
278
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
279
|
+
end
|
280
|
+
|
281
|
+
it "should return the value of the block" do
|
282
|
+
ActsAsTenant.current_tenant = account
|
283
|
+
value = ActsAsTenant.with_tenant(accounts(:bar)) { "something" }
|
284
|
+
expect(value).to eq "something"
|
285
|
+
end
|
286
|
+
|
287
|
+
it "should raise an error when no block is provided" do
|
288
|
+
expect { ActsAsTenant.with_tenant(nil) }.to raise_error(ArgumentError, /block required/)
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
describe "::without_tenant" do
|
293
|
+
it "should set current_tenant to nil inside the block" do
|
294
|
+
ActsAsTenant.without_tenant do
|
295
|
+
expect(ActsAsTenant.current_tenant).to be_nil
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
it "should set current_tenant to nil even if default_tenant is set" do
|
300
|
+
old_default_tenant = ActsAsTenant.default_tenant
|
301
|
+
ActsAsTenant.default_tenant = Account.create!(name: "foo")
|
302
|
+
ActsAsTenant.without_tenant do
|
303
|
+
expect(ActsAsTenant.current_tenant).to be_nil
|
304
|
+
end
|
305
|
+
ensure
|
306
|
+
ActsAsTenant.default_tenant = old_default_tenant
|
307
|
+
end
|
308
|
+
|
309
|
+
it "should reset current_tenant to the previous tenant once exiting the block" do
|
310
|
+
ActsAsTenant.current_tenant = account
|
311
|
+
ActsAsTenant.without_tenant {}
|
312
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
313
|
+
end
|
314
|
+
|
315
|
+
it "should return the value of the block" do
|
316
|
+
value = ActsAsTenant.without_tenant { "something" }
|
317
|
+
expect(value).to eq "something"
|
318
|
+
end
|
319
|
+
|
320
|
+
it "should raise an error when no block is provided" do
|
321
|
+
expect { ActsAsTenant.without_tenant }.to raise_error(ArgumentError, /block required/)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
# Tenant required
|
326
|
+
context "tenant required" do
|
327
|
+
before do
|
328
|
+
account.projects.create!(name: "foobar")
|
329
|
+
allow(ActsAsTenant.configuration).to receive_messages(require_tenant: true)
|
330
|
+
end
|
331
|
+
|
332
|
+
it "should raise an error when no tenant is provided" do
|
333
|
+
expect { Project.all }.to raise_error(ActsAsTenant::Errors::NoTenantSet)
|
334
|
+
end
|
335
|
+
|
336
|
+
it "should not raise an error when no tenant is provided" do
|
337
|
+
expect { ActsAsTenant.without_tenant { Project.all } }.to_not raise_error
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
context "no tenant required" do
|
342
|
+
it "should not raise an error when no tenant is provided" do
|
343
|
+
expect { Project.all }.to_not raise_error
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
describe "ActsAsTenant.default_tenant=" do
|
348
|
+
after(:each) do
|
349
|
+
ActsAsTenant.default_tenant = nil
|
350
|
+
end
|
351
|
+
|
352
|
+
it "provides current_tenant" do
|
353
|
+
ActsAsTenant.default_tenant = account
|
354
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
355
|
+
end
|
356
|
+
|
357
|
+
it "can be overridden by assignment" do
|
358
|
+
ActsAsTenant.default_tenant = account
|
359
|
+
ActsAsTenant.current_tenant = accounts(:bar)
|
360
|
+
expect(ActsAsTenant.current_tenant).to eq(accounts(:bar))
|
361
|
+
end
|
362
|
+
|
363
|
+
it "can be overridden by with_tenant" do
|
364
|
+
ActsAsTenant.default_tenant = account
|
365
|
+
ActsAsTenant.with_tenant accounts(:bar) do
|
366
|
+
expect(ActsAsTenant.current_tenant).to eq(accounts(:bar))
|
367
|
+
end
|
368
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
369
|
+
end
|
370
|
+
|
371
|
+
it "doesn't override existing current_tenant" do
|
372
|
+
ActsAsTenant.current_tenant = accounts(:bar)
|
373
|
+
ActsAsTenant.default_tenant = account
|
374
|
+
expect(ActsAsTenant.current_tenant).to eq(accounts(:bar))
|
375
|
+
end
|
376
|
+
|
377
|
+
it "survives request resets" do
|
378
|
+
ActsAsTenant.default_tenant = account
|
379
|
+
RequestStore.clear!
|
380
|
+
expect(ActsAsTenant.current_tenant).to eq(account)
|
381
|
+
end
|
382
|
+
end
|
383
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,23 +1,19 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
3
|
|
4
|
-
|
4
|
+
require_relative "../spec/dummy/config/environment"
|
5
|
+
ActiveRecord::Migrator.migrations_paths = [File.expand_path("../spec/dummy/db/migrate", __dir__)]
|
6
|
+
ActiveRecord::Migration.maintain_test_schema!
|
5
7
|
|
6
|
-
require
|
7
|
-
require 'acts_as_tenant'
|
8
|
+
require "rspec/rails"
|
8
9
|
|
9
10
|
RSpec.configure do |config|
|
10
11
|
config.after(:each) do
|
11
12
|
ActsAsTenant.current_tenant = nil
|
12
13
|
end
|
13
14
|
|
15
|
+
config.fixture_path = "spec/fixtures"
|
16
|
+
config.global_fixtures = :all
|
17
|
+
config.use_transactional_fixtures = true
|
14
18
|
config.infer_base_class_for_anonymous_controllers = true
|
15
19
|
end
|
16
|
-
|
17
|
-
# Setup a test app
|
18
|
-
module Rollcall
|
19
|
-
class Application < Rails::Application; end
|
20
|
-
end
|
21
|
-
|
22
|
-
Rollcall::Application.config.secret_token = '1234567890123456789012345678901234567890'
|
23
|
-
Rollcall::Application.config.secret_key_base = '1234567890123456789012345678901234567890'
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_tenant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erwin Matthijssen
|
8
|
-
|
8
|
+
- Chris Oliver
|
9
|
+
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2020-11-18 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: request_store
|
@@ -30,14 +31,14 @@ dependencies:
|
|
30
31
|
requirements:
|
31
32
|
- - ">="
|
32
33
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
34
|
+
version: '5.2'
|
34
35
|
type: :runtime
|
35
36
|
prerelease: false
|
36
37
|
version_requirements: !ruby/object:Gem::Requirement
|
37
38
|
requirements:
|
38
39
|
- - ">="
|
39
40
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
41
|
+
version: '5.2'
|
41
42
|
- !ruby/object:Gem::Dependency
|
42
43
|
name: rspec
|
43
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,21 +68,41 @@ dependencies:
|
|
67
68
|
- !ruby/object:Gem::Version
|
68
69
|
version: '0'
|
69
70
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
71
|
+
name: sqlite3
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: sidekiq
|
71
86
|
requirement: !ruby/object:Gem::Requirement
|
72
87
|
requirements:
|
73
88
|
- - "~>"
|
74
89
|
- !ruby/object:Gem::Version
|
75
|
-
version: '1
|
90
|
+
version: '6.1'
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 6.1.2
|
76
94
|
type: :development
|
77
95
|
prerelease: false
|
78
96
|
version_requirements: !ruby/object:Gem::Requirement
|
79
97
|
requirements:
|
80
98
|
- - "~>"
|
81
99
|
- !ruby/object:Gem::Version
|
82
|
-
version: '1
|
100
|
+
version: '6.1'
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 6.1.2
|
83
104
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
105
|
+
name: standard
|
85
106
|
requirement: !ruby/object:Gem::Requirement
|
86
107
|
requirements:
|
87
108
|
- - ">="
|
@@ -95,59 +116,128 @@ dependencies:
|
|
95
116
|
- !ruby/object:Gem::Version
|
96
117
|
version: '0'
|
97
118
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
119
|
+
name: appraisal
|
99
120
|
requirement: !ruby/object:Gem::Requirement
|
100
121
|
requirements:
|
101
|
-
- -
|
122
|
+
- - ">="
|
102
123
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
124
|
+
version: '0'
|
104
125
|
type: :development
|
105
126
|
prerelease: false
|
106
127
|
version_requirements: !ruby/object:Gem::Requirement
|
107
128
|
requirements:
|
108
|
-
- -
|
129
|
+
- - ">="
|
109
130
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
131
|
+
version: '0'
|
111
132
|
description: Integrates multi-tenancy into a Rails application in a convenient and
|
112
133
|
out-of-your way manner
|
113
134
|
email:
|
114
135
|
- erwin.matthijssen@gmail.com
|
136
|
+
- excid3@gmail.com
|
115
137
|
executables: []
|
116
138
|
extensions: []
|
117
139
|
extra_rdoc_files: []
|
118
140
|
files:
|
119
|
-
- ".gitignore"
|
120
|
-
- ".travis.yml"
|
121
|
-
- CHANGELOG.md
|
122
|
-
- Gemfile
|
123
141
|
- MIT-LICENSE
|
124
142
|
- README.md
|
125
143
|
- Rakefile
|
126
|
-
- _config.yml
|
127
|
-
- acts_as_tenant.gemspec
|
128
|
-
- docs/blog_post.md
|
129
144
|
- lib/acts_as_tenant.rb
|
130
145
|
- lib/acts_as_tenant/configuration.rb
|
131
146
|
- lib/acts_as_tenant/controller_extensions.rb
|
147
|
+
- lib/acts_as_tenant/controller_extensions/filter.rb
|
148
|
+
- lib/acts_as_tenant/controller_extensions/subdomain.rb
|
149
|
+
- lib/acts_as_tenant/controller_extensions/subdomain_or_domain.rb
|
132
150
|
- lib/acts_as_tenant/errors.rb
|
133
151
|
- lib/acts_as_tenant/model_extensions.rb
|
134
152
|
- lib/acts_as_tenant/sidekiq.rb
|
153
|
+
- lib/acts_as_tenant/tenant_helper.rb
|
154
|
+
- lib/acts_as_tenant/test_tenant_middleware.rb
|
135
155
|
- lib/acts_as_tenant/version.rb
|
136
|
-
- rails/init.rb
|
137
|
-
- spec/active_record_helper.rb
|
138
|
-
- spec/active_record_models.rb
|
139
156
|
- spec/acts_as_tenant/configuration_spec.rb
|
140
|
-
- spec/acts_as_tenant/model_extensions_spec.rb
|
141
157
|
- spec/acts_as_tenant/sidekiq_spec.rb
|
142
|
-
- spec/
|
143
|
-
- spec/
|
144
|
-
- spec/
|
145
|
-
- spec/
|
158
|
+
- spec/controllers/filter_spec.rb
|
159
|
+
- spec/controllers/subdomain_or_domain_spec.rb
|
160
|
+
- spec/controllers/subdomain_spec.rb
|
161
|
+
- spec/dummy/.ruby-version
|
162
|
+
- spec/dummy/Rakefile
|
163
|
+
- spec/dummy/app/assets/config/manifest.js
|
164
|
+
- spec/dummy/app/assets/images/.keep
|
165
|
+
- spec/dummy/app/assets/stylesheets/application.css
|
166
|
+
- spec/dummy/app/channels/application_cable/channel.rb
|
167
|
+
- spec/dummy/app/channels/application_cable/connection.rb
|
168
|
+
- spec/dummy/app/controllers/application_controller.rb
|
169
|
+
- spec/dummy/app/controllers/concerns/.keep
|
170
|
+
- spec/dummy/app/helpers/application_helper.rb
|
171
|
+
- spec/dummy/app/javascript/packs/application.js
|
172
|
+
- spec/dummy/app/jobs/application_job.rb
|
173
|
+
- spec/dummy/app/mailers/application_mailer.rb
|
174
|
+
- spec/dummy/app/mailers/user_mailer.rb
|
175
|
+
- spec/dummy/app/models/account.rb
|
176
|
+
- spec/dummy/app/models/aliased_task.rb
|
177
|
+
- spec/dummy/app/models/article.rb
|
178
|
+
- spec/dummy/app/models/comment.rb
|
179
|
+
- spec/dummy/app/models/concerns/.keep
|
180
|
+
- spec/dummy/app/models/custom_counter_cache_task.rb
|
181
|
+
- spec/dummy/app/models/custom_foreign_key_task.rb
|
182
|
+
- spec/dummy/app/models/custom_primary_key_task.rb
|
183
|
+
- spec/dummy/app/models/global_project.rb
|
184
|
+
- spec/dummy/app/models/manager.rb
|
185
|
+
- spec/dummy/app/models/polymorphic_tenant_comment.rb
|
186
|
+
- spec/dummy/app/models/project.rb
|
187
|
+
- spec/dummy/app/models/task.rb
|
188
|
+
- spec/dummy/app/models/unique_task.rb
|
189
|
+
- spec/dummy/app/models/unscoped_model.rb
|
190
|
+
- spec/dummy/app/views/layouts/application.html.erb
|
191
|
+
- spec/dummy/app/views/layouts/mailer.html.erb
|
192
|
+
- spec/dummy/app/views/layouts/mailer.text.erb
|
193
|
+
- spec/dummy/bin/rails
|
194
|
+
- spec/dummy/bin/rake
|
195
|
+
- spec/dummy/bin/setup
|
196
|
+
- spec/dummy/config.ru
|
197
|
+
- spec/dummy/config/application.rb
|
198
|
+
- spec/dummy/config/boot.rb
|
199
|
+
- spec/dummy/config/cable.yml
|
200
|
+
- spec/dummy/config/database.yml
|
201
|
+
- spec/dummy/config/environment.rb
|
202
|
+
- spec/dummy/config/environments/development.rb
|
203
|
+
- spec/dummy/config/environments/production.rb
|
204
|
+
- spec/dummy/config/environments/test.rb
|
205
|
+
- spec/dummy/config/initializers/application_controller_renderer.rb
|
206
|
+
- spec/dummy/config/initializers/assets.rb
|
207
|
+
- spec/dummy/config/initializers/backtrace_silencers.rb
|
208
|
+
- spec/dummy/config/initializers/content_security_policy.rb
|
209
|
+
- spec/dummy/config/initializers/cookies_serializer.rb
|
210
|
+
- spec/dummy/config/initializers/filter_parameter_logging.rb
|
211
|
+
- spec/dummy/config/initializers/inflections.rb
|
212
|
+
- spec/dummy/config/initializers/mime_types.rb
|
213
|
+
- spec/dummy/config/initializers/wrap_parameters.rb
|
214
|
+
- spec/dummy/config/locales/en.yml
|
215
|
+
- spec/dummy/config/puma.rb
|
216
|
+
- spec/dummy/config/routes.rb
|
217
|
+
- spec/dummy/config/spring.rb
|
218
|
+
- spec/dummy/config/storage.yml
|
219
|
+
- spec/dummy/db/schema.rb
|
220
|
+
- spec/dummy/lib/assets/.keep
|
221
|
+
- spec/dummy/log/.keep
|
222
|
+
- spec/dummy/public/404.html
|
223
|
+
- spec/dummy/public/422.html
|
224
|
+
- spec/dummy/public/500.html
|
225
|
+
- spec/dummy/public/apple-touch-icon-precomposed.png
|
226
|
+
- spec/dummy/public/apple-touch-icon.png
|
227
|
+
- spec/dummy/public/favicon.ico
|
228
|
+
- spec/dummy/tmp/development_secret.txt
|
229
|
+
- spec/fixtures/accounts.yml
|
230
|
+
- spec/fixtures/custom_primary_key_tasks.yml
|
231
|
+
- spec/fixtures/global_projects.yml
|
232
|
+
- spec/fixtures/projects.yml
|
233
|
+
- spec/helpers/tenant_helper_spec.rb
|
234
|
+
- spec/middlewares/test_tenant_middleware_spec.rb
|
235
|
+
- spec/models/model_extensions_spec.rb
|
146
236
|
- spec/spec_helper.rb
|
147
|
-
homepage:
|
237
|
+
homepage: https://github.com/ErwinM/acts_as_tenant
|
148
238
|
licenses: []
|
149
239
|
metadata: {}
|
150
|
-
post_install_message:
|
240
|
+
post_install_message:
|
151
241
|
rdoc_options: []
|
152
242
|
require_paths:
|
153
243
|
- lib
|
@@ -162,19 +252,89 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
162
252
|
- !ruby/object:Gem::Version
|
163
253
|
version: '0'
|
164
254
|
requirements: []
|
165
|
-
|
166
|
-
|
167
|
-
signing_key:
|
255
|
+
rubygems_version: 3.1.4
|
256
|
+
signing_key:
|
168
257
|
specification_version: 4
|
169
258
|
summary: Add multi-tenancy to Rails applications using a shared db strategy
|
170
259
|
test_files:
|
171
|
-
- spec/active_record_helper.rb
|
172
|
-
- spec/active_record_models.rb
|
173
260
|
- spec/acts_as_tenant/configuration_spec.rb
|
174
|
-
- spec/acts_as_tenant/model_extensions_spec.rb
|
175
261
|
- spec/acts_as_tenant/sidekiq_spec.rb
|
176
|
-
- spec/
|
177
|
-
- spec/
|
178
|
-
- spec/
|
179
|
-
- spec/
|
262
|
+
- spec/controllers/filter_spec.rb
|
263
|
+
- spec/controllers/subdomain_or_domain_spec.rb
|
264
|
+
- spec/controllers/subdomain_spec.rb
|
265
|
+
- spec/dummy/.ruby-version
|
266
|
+
- spec/dummy/Rakefile
|
267
|
+
- spec/dummy/app/assets/config/manifest.js
|
268
|
+
- spec/dummy/app/assets/images/.keep
|
269
|
+
- spec/dummy/app/assets/stylesheets/application.css
|
270
|
+
- spec/dummy/app/channels/application_cable/channel.rb
|
271
|
+
- spec/dummy/app/channels/application_cable/connection.rb
|
272
|
+
- spec/dummy/app/controllers/application_controller.rb
|
273
|
+
- spec/dummy/app/controllers/concerns/.keep
|
274
|
+
- spec/dummy/app/helpers/application_helper.rb
|
275
|
+
- spec/dummy/app/javascript/packs/application.js
|
276
|
+
- spec/dummy/app/jobs/application_job.rb
|
277
|
+
- spec/dummy/app/mailers/application_mailer.rb
|
278
|
+
- spec/dummy/app/mailers/user_mailer.rb
|
279
|
+
- spec/dummy/app/models/account.rb
|
280
|
+
- spec/dummy/app/models/aliased_task.rb
|
281
|
+
- spec/dummy/app/models/article.rb
|
282
|
+
- spec/dummy/app/models/comment.rb
|
283
|
+
- spec/dummy/app/models/concerns/.keep
|
284
|
+
- spec/dummy/app/models/custom_counter_cache_task.rb
|
285
|
+
- spec/dummy/app/models/custom_foreign_key_task.rb
|
286
|
+
- spec/dummy/app/models/custom_primary_key_task.rb
|
287
|
+
- spec/dummy/app/models/global_project.rb
|
288
|
+
- spec/dummy/app/models/manager.rb
|
289
|
+
- spec/dummy/app/models/polymorphic_tenant_comment.rb
|
290
|
+
- spec/dummy/app/models/project.rb
|
291
|
+
- spec/dummy/app/models/task.rb
|
292
|
+
- spec/dummy/app/models/unique_task.rb
|
293
|
+
- spec/dummy/app/models/unscoped_model.rb
|
294
|
+
- spec/dummy/app/views/layouts/application.html.erb
|
295
|
+
- spec/dummy/app/views/layouts/mailer.html.erb
|
296
|
+
- spec/dummy/app/views/layouts/mailer.text.erb
|
297
|
+
- spec/dummy/bin/rails
|
298
|
+
- spec/dummy/bin/rake
|
299
|
+
- spec/dummy/bin/setup
|
300
|
+
- spec/dummy/config.ru
|
301
|
+
- spec/dummy/config/application.rb
|
302
|
+
- spec/dummy/config/boot.rb
|
303
|
+
- spec/dummy/config/cable.yml
|
304
|
+
- spec/dummy/config/database.yml
|
305
|
+
- spec/dummy/config/environment.rb
|
306
|
+
- spec/dummy/config/environments/development.rb
|
307
|
+
- spec/dummy/config/environments/production.rb
|
308
|
+
- spec/dummy/config/environments/test.rb
|
309
|
+
- spec/dummy/config/initializers/application_controller_renderer.rb
|
310
|
+
- spec/dummy/config/initializers/assets.rb
|
311
|
+
- spec/dummy/config/initializers/backtrace_silencers.rb
|
312
|
+
- spec/dummy/config/initializers/content_security_policy.rb
|
313
|
+
- spec/dummy/config/initializers/cookies_serializer.rb
|
314
|
+
- spec/dummy/config/initializers/filter_parameter_logging.rb
|
315
|
+
- spec/dummy/config/initializers/inflections.rb
|
316
|
+
- spec/dummy/config/initializers/mime_types.rb
|
317
|
+
- spec/dummy/config/initializers/wrap_parameters.rb
|
318
|
+
- spec/dummy/config/locales/en.yml
|
319
|
+
- spec/dummy/config/puma.rb
|
320
|
+
- spec/dummy/config/routes.rb
|
321
|
+
- spec/dummy/config/spring.rb
|
322
|
+
- spec/dummy/config/storage.yml
|
323
|
+
- spec/dummy/db/schema.rb
|
324
|
+
- spec/dummy/lib/assets/.keep
|
325
|
+
- spec/dummy/log/.keep
|
326
|
+
- spec/dummy/public/404.html
|
327
|
+
- spec/dummy/public/422.html
|
328
|
+
- spec/dummy/public/500.html
|
329
|
+
- spec/dummy/public/apple-touch-icon-precomposed.png
|
330
|
+
- spec/dummy/public/apple-touch-icon.png
|
331
|
+
- spec/dummy/public/favicon.ico
|
332
|
+
- spec/dummy/tmp/development_secret.txt
|
333
|
+
- spec/fixtures/accounts.yml
|
334
|
+
- spec/fixtures/custom_primary_key_tasks.yml
|
335
|
+
- spec/fixtures/global_projects.yml
|
336
|
+
- spec/fixtures/projects.yml
|
337
|
+
- spec/helpers/tenant_helper_spec.rb
|
338
|
+
- spec/middlewares/test_tenant_middleware_spec.rb
|
339
|
+
- spec/models/model_extensions_spec.rb
|
180
340
|
- spec/spec_helper.rb
|