ros-apartment 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/.rubocop-linter.yml +22 -0
  3. data/.pryrc +5 -3
  4. data/.rubocop.yml +17 -0
  5. data/.rubocop_todo.yml +29 -0
  6. data/.story_branch.yml +4 -0
  7. data/.travis.yml +12 -11
  8. data/Appraisals +18 -18
  9. data/Gemfile +4 -1
  10. data/README.md +33 -5
  11. data/Rakefile +2 -0
  12. data/apartment.gemspec +6 -6
  13. data/gemfiles/rails_4_2.gemfile +12 -10
  14. data/gemfiles/rails_5_0.gemfile +11 -9
  15. data/gemfiles/rails_5_1.gemfile +11 -9
  16. data/gemfiles/rails_5_2.gemfile +10 -8
  17. data/gemfiles/rails_6_0.gemfile +11 -9
  18. data/gemfiles/rails_master.gemfile +11 -9
  19. data/lib/apartment/adapters/abstract_adapter.rb +34 -34
  20. data/lib/apartment/adapters/abstract_jdbc_adapter.rb +4 -3
  21. data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
  22. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +13 -11
  23. data/lib/apartment/adapters/mysql2_adapter.rb +10 -9
  24. data/lib/apartment/adapters/postgis_adapter.rb +3 -2
  25. data/lib/apartment/adapters/postgresql_adapter.rb +28 -25
  26. data/lib/apartment/adapters/sqlite3_adapter.rb +16 -8
  27. data/lib/apartment/console.rb +28 -3
  28. data/lib/apartment/custom_console.rb +26 -0
  29. data/lib/apartment/deprecation.rb +2 -1
  30. data/lib/apartment/elevators/domain.rb +4 -3
  31. data/lib/apartment/elevators/first_subdomain.rb +3 -2
  32. data/lib/apartment/elevators/generic.rb +4 -3
  33. data/lib/apartment/elevators/host.rb +6 -1
  34. data/lib/apartment/elevators/host_hash.rb +6 -2
  35. data/lib/apartment/elevators/subdomain.rb +9 -5
  36. data/lib/apartment/migrator.rb +4 -3
  37. data/lib/apartment/railtie.rb +13 -5
  38. data/lib/apartment/reloader.rb +2 -1
  39. data/lib/apartment/tasks/enhancements.rb +4 -6
  40. data/lib/apartment/tenant.rb +3 -4
  41. data/lib/apartment/version.rb +3 -1
  42. data/lib/apartment.rb +15 -9
  43. data/lib/generators/apartment/install/install_generator.rb +4 -3
  44. data/lib/generators/apartment/install/templates/apartment.rb +3 -2
  45. data/lib/tasks/apartment.rake +19 -18
  46. data/spec/adapters/jdbc_mysql_adapter_spec.rb +5 -4
  47. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +10 -12
  48. data/spec/adapters/mysql2_adapter_spec.rb +15 -13
  49. data/spec/adapters/postgresql_adapter_spec.rb +22 -20
  50. data/spec/adapters/sqlite3_adapter_spec.rb +41 -23
  51. data/spec/apartment_spec.rb +4 -2
  52. data/spec/dummy/app/controllers/application_controller.rb +4 -3
  53. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  54. data/spec/dummy/app/models/application_record.rb +6 -0
  55. data/spec/dummy/app/models/company.rb +4 -2
  56. data/spec/dummy/app/models/user.rb +4 -2
  57. data/spec/dummy/config/application.rb +11 -9
  58. data/spec/dummy/config/boot.rb +4 -2
  59. data/spec/dummy/config/environment.rb +3 -1
  60. data/spec/dummy/config/environments/development.rb +2 -1
  61. data/spec/dummy/config/environments/production.rb +3 -1
  62. data/spec/dummy/config/environments/test.rb +3 -1
  63. data/spec/dummy/config/initializers/apartment.rb +4 -2
  64. data/spec/dummy/config/initializers/backtrace_silencers.rb +1 -0
  65. data/spec/dummy/config/initializers/inflections.rb +1 -0
  66. data/spec/dummy/config/initializers/mime_types.rb +1 -0
  67. data/spec/dummy/config/initializers/secret_token.rb +2 -0
  68. data/spec/dummy/config/initializers/session_store.rb +3 -1
  69. data/spec/dummy/config/routes.rb +3 -1
  70. data/spec/dummy/config.ru +3 -1
  71. data/spec/dummy/db/seeds/import.rb +3 -1
  72. data/spec/dummy/script/rails +4 -2
  73. data/spec/dummy_engine/config/initializers/apartment.rb +4 -3
  74. data/spec/dummy_engine/lib/dummy_engine/engine.rb +2 -0
  75. data/spec/dummy_engine/lib/dummy_engine/version.rb +3 -1
  76. data/spec/dummy_engine/lib/dummy_engine.rb +3 -1
  77. data/spec/dummy_engine/test/dummy/config/application.rb +4 -2
  78. data/spec/dummy_engine/test/dummy/config/boot.rb +4 -2
  79. data/spec/dummy_engine/test/dummy/config/environment.rb +3 -1
  80. data/spec/dummy_engine/test/dummy/config/environments/development.rb +2 -0
  81. data/spec/dummy_engine/test/dummy/config/environments/production.rb +2 -0
  82. data/spec/dummy_engine/test/dummy/config/environments/test.rb +2 -0
  83. data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +2 -0
  84. data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +1 -0
  85. data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +3 -1
  86. data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +2 -0
  87. data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +1 -0
  88. data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +1 -0
  89. data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +2 -0
  90. data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +2 -0
  91. data/spec/dummy_engine/test/dummy/config/routes.rb +2 -0
  92. data/spec/dummy_engine/test/dummy/config.ru +3 -1
  93. data/spec/examples/connection_adapter_examples.rb +15 -13
  94. data/spec/examples/generic_adapter_custom_configuration_example.rb +21 -23
  95. data/spec/examples/generic_adapter_examples.rb +40 -39
  96. data/spec/examples/schema_adapter_examples.rb +93 -88
  97. data/spec/integration/apartment_rake_integration_spec.rb +24 -24
  98. data/spec/integration/query_caching_spec.rb +10 -8
  99. data/spec/integration/use_within_an_engine_spec.rb +5 -5
  100. data/spec/schemas/v1.rb +2 -3
  101. data/spec/schemas/v2.rb +22 -24
  102. data/spec/schemas/v3.rb +26 -28
  103. data/spec/spec_helper.rb +8 -6
  104. data/spec/support/apartment_helpers.rb +9 -5
  105. data/spec/support/capybara_sessions.rb +5 -5
  106. data/spec/support/config.rb +5 -2
  107. data/spec/support/contexts.rb +10 -8
  108. data/spec/support/requirements.rb +19 -6
  109. data/spec/support/setup.rb +5 -5
  110. data/spec/tasks/apartment_rake_spec.rb +30 -35
  111. data/spec/tenant_spec.rb +48 -44
  112. data/spec/unit/config_spec.rb +26 -27
  113. data/spec/unit/elevators/domain_spec.rb +9 -8
  114. data/spec/unit/elevators/first_subdomain_spec.rb +14 -12
  115. data/spec/unit/elevators/generic_spec.rb +16 -15
  116. data/spec/unit/elevators/host_hash_spec.rb +9 -8
  117. data/spec/unit/elevators/host_spec.rb +26 -26
  118. data/spec/unit/elevators/subdomain_spec.rb +24 -23
  119. data/spec/unit/migrator_spec.rb +18 -17
  120. data/spec/unit/reloader_spec.rb +8 -8
  121. metadata +40 -22
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'rake'
3
5
  require 'apartment/migrator'
4
6
  require 'apartment/tenant'
5
7
 
6
- describe "apartment rake tasks" do
7
-
8
+ describe 'apartment rake tasks' do
8
9
  before do
9
10
  @rake = Rake::Application.new
10
11
  Rake.application = @rake
@@ -20,110 +21,104 @@ describe "apartment rake tasks" do
20
21
 
21
22
  after do
22
23
  Rake.application = nil
23
- ENV['VERSION'] = nil # linux users reported env variable carrying on between tests
24
+ ENV['VERSION'] = nil # linux users reported env variable carrying on between tests
24
25
  end
25
26
 
26
27
  after(:all) do
27
28
  Apartment::Test.load_schema
28
29
  end
29
30
 
30
- let(:version){ '1234' }
31
+ let(:version) { '1234' }
31
32
 
32
33
  context 'database migration' do
33
-
34
- let(:tenant_names){ 3.times.map{ Apartment::Test.next_db } }
35
- let(:tenant_count){ tenant_names.length }
34
+ let(:tenant_names) { 3.times.map { Apartment::Test.next_db } }
35
+ let(:tenant_count) { tenant_names.length }
36
36
 
37
37
  before do
38
38
  allow(Apartment).to receive(:tenant_names).and_return tenant_names
39
39
  end
40
40
 
41
- describe "apartment:migrate" do
41
+ describe 'apartment:migrate' do
42
42
  before do
43
- allow(ActiveRecord::Migrator).to receive(:migrate) # don't care about this
43
+ allow(ActiveRecord::Migrator).to receive(:migrate) # don't care about this
44
44
  end
45
45
 
46
- it "should migrate public and all multi-tenant dbs" do
46
+ it 'should migrate public and all multi-tenant dbs' do
47
47
  expect(Apartment::Migrator).to receive(:migrate).exactly(tenant_count).times
48
48
  @rake['apartment:migrate'].invoke
49
49
  end
50
50
  end
51
51
 
52
- describe "apartment:migrate:up" do
53
-
54
- context "without a version" do
52
+ describe 'apartment:migrate:up' do
53
+ context 'without a version' do
55
54
  before do
56
55
  ENV['VERSION'] = nil
57
56
  end
58
57
 
59
- it "requires a version to migrate to" do
60
- expect{
58
+ it 'requires a version to migrate to' do
59
+ expect do
61
60
  @rake['apartment:migrate:up'].invoke
62
- }.to raise_error("VERSION is required")
61
+ end.to raise_error('VERSION is required')
63
62
  end
64
63
  end
65
64
 
66
- context "with version" do
67
-
65
+ context 'with version' do
68
66
  before do
69
67
  ENV['VERSION'] = version
70
68
  end
71
69
 
72
- it "migrates up to a specific version" do
70
+ it 'migrates up to a specific version' do
73
71
  expect(Apartment::Migrator).to receive(:run).with(:up, anything, version.to_i).exactly(tenant_count).times
74
72
  @rake['apartment:migrate:up'].invoke
75
73
  end
76
74
  end
77
75
  end
78
76
 
79
- describe "apartment:migrate:down" do
80
-
81
- context "without a version" do
77
+ describe 'apartment:migrate:down' do
78
+ context 'without a version' do
82
79
  before do
83
80
  ENV['VERSION'] = nil
84
81
  end
85
82
 
86
- it "requires a version to migrate to" do
87
- expect{
83
+ it 'requires a version to migrate to' do
84
+ expect do
88
85
  @rake['apartment:migrate:down'].invoke
89
- }.to raise_error("VERSION is required")
86
+ end.to raise_error('VERSION is required')
90
87
  end
91
88
  end
92
89
 
93
- context "with version" do
94
-
90
+ context 'with version' do
95
91
  before do
96
92
  ENV['VERSION'] = version
97
93
  end
98
94
 
99
- it "migrates up to a specific version" do
95
+ it 'migrates up to a specific version' do
100
96
  expect(Apartment::Migrator).to receive(:run).with(:down, anything, version.to_i).exactly(tenant_count).times
101
97
  @rake['apartment:migrate:down'].invoke
102
98
  end
103
99
  end
104
100
  end
105
101
 
106
- describe "apartment:rollback" do
107
- let(:step){ '3' }
102
+ describe 'apartment:rollback' do
103
+ let(:step) { '3' }
108
104
 
109
- it "should rollback dbs" do
105
+ it 'should rollback dbs' do
110
106
  expect(Apartment::Migrator).to receive(:rollback).exactly(tenant_count).times
111
107
  @rake['apartment:rollback'].invoke
112
108
  end
113
109
 
114
- it "should rollback dbs STEP amt" do
110
+ it 'should rollback dbs STEP amt' do
115
111
  expect(Apartment::Migrator).to receive(:rollback).with(anything, step.to_i).exactly(tenant_count).times
116
112
  ENV['STEP'] = step
117
113
  @rake['apartment:rollback'].invoke
118
114
  end
119
115
  end
120
116
 
121
- describe "apartment:drop" do
122
- it "should migrate public and all multi-tenant dbs" do
117
+ describe 'apartment:drop' do
118
+ it 'should migrate public and all multi-tenant dbs' do
123
119
  expect(Apartment::Tenant).to receive(:drop).exactly(tenant_count).times
124
120
  @rake['apartment:drop'].invoke
125
121
  end
126
122
  end
127
-
128
123
  end
129
124
  end
data/spec/tenant_spec.rb CHANGED
@@ -1,24 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Apartment::Tenant do
4
- context "using mysql", database: :mysql do
5
-
6
+ context 'using mysql', database: :mysql do
6
7
  before { subject.reload!(config) }
7
8
 
8
- describe "#adapter" do
9
- it "should load mysql adapter" do
9
+ describe '#adapter' do
10
+ it 'should load mysql adapter' do
10
11
  subject.adapter
11
12
  expect(Apartment::Adapters::Mysql2Adapter).to be_a(Class)
12
13
  end
13
14
  end
14
15
 
15
- # TODO this doesn't belong here, but there aren't integration tests currently for mysql
16
+ # TODO: this doesn't belong here, but there aren't integration tests currently for mysql
16
17
  # where to put???
17
- describe "exception recovery", :type => :request do
18
+ describe 'exception recovery', type: :request do
18
19
  before do
19
20
  subject.create db1
20
21
  end
21
- after{ subject.drop db1 }
22
+ after { subject.drop db1 }
22
23
 
23
24
  # it "should recover from incorrect database" do
24
25
  # session = Capybara::Session.new(:rack_test, Capybara.app)
@@ -30,9 +31,9 @@ describe Apartment::Tenant do
30
31
  # end
31
32
  end
32
33
 
33
- # TODO re-organize these tests
34
- context "with prefix and schemas" do
35
- describe "#create" do
34
+ # TODO: re-organize these tests
35
+ context 'with prefix and schemas' do
36
+ describe '#create' do
36
37
  before do
37
38
  Apartment.configure do |config|
38
39
  config.prepend_environment = true
@@ -42,35 +43,41 @@ describe Apartment::Tenant do
42
43
  subject.reload!(config)
43
44
  end
44
45
 
45
- after { subject.drop "db_with_prefix" rescue nil }
46
+ after do
47
+ begin
48
+ subject.drop 'db_with_prefix'
49
+ rescue StandardError => _e
50
+ nil
51
+ end
52
+ end
46
53
 
47
- it "should create a new database" do
48
- subject.create "db_with_prefix"
54
+ it 'should create a new database' do
55
+ subject.create 'db_with_prefix'
49
56
  end
50
57
  end
51
58
  end
52
59
  end
53
60
 
54
- context "using postgresql", database: :postgresql do
61
+ context 'using postgresql', database: :postgresql do
55
62
  before do
56
63
  Apartment.use_schemas = true
57
64
  subject.reload!(config)
58
65
  end
59
66
 
60
- describe "#adapter" do
61
- it "should load postgresql adapter" do
67
+ describe '#adapter' do
68
+ it 'should load postgresql adapter' do
62
69
  expect(subject.adapter).to be_a(Apartment::Adapters::PostgresqlSchemaAdapter)
63
70
  end
64
71
 
65
- it "raises exception with invalid adapter specified" do
72
+ it 'raises exception with invalid adapter specified' do
66
73
  subject.reload!(config.merge(adapter: 'unknown'))
67
74
 
68
- expect {
75
+ expect do
69
76
  Apartment::Tenant.adapter
70
- }.to raise_error(RuntimeError)
77
+ end.to raise_error(RuntimeError)
71
78
  end
72
79
 
73
- context "threadsafety" do
80
+ context 'threadsafety' do
74
81
  before { subject.create db1 }
75
82
  after { subject.drop db1 }
76
83
 
@@ -83,8 +90,8 @@ describe Apartment::Tenant do
83
90
  end
84
91
  end
85
92
 
86
- # TODO above spec are also with use_schemas=true
87
- context "with schemas" do
93
+ # TODO: above spec are also with use_schemas=true
94
+ context 'with schemas' do
88
95
  before do
89
96
  Apartment.configure do |config|
90
97
  config.excluded_models = []
@@ -94,30 +101,28 @@ describe Apartment::Tenant do
94
101
  subject.create db1
95
102
  end
96
103
 
97
- after{ subject.drop db1 }
104
+ after { subject.drop db1 }
98
105
 
99
- describe "#create" do
100
- it "should seed data" do
106
+ describe '#create' do
107
+ it 'should seed data' do
101
108
  subject.switch! db1
102
109
  expect(User.count).to be > 0
103
110
  end
104
111
  end
105
112
 
106
- describe "#switch!" do
113
+ describe '#switch!' do
114
+ let(:x) { rand(3) }
107
115
 
108
- let(:x){ rand(3) }
116
+ context 'creating models' do
117
+ before { subject.create db2 }
118
+ after { subject.drop db2 }
109
119
 
110
- context "creating models" do
111
-
112
- before{ subject.create db2 }
113
- after{ subject.drop db2 }
114
-
115
- it "should create a model instance in the current schema" do
120
+ it 'should create a model instance in the current schema' do
116
121
  subject.switch! db2
117
- db2_count = User.count + x.times{ User.create }
122
+ db2_count = User.count + x.times { User.create }
118
123
 
119
124
  subject.switch! db1
120
- db_count = User.count + x.times{ User.create }
125
+ db_count = User.count + x.times { User.create }
121
126
 
122
127
  subject.switch! db2
123
128
  expect(User.count).to eq(db2_count)
@@ -127,11 +132,10 @@ describe Apartment::Tenant do
127
132
  end
128
133
  end
129
134
 
130
- context "with excluded models" do
131
-
135
+ context 'with excluded models' do
132
136
  before do
133
137
  Apartment.configure do |config|
134
- config.excluded_models = ["Company"]
138
+ config.excluded_models = ['Company']
135
139
  end
136
140
  subject.init
137
141
  end
@@ -144,12 +148,12 @@ describe Apartment::Tenant do
144
148
  end
145
149
  end
146
150
 
147
- it "should create excluded models in public schema" do
151
+ it 'should create excluded models in public schema' do
148
152
  subject.reset # ensure we're on public schema
149
- count = Company.count + x.times{ Company.create }
153
+ count = Company.count + x.times { Company.create }
150
154
 
151
155
  subject.switch! db1
152
- x.times{ Company.create }
156
+ x.times { Company.create }
153
157
  expect(Company.count).to eq(count + x)
154
158
  subject.reset
155
159
  expect(Company.count).to eq(count + x)
@@ -158,7 +162,7 @@ describe Apartment::Tenant do
158
162
  end
159
163
  end
160
164
 
161
- context "seed paths" do
165
+ context 'seed paths' do
162
166
  before do
163
167
  Apartment.configure do |config|
164
168
  config.excluded_models = []
@@ -167,7 +171,7 @@ describe Apartment::Tenant do
167
171
  end
168
172
  end
169
173
 
170
- after{ subject.drop db1 }
174
+ after { subject.drop db1 }
171
175
 
172
176
  it 'should seed from default path' do
173
177
  subject.create db1
@@ -178,7 +182,7 @@ describe Apartment::Tenant do
178
182
 
179
183
  it 'should seed from custom path' do
180
184
  Apartment.configure do |config|
181
- config.seed_data_file = "#{Rails.root}/db/seeds/import.rb"
185
+ config.seed_data_file = Rails.root.join('db', 'seeds', 'import.rb')
182
186
  end
183
187
  subject.create db1
184
188
  subject.switch! db1
@@ -1,11 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Apartment do
4
-
5
- describe "#config" do
6
-
7
- let(:excluded_models){ ["Company"] }
8
- let(:seed_data_file_path){ "#{Rails.root}/db/seeds/import.rb" }
6
+ describe '#config' do
7
+ let(:excluded_models) { ['Company'] }
8
+ let(:seed_data_file_path) { Rails.root.join('db', 'seeds', 'import.rb') }
9
9
 
10
10
  def tenant_names_from_array(names)
11
11
  names.each_with_object({}) do |tenant, hash|
@@ -13,21 +13,21 @@ describe Apartment do
13
13
  end.with_indifferent_access
14
14
  end
15
15
 
16
- it "should yield the Apartment object" do
16
+ it 'should yield the Apartment object' do
17
17
  Apartment.configure do |config|
18
18
  config.excluded_models = []
19
19
  expect(config).to eq(Apartment)
20
20
  end
21
21
  end
22
22
 
23
- it "should set excluded models" do
23
+ it 'should set excluded models' do
24
24
  Apartment.configure do |config|
25
25
  config.excluded_models = excluded_models
26
26
  end
27
27
  expect(Apartment.excluded_models).to eq(excluded_models)
28
28
  end
29
29
 
30
- it "should set use_schemas" do
30
+ it 'should set use_schemas' do
31
31
  Apartment.configure do |config|
32
32
  config.excluded_models = []
33
33
  config.use_schemas = false
@@ -35,14 +35,14 @@ describe Apartment do
35
35
  expect(Apartment.use_schemas).to be false
36
36
  end
37
37
 
38
- it "should set seed_data_file" do
38
+ it 'should set seed_data_file' do
39
39
  Apartment.configure do |config|
40
40
  config.seed_data_file = seed_data_file_path
41
41
  end
42
42
  expect(Apartment.seed_data_file).to eq(seed_data_file_path)
43
43
  end
44
44
 
45
- it "should set seed_after_create" do
45
+ it 'should set seed_after_create' do
46
46
  Apartment.configure do |config|
47
47
  config.excluded_models = []
48
48
  config.seed_after_create = true
@@ -50,7 +50,7 @@ describe Apartment do
50
50
  expect(Apartment.seed_after_create).to be true
51
51
  end
52
52
 
53
- context "databases" do
53
+ context 'databases' do
54
54
  let(:users_conf_hash) { { port: 5444 } }
55
55
 
56
56
  before do
@@ -59,54 +59,53 @@ describe Apartment do
59
59
  end
60
60
  end
61
61
 
62
- context "tenant_names as string array" do
63
- let(:tenant_names) { ['users', 'companies'] }
62
+ context 'tenant_names as string array' do
63
+ let(:tenant_names) { %w[users companies] }
64
64
 
65
- it "should return object if it doesnt respond_to call" do
65
+ it 'should return object if it doesnt respond_to call' do
66
66
  expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names).keys)
67
67
  end
68
68
 
69
- it "should set tenants_with_config" do
69
+ it 'should set tenants_with_config' do
70
70
  expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names))
71
71
  end
72
72
  end
73
73
 
74
- context "tenant_names as proc returning an array" do
75
- let(:tenant_names) { lambda { ['users', 'companies'] } }
74
+ context 'tenant_names as proc returning an array' do
75
+ let(:tenant_names) { -> { %w[users companies] } }
76
76
 
77
- it "should return object if it doesnt respond_to call" do
77
+ it 'should return object if it doesnt respond_to call' do
78
78
  expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names.call).keys)
79
79
  end
80
80
 
81
- it "should set tenants_with_config" do
81
+ it 'should set tenants_with_config' do
82
82
  expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names.call))
83
83
  end
84
84
  end
85
85
 
86
- context "tenant_names as Hash" do
86
+ context 'tenant_names as Hash' do
87
87
  let(:tenant_names) { { users: users_conf_hash }.with_indifferent_access }
88
88
 
89
- it "should return object if it doesnt respond_to call" do
89
+ it 'should return object if it doesnt respond_to call' do
90
90
  expect(Apartment.tenant_names).to eq(tenant_names.keys)
91
91
  end
92
92
 
93
- it "should set tenants_with_config" do
93
+ it 'should set tenants_with_config' do
94
94
  expect(Apartment.tenants_with_config).to eq(tenant_names)
95
95
  end
96
96
  end
97
97
 
98
- context "tenant_names as proc returning a Hash" do
99
- let(:tenant_names) { lambda { { users: users_conf_hash }.with_indifferent_access } }
98
+ context 'tenant_names as proc returning a Hash' do
99
+ let(:tenant_names) { -> { { users: users_conf_hash }.with_indifferent_access } }
100
100
 
101
- it "should return object if it doesnt respond_to call" do
101
+ it 'should return object if it doesnt respond_to call' do
102
102
  expect(Apartment.tenant_names).to eq(tenant_names.call.keys)
103
103
  end
104
104
 
105
- it "should set tenants_with_config" do
105
+ it 'should set tenants_with_config' do
106
106
  expect(Apartment.tenants_with_config).to eq(tenant_names.call)
107
107
  end
108
108
  end
109
109
  end
110
-
111
110
  end
112
111
  end
@@ -1,29 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'apartment/elevators/domain'
3
5
 
4
6
  describe Apartment::Elevators::Domain do
7
+ subject(:elevator) { described_class.new(proc {}) }
5
8
 
6
- subject(:elevator){ described_class.new(Proc.new{}) }
7
-
8
- describe "#parse_tenant_name" do
9
- it "parses the host for a domain name" do
9
+ describe '#parse_tenant_name' do
10
+ it 'parses the host for a domain name' do
10
11
  request = ActionDispatch::Request.new('HTTP_HOST' => 'example.com')
11
12
  expect(elevator.parse_tenant_name(request)).to eq('example')
12
13
  end
13
14
 
14
- it "ignores a www prefix and domain suffix" do
15
+ it 'ignores a www prefix and domain suffix' do
15
16
  request = ActionDispatch::Request.new('HTTP_HOST' => 'www.example.bc.ca')
16
17
  expect(elevator.parse_tenant_name(request)).to eq('example')
17
18
  end
18
19
 
19
- it "returns nil if there is no host" do
20
+ it 'returns nil if there is no host' do
20
21
  request = ActionDispatch::Request.new('HTTP_HOST' => '')
21
22
  expect(elevator.parse_tenant_name(request)).to be_nil
22
23
  end
23
24
  end
24
25
 
25
- describe "#call" do
26
- it "switches to the proper tenant" do
26
+ describe '#call' do
27
+ it 'switches to the proper tenant' do
27
28
  expect(Apartment::Tenant).to receive(:switch).with('example')
28
29
 
29
30
  elevator.call('HTTP_HOST' => 'www.example.com')
@@ -1,24 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'apartment/elevators/first_subdomain'
3
5
 
4
6
  describe Apartment::Elevators::FirstSubdomain do
5
- describe "subdomain" do
6
- subject { described_class.new("test").parse_tenant_name(request) }
7
- let(:request) { double(:request, :host => "#{subdomain}.example.com") }
7
+ describe 'subdomain' do
8
+ subject { described_class.new('test').parse_tenant_name(request) }
9
+ let(:request) { double(:request, host: "#{subdomain}.example.com") }
8
10
 
9
- context "one subdomain" do
10
- let(:subdomain) { "test" }
11
- it { is_expected.to eq("test") }
11
+ context 'one subdomain' do
12
+ let(:subdomain) { 'test' }
13
+ it { is_expected.to eq('test') }
12
14
  end
13
15
 
14
- context "nested subdomains" do
15
- let(:subdomain) { "test1.test2" }
16
- it { is_expected.to eq("test1") }
16
+ context 'nested subdomains' do
17
+ let(:subdomain) { 'test1.test2' }
18
+ it { is_expected.to eq('test1') }
17
19
  end
18
-
19
- context "no subdomain" do
20
+
21
+ context 'no subdomain' do
20
22
  let(:subdomain) { nil }
21
23
  it { is_expected.to eq(nil) }
22
24
  end
23
25
  end
24
- end
26
+ end
@@ -1,49 +1,50 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'apartment/elevators/generic'
3
5
 
4
6
  describe Apartment::Elevators::Generic do
5
-
6
7
  class MyElevator < described_class
7
8
  def parse_tenant_name(*)
8
9
  'tenant2'
9
10
  end
10
11
  end
11
12
 
12
- subject(:elevator){ described_class.new(Proc.new{}) }
13
+ subject(:elevator) { described_class.new(proc {}) }
13
14
 
14
- describe "#call" do
15
- it "calls the processor if given" do
16
- elevator = described_class.new(Proc.new{}, Proc.new{'tenant1'})
15
+ describe '#call' do
16
+ it 'calls the processor if given' do
17
+ elevator = described_class.new(proc {}, proc { 'tenant1' })
17
18
 
18
19
  expect(Apartment::Tenant).to receive(:switch).with('tenant1')
19
20
 
20
21
  elevator.call('HTTP_HOST' => 'foo.bar.com')
21
22
  end
22
23
 
23
- it "raises if parse_tenant_name not implemented" do
24
- expect {
24
+ it 'raises if parse_tenant_name not implemented' do
25
+ expect do
25
26
  elevator.call('HTTP_HOST' => 'foo.bar.com')
26
- }.to raise_error(RuntimeError)
27
+ end.to raise_error(RuntimeError)
27
28
  end
28
29
 
29
- it "switches to the parsed db_name" do
30
- elevator = MyElevator.new(Proc.new{})
30
+ it 'switches to the parsed db_name' do
31
+ elevator = MyElevator.new(proc {})
31
32
 
32
33
  expect(Apartment::Tenant).to receive(:switch).with('tenant2')
33
34
 
34
35
  elevator.call('HTTP_HOST' => 'foo.bar.com')
35
36
  end
36
37
 
37
- it "calls the block implementation of `switch`" do
38
- elevator = MyElevator.new(Proc.new{}, Proc.new{'tenant2'})
38
+ it 'calls the block implementation of `switch`' do
39
+ elevator = MyElevator.new(proc {}, proc { 'tenant2' })
39
40
 
40
41
  expect(Apartment::Tenant).to receive(:switch).with('tenant2').and_yield
41
42
  elevator.call('HTTP_HOST' => 'foo.bar.com')
42
43
  end
43
44
 
44
- it "does not call `switch` if no database given" do
45
- app = Proc.new{}
46
- elevator = MyElevator.new(app, Proc.new{})
45
+ it 'does not call `switch` if no database given' do
46
+ app = proc {}
47
+ elevator = MyElevator.new(app, proc {})
47
48
 
48
49
  expect(Apartment::Tenant).not_to receive(:switch)
49
50
  expect(app).to receive :call