acts_as_tenant 0.4.0 → 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 +90 -26
- data/Rakefile +5 -14
- data/lib/acts_as_tenant.rb +114 -12
- data/lib/acts_as_tenant/configuration.rb +6 -19
- data/lib/acts_as_tenant/controller_extensions.rb +11 -56
- 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 +62 -107
- data/lib/acts_as_tenant/sidekiq.rb +16 -8
- 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/controllers/filter_spec.rb +28 -0
- 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 +10 -15
- metadata +196 -54
- data/.gitignore +0 -7
- data/.travis.yml +0 -6
- data/CHANGELOG.md +0 -101
- data/Gemfile +0 -4
- data/acts_as_tenant.gemspec +0 -33
- data/rails/init.rb +0 -2
- data/spec/active_record_helper.rb +0 -22
- data/spec/active_record_models.rb +0 -109
- data/spec/acts_as_tenant/model_extensions_spec.rb +0 -394
- data/spec/acts_as_tenant/tenant_by_filter_spec.rb +0 -33
- 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
- data/spec/mongoid.yml +0 -6
- data/spec/mongoid_helper.rb +0 -23
- data/spec/mongoid_models.rb +0 -81
data/.gitignore
DELETED
data/.travis.yml
DELETED
data/CHANGELOG.md
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
0.4.0
|
2
|
-
------
|
3
|
-
* (Sub)domain lookup is no longer case insensitive
|
4
|
-
* Added ability to use inverse_of (thx lowjoel)
|
5
|
-
* Added ability to disable tenant checking for a block (thx duboff)
|
6
|
-
* Allow for validation that associations belong to the tenant to reflect on associations which return an Array from `where` (thx ludamillion)
|
7
|
-
|
8
|
-
0.3.9
|
9
|
-
-----
|
10
|
-
* Added ability to configure a default tenant for testing purposes. (thx iangreenleaf)
|
11
|
-
* AaT will now accept a string for a tenant_id (thx calebthompson)
|
12
|
-
* Improvements to readme (thx stgeneral)
|
13
|
-
|
14
|
-
0.3.8
|
15
|
-
-----
|
16
|
-
* Added Mongoid compatibility [thx iangreenleaf]
|
17
|
-
|
18
|
-
0.3.7
|
19
|
-
-----
|
20
|
-
* Fix for proper handling of polymorphic associations (thx sol1dus)
|
21
|
-
* Fix fefault scope to generate correct sql when using database prefix (thx IgorDobryn)
|
22
|
-
* Added ability to specify a custom Primary Key (thx matiasdim)
|
23
|
-
* Sidekiq 3.2.2+ no longer supports Ruby 1.9. Locking Sidekiq in gemspec at 3.2.1.
|
24
|
-
* Update RSPEC to 3.0. Convert all specs (thx petergoldstein)
|
25
|
-
* support sidekiq 3 interface (thx davekaro)
|
26
|
-
|
27
|
-
0.3.6
|
28
|
-
-----
|
29
|
-
* Added method `set_current_tenant_by_subdomain_or_domain` (thx preth00nker)
|
30
|
-
|
31
|
-
0.3.5
|
32
|
-
-----
|
33
|
-
* Fix to degredation introduced after 3.1 that prevented tenant_id from being set during initialization (thx jorgevaldivia)
|
34
|
-
|
35
|
-
0.3.4
|
36
|
-
-----
|
37
|
-
* Fix to a bug introduced in 0.3.2
|
38
|
-
|
39
|
-
0.3.3
|
40
|
-
-----
|
41
|
-
* Support user defined foreign keys on scoped models
|
42
|
-
|
43
|
-
0.3.2
|
44
|
-
-----
|
45
|
-
* correctly support nested models with has_many :through (thx dexion)
|
46
|
-
* Support 'www.subdomain.example.com' (thx wtfiwtz)
|
47
|
-
* Support setting `tenant_id` on scoped models if the `tenant_id` is nil (thx Matt Wilson)
|
48
|
-
|
49
|
-
0.3.1
|
50
|
-
-----
|
51
|
-
* Added support for Rails 4
|
52
|
-
|
53
|
-
0.3.0
|
54
|
-
-----
|
55
|
-
* You can now raise an exception if a query on a scope model is made without a tenant set. Adding an initializer that sets config.require_tenant to true will accomplish this. See readme for more details.
|
56
|
-
* `ActsAsTenant.with_tenant` will now return the value of the block it evaluates instead of the original tenant. The original tenant is restored automatically.
|
57
|
-
* acts_as_tenant now raises standard errors which can be caught individually.
|
58
|
-
* `set_current_tenant_to`, which was deprecated some versions ago and could lead to weird errors, has been removed.
|
59
|
-
|
60
|
-
|
61
|
-
0.2.9
|
62
|
-
-----
|
63
|
-
* Added support for many-to-many associations (thx Nucleoid)
|
64
|
-
|
65
|
-
0.2.8
|
66
|
-
-----
|
67
|
-
* Added dependencies to gemspec (thx aaronrenner)
|
68
|
-
* Added the `ActsAsTenant.with_tenant` block method (see readme) (thx aaronrenner)
|
69
|
-
* Acts_as_Tenant is now thread safe (thx davide)
|
70
|
-
|
71
|
-
0.2.7
|
72
|
-
-----
|
73
|
-
* Changed the interface for passing in the current_tenant manually in the controller. `set_current_tenant_to` has been deprecated and replaced by `set_current_tenant_through_filter` declaration and the `set_current_tenant` method. See readme for details.
|
74
|
-
|
75
|
-
0.2.6
|
76
|
-
-----
|
77
|
-
* Fixed a bug with resolving the tenant model name (thx devton!)
|
78
|
-
* Added support for using relations: User.create(:account => Account.first) now works, while it wouldn't before (thx bnmrrs)
|
79
|
-
|
80
|
-
0.2.5
|
81
|
-
-----
|
82
|
-
* Added Rails 3.2 compatibility (thx nickveys!)
|
83
|
-
|
84
|
-
0.2.4
|
85
|
-
-----
|
86
|
-
* Added correct handling of child models that do not have their parent set (foreign key == nil)
|
87
|
-
|
88
|
-
|
89
|
-
0.2.3
|
90
|
-
-----
|
91
|
-
* Added support for models that declare a has_one relationships, these would error out in the previous versions.
|
92
|
-
|
93
|
-
|
94
|
-
0.2.2
|
95
|
-
-----
|
96
|
-
* Enhancements
|
97
|
-
* Added support for aliased associations ( belongs_to :something, :class_name => 'SomethingElse'). In previous version these would raise an 'uninitialized constant' error.
|
98
|
-
|
99
|
-
0.2.1
|
100
|
-
-----
|
101
|
-
* Initial release
|
data/Gemfile
DELETED
data/acts_as_tenant.gemspec
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "acts_as_tenant/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.name = "acts_as_tenant"
|
7
|
-
s.version = ActsAsTenant::VERSION
|
8
|
-
s.authors = ["Erwin Matthijssen"]
|
9
|
-
s.email = ["erwin.matthijssen@gmail.com"]
|
10
|
-
s.homepage = "http://www.rollcallapp.com/blog"
|
11
|
-
s.summary = %q{Add multi-tenancy to Rails applications using a shared db strategy}
|
12
|
-
s.description = %q{Integrates multi-tenancy into a Rails application in a convenient and out-of-your way manner}
|
13
|
-
|
14
|
-
s.rubyforge_project = "acts_as_tenant"
|
15
|
-
|
16
|
-
s.files = `git ls-files`.split("\n")
|
17
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
-
s.require_paths = ["lib"]
|
20
|
-
|
21
|
-
#add_runtime_dependency("rails")
|
22
|
-
s.add_runtime_dependency('request_store', '>= 1.0.5')
|
23
|
-
s.add_dependency('rails','>= 3.1')
|
24
|
-
#s.add_dependency('request_store', '>= 1.0.5')
|
25
|
-
|
26
|
-
s.add_development_dependency('rspec', '>=3.0')
|
27
|
-
s.add_development_dependency('rspec-rails')
|
28
|
-
s.add_development_dependency('database_cleaner', '~> 1.3.0')
|
29
|
-
s.add_development_dependency('sqlite3')
|
30
|
-
s.add_development_dependency('mongoid', '~> 4.0')
|
31
|
-
|
32
|
-
s.add_development_dependency('sidekiq', '3.2.1')
|
33
|
-
end
|
data/rails/init.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'rails/all'
|
2
|
-
require 'database_cleaner'
|
3
|
-
require 'yaml'
|
4
|
-
|
5
|
-
dbconfig = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'database.yml')))
|
6
|
-
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
7
|
-
ActiveRecord::Base.establish_connection(dbconfig[ENV['DB'] || 'sqlite'])
|
8
|
-
|
9
|
-
RSpec.configure do |config|
|
10
|
-
config.before(:suite) do
|
11
|
-
DatabaseCleaner[:active_record].strategy = :transaction
|
12
|
-
DatabaseCleaner[:active_record].clean_with(:truncation)
|
13
|
-
end
|
14
|
-
|
15
|
-
config.before(:each) do
|
16
|
-
DatabaseCleaner[:active_record].start
|
17
|
-
end
|
18
|
-
|
19
|
-
config.after(:each) do
|
20
|
-
DatabaseCleaner[:active_record].clean
|
21
|
-
end
|
22
|
-
end
|
@@ -1,109 +0,0 @@
|
|
1
|
-
ActiveRecord::Schema.define(:version => 1) do
|
2
|
-
create_table :accounts, :force => true do |t|
|
3
|
-
t.column :name, :string
|
4
|
-
t.column :subdomain, :string
|
5
|
-
t.column :domain, :string
|
6
|
-
end
|
7
|
-
|
8
|
-
create_table :projects, :force => true do |t|
|
9
|
-
t.column :name, :string
|
10
|
-
t.column :account_id, :integer
|
11
|
-
end
|
12
|
-
|
13
|
-
create_table :managers, :force => true do |t|
|
14
|
-
t.column :name, :string
|
15
|
-
t.column :project_id, :integer
|
16
|
-
t.column :account_id, :integer
|
17
|
-
end
|
18
|
-
|
19
|
-
create_table :tasks, :force => true do |t|
|
20
|
-
t.column :name, :string
|
21
|
-
t.column :account_id, :integer
|
22
|
-
t.column :project_id, :integer
|
23
|
-
t.column :completed, :boolean
|
24
|
-
end
|
25
|
-
|
26
|
-
create_table :countries, :force => true do |t|
|
27
|
-
t.column :name, :string
|
28
|
-
end
|
29
|
-
|
30
|
-
create_table :unscoped_models, :force => true do |t|
|
31
|
-
t.column :name, :string
|
32
|
-
end
|
33
|
-
|
34
|
-
create_table :aliased_tasks, :force => true do |t|
|
35
|
-
t.column :name, :string
|
36
|
-
t.column :project_alias_id, :integer
|
37
|
-
t.column :account_id, :integer
|
38
|
-
end
|
39
|
-
|
40
|
-
create_table :unique_tasks, :force => true do |t|
|
41
|
-
t.column :name, :string
|
42
|
-
t.column :user_defined_scope, :string
|
43
|
-
t.column :project_id, :integer
|
44
|
-
t.column :account_id, :integer
|
45
|
-
end
|
46
|
-
|
47
|
-
create_table :custom_foreign_key_tasks, :force => true do |t|
|
48
|
-
t.column :name, :string
|
49
|
-
t.column :accountID, :integer
|
50
|
-
end
|
51
|
-
|
52
|
-
create_table :comments, :force => true do |t|
|
53
|
-
t.column :commentable_id, :integer
|
54
|
-
t.column :commentable_type, :string
|
55
|
-
t.column :account_id, :integer
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
class Account < ActiveRecord::Base
|
61
|
-
has_many :projects
|
62
|
-
end
|
63
|
-
|
64
|
-
class Project < ActiveRecord::Base
|
65
|
-
has_one :manager
|
66
|
-
has_many :tasks
|
67
|
-
acts_as_tenant :account
|
68
|
-
|
69
|
-
validates_uniqueness_to_tenant :name
|
70
|
-
end
|
71
|
-
|
72
|
-
class Manager < ActiveRecord::Base
|
73
|
-
belongs_to :project
|
74
|
-
acts_as_tenant :account
|
75
|
-
end
|
76
|
-
|
77
|
-
class Task < ActiveRecord::Base
|
78
|
-
belongs_to :project
|
79
|
-
default_scope -> { where(:completed => nil).order("name") }
|
80
|
-
|
81
|
-
acts_as_tenant :account
|
82
|
-
validates_uniqueness_of :name
|
83
|
-
end
|
84
|
-
|
85
|
-
class UnscopedModel < ActiveRecord::Base
|
86
|
-
validates_uniqueness_of :name
|
87
|
-
end
|
88
|
-
|
89
|
-
class AliasedTask < ActiveRecord::Base
|
90
|
-
acts_as_tenant(:account)
|
91
|
-
belongs_to :project_alias, :class_name => "Project"
|
92
|
-
end
|
93
|
-
|
94
|
-
class UniqueTask < ActiveRecord::Base
|
95
|
-
acts_as_tenant(:account)
|
96
|
-
belongs_to :project
|
97
|
-
validates_uniqueness_to_tenant :name, scope: :user_defined_scope
|
98
|
-
end
|
99
|
-
|
100
|
-
class CustomForeignKeyTask < ActiveRecord::Base
|
101
|
-
acts_as_tenant(:account, :foreign_key => "accountID")
|
102
|
-
validates_uniqueness_to_tenant :name
|
103
|
-
end
|
104
|
-
|
105
|
-
class Comment < ActiveRecord::Base
|
106
|
-
belongs_to :commentable, polymorphic: true
|
107
|
-
belongs_to :task, -> { where(comments: { commentable_type: 'Task' }) }, foreign_key: 'commentable_id'
|
108
|
-
acts_as_tenant :account
|
109
|
-
end
|
@@ -1,394 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require "#{$orm}_models"
|
3
|
-
|
4
|
-
describe ActsAsTenant do
|
5
|
-
after { ActsAsTenant.current_tenant = nil }
|
6
|
-
|
7
|
-
# Setting and getting
|
8
|
-
describe 'Setting the current tenant' do
|
9
|
-
before { ActsAsTenant.current_tenant = :foo }
|
10
|
-
it { ActsAsTenant.current_tenant == :foo }
|
11
|
-
end
|
12
|
-
|
13
|
-
describe 'is_scoped_as_tenant should return the correct value when true' do
|
14
|
-
it {expect(Project.respond_to?(:scoped_by_tenant?)).to eq(true)}
|
15
|
-
end
|
16
|
-
|
17
|
-
describe 'is_scoped_as_tenant should return the correct value when false' do
|
18
|
-
it {expect(UnscopedModel.respond_to?(:scoped_by_tenant?)).to eq(false)}
|
19
|
-
end
|
20
|
-
|
21
|
-
describe 'tenant_id should be immutable, if already set' do
|
22
|
-
before do
|
23
|
-
@account = Account.create!(:name => 'foo')
|
24
|
-
@project = @account.projects.create!(:name => 'bar')
|
25
|
-
end
|
26
|
-
|
27
|
-
it { expect {@project.account_id = @account.id + 1}.to raise_error }
|
28
|
-
end
|
29
|
-
|
30
|
-
describe 'setting tenant_id to the same value should not error' do
|
31
|
-
before do
|
32
|
-
@account = Account.create!(:name => 'foo')
|
33
|
-
@project = @account.projects.create!(:name => 'bar')
|
34
|
-
end
|
35
|
-
|
36
|
-
it { expect {@project.account_id = @account.id}.not_to raise_error }
|
37
|
-
end
|
38
|
-
|
39
|
-
describe 'setting tenant_id to a string with same to_i value should not error' do
|
40
|
-
before do
|
41
|
-
@account = Account.create!(:name => 'foo')
|
42
|
-
@project = @account.projects.create!(:name => 'bar')
|
43
|
-
end
|
44
|
-
|
45
|
-
it { expect {@project.account_id = @account.id.to_s}.not_to raise_error }
|
46
|
-
end
|
47
|
-
|
48
|
-
describe 'tenant_id should be mutable, if not already set' do
|
49
|
-
before do
|
50
|
-
@account = Account.create!(:name => 'foo')
|
51
|
-
@project = Project.create!(:name => 'bar')
|
52
|
-
end
|
53
|
-
|
54
|
-
it { expect(@project.account_id).to be_nil }
|
55
|
-
it { expect { @project.account = @account }.not_to raise_error }
|
56
|
-
end
|
57
|
-
|
58
|
-
describe 'tenant_id should auto populate after initialization' do
|
59
|
-
before do
|
60
|
-
@account = Account.create!(:name => 'foo')
|
61
|
-
ActsAsTenant.current_tenant = @account
|
62
|
-
end
|
63
|
-
it {expect(Project.new.account_id).to eq(@account.id)}
|
64
|
-
end
|
65
|
-
|
66
|
-
describe 'Handles custom foreign_key on tenant model' do
|
67
|
-
before do
|
68
|
-
@account = Account.create!(:name => 'foo')
|
69
|
-
ActsAsTenant.current_tenant = @account
|
70
|
-
@custom_foreign_key_task = CustomForeignKeyTask.create!(:name => 'foo')
|
71
|
-
end
|
72
|
-
|
73
|
-
it { expect(@custom_foreign_key_task.account).to eq(@account) }
|
74
|
-
end
|
75
|
-
|
76
|
-
# Scoping models
|
77
|
-
describe 'Project.all should be scoped to the current tenant if set' do
|
78
|
-
before do
|
79
|
-
@account1 = Account.create!(:name => 'foo')
|
80
|
-
@account2 = Account.create!(:name => 'bar')
|
81
|
-
|
82
|
-
@project1 = @account1.projects.create!(:name => 'foobar')
|
83
|
-
@project2 = @account2.projects.create!(:name => 'baz')
|
84
|
-
|
85
|
-
ActsAsTenant.current_tenant= @account1
|
86
|
-
@projects = Project.all
|
87
|
-
end
|
88
|
-
|
89
|
-
it { expect(@projects.length).to eq(1) }
|
90
|
-
it { expect(@projects).to eq([@project1]) }
|
91
|
-
end
|
92
|
-
|
93
|
-
describe 'Project.unscoped.all should return the unscoped value' do
|
94
|
-
before do
|
95
|
-
@account1 = Account.create!(:name => 'foo')
|
96
|
-
@account2 = Account.create!(:name => 'bar')
|
97
|
-
|
98
|
-
@project1 = @account1.projects.create!(:name => 'foobar')
|
99
|
-
@project2 = @account2.projects.create!(:name => 'baz')
|
100
|
-
|
101
|
-
ActsAsTenant.current_tenant= @account1
|
102
|
-
@projects = Project.unscoped
|
103
|
-
end
|
104
|
-
|
105
|
-
it { expect(@projects.count).to eq(2) }
|
106
|
-
end
|
107
|
-
|
108
|
-
describe 'Querying the tenant from a scoped model without a tenant set' do
|
109
|
-
before do
|
110
|
-
@project = Project.create!(:name => 'bar')
|
111
|
-
end
|
112
|
-
|
113
|
-
it { @project.account }
|
114
|
-
end
|
115
|
-
|
116
|
-
describe 'Querying the tenant from a scoped model with a tenant set' do
|
117
|
-
before do
|
118
|
-
@account = Account.create!(:name => 'foo')
|
119
|
-
@project = @account.projects.create!(:name => 'foobar')
|
120
|
-
ActsAsTenant.current_tenant= @account1
|
121
|
-
end
|
122
|
-
|
123
|
-
it { @project.account }
|
124
|
-
end
|
125
|
-
|
126
|
-
# Associations
|
127
|
-
describe 'Associations should be correctly scoped by current tenant' do
|
128
|
-
before do
|
129
|
-
@account = Account.create!(:name => 'foo')
|
130
|
-
@project = Project.create!(:name => 'foobar', :account => @account )
|
131
|
-
# the next line should normally be (nearly) impossible: a task assigned to a tenant project,
|
132
|
-
# but the task has no tenant assigned
|
133
|
-
@task1 = Task.create!(:name => 'no_tenant', :project => @project)
|
134
|
-
|
135
|
-
ActsAsTenant.current_tenant = @account
|
136
|
-
@task2 = @project.tasks.create!(:name => 'baz')
|
137
|
-
|
138
|
-
@project.reload
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'should correctly set the tenant on the task created with current_tenant set' do
|
142
|
-
expect(@task2.account).to eq(@account)
|
143
|
-
end
|
144
|
-
|
145
|
-
it 'should filter out the non-tenant task from the project' do
|
146
|
-
expect(@project.tasks.length).to eq(1)
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
describe 'Associations can only be made with in-scope objects' do
|
151
|
-
before do
|
152
|
-
@account = Account.create!(:name => 'foo')
|
153
|
-
@project1 = Project.create!(:name => 'inaccessible_project', :account => Account.create!)
|
154
|
-
ActsAsTenant.current_tenant = @account
|
155
|
-
|
156
|
-
@project2 = Project.create!(:name => 'accessible_project')
|
157
|
-
@task = @project2.tasks.create!(:name => 'bar')
|
158
|
-
end
|
159
|
-
|
160
|
-
it { expect(@task.update_attributes(:project_id => @project1.id)).to eq(false) }
|
161
|
-
end
|
162
|
-
|
163
|
-
describe "Create and save an AaT-enabled child without it having a parent" do
|
164
|
-
before do
|
165
|
-
@account = Account.create!(:name => 'baz')
|
166
|
-
ActsAsTenant.current_tenant = @account
|
167
|
-
end
|
168
|
-
it { expect(Task.create(:name => 'bar').valid?).to eq(true) }
|
169
|
-
end
|
170
|
-
|
171
|
-
describe "It should be possible to use aliased associations" do
|
172
|
-
it { expect(AliasedTask.create(:name => 'foo', :project_alias => @project2).valid?).to eq(true) }
|
173
|
-
end
|
174
|
-
|
175
|
-
describe "It should be possible to use associations with foreign_key from polymorphic" do
|
176
|
-
before do
|
177
|
-
@account = Account.create!(name: 'foo')
|
178
|
-
ActsAsTenant.current_tenant = @account
|
179
|
-
@project = Project.create!(name: 'project', account: @account)
|
180
|
-
@comment = Comment.new commentable: @project, account: @account
|
181
|
-
end
|
182
|
-
|
183
|
-
it { expect(@comment.save!).to eq(true) }
|
184
|
-
end
|
185
|
-
|
186
|
-
# Additional default_scopes
|
187
|
-
describe 'When dealing with a user defined default_scope' do
|
188
|
-
before do
|
189
|
-
@account = Account.create!(:name => 'foo')
|
190
|
-
@project1 = Project.create!(:name => 'inaccessible')
|
191
|
-
@task1 = Task.create!(:name => 'no_tenant', :project => @project1)
|
192
|
-
|
193
|
-
ActsAsTenant.current_tenant = @account
|
194
|
-
@project2 = Project.create!(:name => 'accessible')
|
195
|
-
@task2 = @project2.tasks.create!(:name => 'bar')
|
196
|
-
@task3 = @project2.tasks.create!(:name => 'baz')
|
197
|
-
@task4 = @project2.tasks.create!(:name => 'foo')
|
198
|
-
@task5 = @project2.tasks.create!(:name => 'foobar', :completed => true )
|
199
|
-
|
200
|
-
@tasks= Task.all
|
201
|
-
end
|
202
|
-
|
203
|
-
it 'should apply both the tenant scope and the user defined default_scope, including :order' do
|
204
|
-
expect(@tasks.length).to eq(3)
|
205
|
-
expect(@tasks).to eq([@task2, @task3, @task4])
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
# Validates_uniqueness
|
210
|
-
describe 'When using validates_uniqueness_to_tenant in a aat model' do
|
211
|
-
before do
|
212
|
-
account = Account.create!(:name => 'foo')
|
213
|
-
ActsAsTenant.current_tenant = account
|
214
|
-
Project.create!(:name => 'existing_name')
|
215
|
-
end
|
216
|
-
|
217
|
-
it 'should not be possible to create a duplicate within the same tenant' do
|
218
|
-
expect(Project.create(:name => 'existing_name').valid?).to eq(false)
|
219
|
-
end
|
220
|
-
|
221
|
-
it 'should be possible to create a duplicate outside the tenant scope' do
|
222
|
-
account = Account.create!(:name => 'baz')
|
223
|
-
ActsAsTenant.current_tenant = account
|
224
|
-
expect(Project.create(:name => 'bar').valid?).to eq(true)
|
225
|
-
end
|
226
|
-
end
|
227
|
-
|
228
|
-
describe 'Handles user defined scopes' do
|
229
|
-
before do
|
230
|
-
UniqueTask.create!(:name => 'foo', :user_defined_scope => 'unique_scope')
|
231
|
-
end
|
232
|
-
|
233
|
-
it { expect(UniqueTask.create(:name => 'foo', :user_defined_scope => 'another_scope')).to be_valid }
|
234
|
-
it { expect(UniqueTask.create(:name => 'foo', :user_defined_scope => 'unique_scope')).not_to be_valid }
|
235
|
-
end
|
236
|
-
|
237
|
-
describe 'When using validates_uniqueness_of in a NON-aat model' do
|
238
|
-
before do
|
239
|
-
UnscopedModel.create!(:name => 'foo')
|
240
|
-
end
|
241
|
-
it 'should not be possible to create duplicates' do
|
242
|
-
expect(UnscopedModel.create(:name => 'foo').valid?).to eq(false)
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
# ::with_tenant
|
247
|
-
describe "::with_tenant" do
|
248
|
-
it "should set current_tenant to the specified tenant inside the block" do
|
249
|
-
@account = Account.create!(:name => 'baz')
|
250
|
-
|
251
|
-
ActsAsTenant.with_tenant(@account) do
|
252
|
-
expect(ActsAsTenant.current_tenant).to eq(@account)
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
it "should reset current_tenant to the previous tenant once exiting the block" do
|
257
|
-
@account1 = Account.create!(:name => 'foo')
|
258
|
-
@account2 = Account.create!(:name => 'bar')
|
259
|
-
|
260
|
-
ActsAsTenant.current_tenant = @account1
|
261
|
-
ActsAsTenant.with_tenant @account2 do
|
262
|
-
|
263
|
-
end
|
264
|
-
|
265
|
-
expect(ActsAsTenant.current_tenant).to eq(@account1)
|
266
|
-
end
|
267
|
-
|
268
|
-
it "should return the value of the block" do
|
269
|
-
@account1 = Account.create!(:name => 'foo')
|
270
|
-
@account2 = Account.create!(:name => 'bar')
|
271
|
-
|
272
|
-
ActsAsTenant.current_tenant = @account1
|
273
|
-
value = ActsAsTenant.with_tenant @account2 do
|
274
|
-
"something"
|
275
|
-
end
|
276
|
-
|
277
|
-
expect(value).to eq "something"
|
278
|
-
end
|
279
|
-
|
280
|
-
it "should raise an error when no block is provided" do
|
281
|
-
expect { ActsAsTenant.with_tenant(nil) }.to raise_error(ArgumentError, /block required/)
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
describe "::without_tenant" do
|
286
|
-
it "should set current_tenant to nil inside the block" do
|
287
|
-
ActsAsTenant.without_tenant do
|
288
|
-
expect(ActsAsTenant.current_tenant).to be_nil
|
289
|
-
end
|
290
|
-
end
|
291
|
-
|
292
|
-
it "should reset current_tenant to the previous tenant once exiting the block" do
|
293
|
-
@account1 = Account.create!(:name => 'foo')
|
294
|
-
|
295
|
-
ActsAsTenant.current_tenant = @account1
|
296
|
-
ActsAsTenant.without_tenant do
|
297
|
-
end
|
298
|
-
|
299
|
-
expect(ActsAsTenant.current_tenant).to eq(@account1)
|
300
|
-
end
|
301
|
-
|
302
|
-
it "should return the value of the block" do
|
303
|
-
value = ActsAsTenant.without_tenant do
|
304
|
-
"something"
|
305
|
-
end
|
306
|
-
|
307
|
-
expect(value).to eq "something"
|
308
|
-
end
|
309
|
-
|
310
|
-
it "should raise an error when no block is provided" do
|
311
|
-
expect { ActsAsTenant.without_tenant }.to raise_error(ArgumentError, /block required/)
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
# Tenant required
|
316
|
-
context "tenant required" do
|
317
|
-
before do
|
318
|
-
@account1 = Account.create!(:name => 'foo')
|
319
|
-
@project1 = @account1.projects.create!(:name => 'foobar')
|
320
|
-
allow(ActsAsTenant.configuration).to receive_messages(require_tenant: true)
|
321
|
-
end
|
322
|
-
|
323
|
-
describe "raises exception if no tenant specified" do
|
324
|
-
it "should raise an error when no tenant is provided" do
|
325
|
-
expect { Project.all }.to raise_error(ActsAsTenant::Errors::NoTenantSet)
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
describe "does not raise exception when run in unscoped mode" do
|
330
|
-
it "should not raise an error when no tenant is provided" do
|
331
|
-
expect do
|
332
|
-
ActsAsTenant.without_tenant { Project.all }
|
333
|
-
end.to_not raise_error
|
334
|
-
end
|
335
|
-
end
|
336
|
-
end
|
337
|
-
|
338
|
-
context "no tenant required" do
|
339
|
-
describe "does not raise exception if no tenant specified" do
|
340
|
-
before do
|
341
|
-
@account1 = Account.create!(:name => 'foo')
|
342
|
-
@project1 = @account1.projects.create!(:name => 'foobar')
|
343
|
-
end
|
344
|
-
|
345
|
-
it "should not raise an error when no tenant is provided" do
|
346
|
-
expect { Project.all }.to_not raise_error
|
347
|
-
end
|
348
|
-
end
|
349
|
-
end
|
350
|
-
|
351
|
-
describe "ActsAsTenant.default_tenant=" do
|
352
|
-
before(:each) do
|
353
|
-
@account = Account.create!
|
354
|
-
end
|
355
|
-
|
356
|
-
after(:each) do
|
357
|
-
ActsAsTenant.default_tenant = nil
|
358
|
-
end
|
359
|
-
|
360
|
-
it "provides current_tenant" do
|
361
|
-
ActsAsTenant.default_tenant = @account
|
362
|
-
expect(ActsAsTenant.current_tenant).to eq(@account)
|
363
|
-
end
|
364
|
-
|
365
|
-
it "can be overridden by assignment" do
|
366
|
-
ActsAsTenant.default_tenant = @account
|
367
|
-
@account2 = Account.create!
|
368
|
-
ActsAsTenant.current_tenant = @account2
|
369
|
-
expect(ActsAsTenant.current_tenant).not_to eq(@account)
|
370
|
-
end
|
371
|
-
|
372
|
-
it "can be overridden by with_tenant" do
|
373
|
-
ActsAsTenant.default_tenant = @account
|
374
|
-
@account2 = Account.create!
|
375
|
-
ActsAsTenant.with_tenant @account2 do
|
376
|
-
expect(ActsAsTenant.current_tenant).to eq(@account2)
|
377
|
-
end
|
378
|
-
expect(ActsAsTenant.current_tenant).to eq(@account)
|
379
|
-
end
|
380
|
-
|
381
|
-
it "doesn't override existing current_tenant" do
|
382
|
-
@account2 = Account.create!
|
383
|
-
ActsAsTenant.current_tenant = @account2
|
384
|
-
ActsAsTenant.default_tenant = @account
|
385
|
-
expect(ActsAsTenant.current_tenant).to eq(@account2)
|
386
|
-
end
|
387
|
-
|
388
|
-
it "survives request resets" do
|
389
|
-
ActsAsTenant.default_tenant = @account
|
390
|
-
RequestStore.clear!
|
391
|
-
expect(ActsAsTenant.current_tenant).to eq(@account)
|
392
|
-
end
|
393
|
-
end
|
394
|
-
end
|