acts_as_tenant 0.4.2 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +124 -25
  3. data/Rakefile +5 -3
  4. data/lib/acts_as_tenant/configuration.rb +6 -19
  5. data/lib/acts_as_tenant/controller_extensions/filter.rb +13 -0
  6. data/lib/acts_as_tenant/controller_extensions/subdomain.rb +20 -0
  7. data/lib/acts_as_tenant/controller_extensions/subdomain_or_domain.rb +20 -0
  8. data/lib/acts_as_tenant/controller_extensions.rb +10 -59
  9. data/lib/acts_as_tenant/errors.rb +3 -4
  10. data/lib/acts_as_tenant/model_extensions.rb +59 -136
  11. data/lib/acts_as_tenant/sidekiq.rb +16 -8
  12. data/lib/acts_as_tenant/tenant_helper.rb +7 -0
  13. data/lib/acts_as_tenant/test_tenant_middleware.rb +15 -0
  14. data/lib/acts_as_tenant/version.rb +1 -1
  15. data/lib/acts_as_tenant.rb +112 -14
  16. data/spec/acts_as_tenant/configuration_spec.rb +8 -19
  17. data/spec/acts_as_tenant/sidekiq_spec.rb +14 -19
  18. data/spec/{acts_as_tenant/tenant_by_filter_spec.rb → controllers/filter_spec.rb} +7 -12
  19. data/spec/controllers/subdomain_or_domain_spec.rb +55 -0
  20. data/spec/controllers/subdomain_spec.rb +49 -0
  21. data/spec/dummy/.ruby-version +1 -0
  22. data/spec/dummy/Rakefile +6 -0
  23. data/spec/dummy/app/assets/config/manifest.js +2 -0
  24. data/spec/dummy/app/assets/images/.keep +0 -0
  25. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  26. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  27. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  28. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  29. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  30. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  31. data/spec/dummy/app/javascript/packs/application.js +15 -0
  32. data/spec/dummy/app/jobs/application_job.rb +7 -0
  33. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  34. data/spec/dummy/app/mailers/user_mailer.rb +5 -0
  35. data/spec/dummy/app/models/account.rb +4 -0
  36. data/spec/dummy/app/models/aliased_task.rb +4 -0
  37. data/spec/dummy/app/models/article.rb +3 -0
  38. data/spec/dummy/app/models/comment.rb +5 -0
  39. data/spec/dummy/app/models/concerns/.keep +0 -0
  40. data/spec/dummy/app/models/custom_counter_cache_task.rb +4 -0
  41. data/spec/dummy/app/models/custom_foreign_key_task.rb +4 -0
  42. data/spec/dummy/app/models/custom_primary_key_task.rb +4 -0
  43. data/spec/dummy/app/models/global_project.rb +6 -0
  44. data/spec/dummy/app/models/global_project_with_conditions.rb +6 -0
  45. data/spec/dummy/app/models/global_project_with_if.rb +6 -0
  46. data/spec/dummy/app/models/manager.rb +4 -0
  47. data/spec/dummy/app/models/polymorphic_tenant_comment.rb +5 -0
  48. data/spec/dummy/app/models/project.rb +8 -0
  49. data/spec/dummy/app/models/task.rb +7 -0
  50. data/spec/dummy/app/models/unique_task.rb +5 -0
  51. data/spec/dummy/app/models/unscoped_model.rb +3 -0
  52. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  53. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  54. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  55. data/spec/dummy/bin/rails +4 -0
  56. data/spec/dummy/bin/rake +4 -0
  57. data/spec/dummy/bin/setup +33 -0
  58. data/spec/dummy/config/application.rb +19 -0
  59. data/spec/dummy/config/boot.rb +5 -0
  60. data/spec/dummy/config/cable.yml +10 -0
  61. data/spec/dummy/config/database.yml +19 -0
  62. data/spec/dummy/config/environment.rb +5 -0
  63. data/spec/dummy/config/environments/development.rb +62 -0
  64. data/spec/dummy/config/environments/production.rb +112 -0
  65. data/spec/dummy/config/environments/test.rb +49 -0
  66. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  67. data/spec/dummy/config/initializers/assets.rb +12 -0
  68. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  69. data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
  70. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  71. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  72. data/spec/dummy/config/initializers/inflections.rb +16 -0
  73. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  74. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  75. data/spec/dummy/config/locales/en.yml +40 -0
  76. data/spec/dummy/config/puma.rb +38 -0
  77. data/spec/dummy/config/routes.rb +4 -0
  78. data/spec/dummy/config/spring.rb +6 -0
  79. data/spec/dummy/config/storage.yml +34 -0
  80. data/spec/dummy/config.ru +5 -0
  81. data/spec/dummy/db/schema.rb +84 -0
  82. data/spec/dummy/lib/assets/.keep +0 -0
  83. data/spec/dummy/log/.keep +0 -0
  84. data/spec/dummy/public/404.html +67 -0
  85. data/spec/dummy/public/422.html +67 -0
  86. data/spec/dummy/public/500.html +66 -0
  87. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  88. data/spec/dummy/public/apple-touch-icon.png +0 -0
  89. data/spec/dummy/public/favicon.ico +0 -0
  90. data/spec/dummy/tmp/development_secret.txt +1 -0
  91. data/spec/fixtures/accounts.yml +10 -0
  92. data/spec/fixtures/custom_primary_key_tasks.yml +2 -0
  93. data/spec/fixtures/global_projects.yml +13 -0
  94. data/spec/fixtures/projects.yml +10 -0
  95. data/spec/helpers/tenant_helper_spec.rb +16 -0
  96. data/spec/middlewares/test_tenant_middleware_spec.rb +86 -0
  97. data/spec/models/model_extensions_spec.rb +413 -0
  98. data/spec/spec_helper.rb +9 -13
  99. metadata +205 -41
  100. data/.gitignore +0 -7
  101. data/.travis.yml +0 -4
  102. data/CHANGELOG.md +0 -108
  103. data/Gemfile +0 -4
  104. data/_config.yml +0 -1
  105. data/acts_as_tenant.gemspec +0 -32
  106. data/docs/blog_post.md +0 -67
  107. data/rails/init.rb +0 -2
  108. data/spec/active_record_helper.rb +0 -22
  109. data/spec/active_record_models.rb +0 -116
  110. data/spec/acts_as_tenant/model_extensions_spec.rb +0 -420
  111. data/spec/acts_as_tenant/tenant_by_subdomain_or_domain.rb +0 -46
  112. data/spec/acts_as_tenant/tenant_by_subdomain_spec.rb +0 -32
  113. data/spec/database.yml +0 -3
@@ -1,28 +1,17 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe ActsAsTenant::Configuration do
4
- describe 'no configuration given' do
5
- before do
6
- ActsAsTenant.configure
7
- end
4
+ after { ActsAsTenant.configure }
8
5
 
9
- it 'provides defaults' do
10
- expect(ActsAsTenant.configuration.require_tenant).not_to be_truthy
11
- end
6
+ it "provides defaults" do
7
+ expect(ActsAsTenant.configuration.require_tenant).not_to be_truthy
12
8
  end
13
9
 
14
- describe 'with config block' do
15
- after do
16
- ActsAsTenant.configure
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 'spec_helper'
2
- require 'sidekiq'
3
- require 'acts_as_tenant/sidekiq'
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) { { 'acts_as_tenant' => { 'class' => 'Account', 'id' => 1234 } } }
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 'saves tenant if present' do
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 'does not set tenant if not present' do
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 'restores tenant if tenant saved' do
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 'runs without tenant if no tenant saved' do
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
- describe 'Sidekiq configuration' do
55
- describe 'client configuration' do
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, :type => :controller do
15
+ describe ApplicationController2, type: :controller do
23
16
  controller do
24
17
  def index
25
- render :plain => "custom called"
18
+ # Exercise current_tenant helper method
19
+ render plain: current_tenant.name
26
20
  end
27
21
  end
28
22
 
29
- it 'Finds the correct tenant using the filter command' do
23
+ it "Finds the correct tenant using the filter command" do
30
24
  get :index
31
- expect(ActsAsTenant.current_tenant.name).to eq 'account1'
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
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ class SubdomainController < ActionController::Base
4
+ include Rails.application.routes.url_helpers
5
+ set_current_tenant_by_subdomain
6
+ end
7
+
8
+ describe SubdomainController, type: :controller do
9
+ let(:account) { accounts(:with_domain) }
10
+
11
+ controller(SubdomainController) 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 subdomain.example.com" do
19
+ @request.host = "#{account.subdomain}.example.com"
20
+ get :index
21
+ expect(ActsAsTenant.current_tenant).to eq account
22
+ expect(response.body).to eq(account.subdomain)
23
+ end
24
+
25
+ it "finds the correct tenant with a www.subdomain.example.com" do
26
+ @request.host = "www.#{account.subdomain}.example.com"
27
+ get :index
28
+ expect(ActsAsTenant.current_tenant).to eq account
29
+ expect(response.body).to eq(account.subdomain)
30
+ end
31
+
32
+ it "ignores case when finding tenant by subdomain" do
33
+ @request.host = "#{account.subdomain.upcase}.example.com"
34
+ get :index
35
+ expect(ActsAsTenant.current_tenant).to eq account
36
+ end
37
+
38
+ context "overriding subdomain lookup" do
39
+ after { controller.subdomain_lookup = :last }
40
+
41
+ it "allows overriding the subdomain lookup" do
42
+ controller.subdomain_lookup = :first
43
+ @request.host = "#{account.subdomain}.another.example.com"
44
+ get :index
45
+ expect(ActsAsTenant.current_tenant).to eq account
46
+ expect(response.body).to eq(account.subdomain)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1 @@
1
+ ruby-2.7.1
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative "config/application"
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,2 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../stylesheets .css
File without changes
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Channel < ActionCable::Channel::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Connection < ActionCable::Connection::Base
3
+ end
4
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
File without changes
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require activestorage
15
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ # Automatically retry jobs that encountered a deadlock
3
+ # retry_on ActiveRecord::Deadlocked
4
+
5
+ # Most jobs are safe to ignore if the underlying records are no longer available
6
+ # discard_on ActiveJob::DeserializationError
7
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: "from@example.com", to: "to@example.com"
3
+ layout "mailer"
4
+ end
@@ -0,0 +1,5 @@
1
+ class UserMailer < ApplicationMailer
2
+ def comment_notification
3
+ mail(body: "")
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ class Account < ActiveRecord::Base
2
+ has_many :projects
3
+ has_many :global_projects
4
+ end
@@ -0,0 +1,4 @@
1
+ class AliasedTask < ActiveRecord::Base
2
+ acts_as_tenant(:account)
3
+ belongs_to :project_alias, class_name: "Project"
4
+ end
@@ -0,0 +1,3 @@
1
+ class Article < ActiveRecord::Base
2
+ has_many :polymorphic_tenant_comments, as: :polymorphic_tenant_commentable
3
+ end
@@ -0,0 +1,5 @@
1
+ class Comment < ActiveRecord::Base
2
+ belongs_to :commentable, polymorphic: true
3
+ belongs_to :task, -> { where(comments: {commentable_type: "Task"}) }, foreign_key: "commentable_id"
4
+ acts_as_tenant :account
5
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ class CustomCounterCacheTask < ActiveRecord::Base
2
+ self.table_name = "projects"
3
+ acts_as_tenant(:account, counter_cache: "projects_count")
4
+ end
@@ -0,0 +1,4 @@
1
+ class CustomForeignKeyTask < ActiveRecord::Base
2
+ acts_as_tenant(:account, foreign_key: "accountID")
3
+ validates_uniqueness_to_tenant :name
4
+ end
@@ -0,0 +1,4 @@
1
+ class CustomPrimaryKeyTask < ActiveRecord::Base
2
+ acts_as_tenant(:account, foreign_key: "name", primary_key: "name")
3
+ validates_presence_of :name
4
+ end
@@ -0,0 +1,6 @@
1
+ class GlobalProject < ActiveRecord::Base
2
+ self.table_name = "projects"
3
+
4
+ acts_as_tenant :account, has_global_records: true
5
+ validates_uniqueness_to_tenant :name
6
+ end
@@ -0,0 +1,6 @@
1
+ class GlobalProjectWithConditions < ActiveRecord::Base
2
+ self.table_name = "projects"
3
+
4
+ acts_as_tenant :account, has_global_records: true
5
+ validates_uniqueness_to_tenant :name, conditions: -> { where(name: "foo") }
6
+ end
@@ -0,0 +1,6 @@
1
+ class GlobalProjectWithIf < ActiveRecord::Base
2
+ self.table_name = "projects"
3
+
4
+ acts_as_tenant :account, has_global_records: true
5
+ validates_uniqueness_to_tenant :name, if: ->(instance) { instance.name == "foo" }
6
+ end
@@ -0,0 +1,4 @@
1
+ class Manager < ActiveRecord::Base
2
+ belongs_to :project
3
+ acts_as_tenant :account
4
+ end
@@ -0,0 +1,5 @@
1
+ class PolymorphicTenantComment < ActiveRecord::Base
2
+ belongs_to :polymorphic_tenant_commentable, polymorphic: true
3
+ belongs_to :account
4
+ acts_as_tenant :polymorphic_tenant_commentable, polymorphic: true
5
+ end
@@ -0,0 +1,8 @@
1
+ class Project < ActiveRecord::Base
2
+ has_one :manager
3
+ has_many :tasks
4
+ has_many :polymorphic_tenant_comments, as: :polymorphic_tenant_commentable
5
+ acts_as_tenant :account
6
+
7
+ validates_uniqueness_to_tenant :name
8
+ end