ros-apartment 2.3.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/.github/ISSUE_TEMPLATE.md +21 -0
  3. data/.gitignore +15 -0
  4. data/.pryrc +3 -0
  5. data/.rspec +4 -0
  6. data/.travis.yml +65 -0
  7. data/Appraisals +71 -0
  8. data/Gemfile +10 -0
  9. data/Guardfile +24 -0
  10. data/HISTORY.md +398 -0
  11. data/README.md +576 -0
  12. data/Rakefile +128 -0
  13. data/TODO.md +51 -0
  14. data/apartment.gemspec +46 -0
  15. data/docker-compose.yml +33 -0
  16. data/gemfiles/rails_4_2.gemfile +23 -0
  17. data/gemfiles/rails_5_0.gemfile +22 -0
  18. data/gemfiles/rails_5_1.gemfile +22 -0
  19. data/gemfiles/rails_5_2.gemfile +18 -0
  20. data/gemfiles/rails_6_0.gemfile +22 -0
  21. data/gemfiles/rails_master.gemfile +22 -0
  22. data/lib/apartment.rb +118 -0
  23. data/lib/apartment/adapters/abstract_adapter.rb +269 -0
  24. data/lib/apartment/adapters/abstract_jdbc_adapter.rb +18 -0
  25. data/lib/apartment/adapters/jdbc_mysql_adapter.rb +19 -0
  26. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +56 -0
  27. data/lib/apartment/adapters/mysql2_adapter.rb +71 -0
  28. data/lib/apartment/adapters/postgis_adapter.rb +12 -0
  29. data/lib/apartment/adapters/postgresql_adapter.rb +236 -0
  30. data/lib/apartment/adapters/sqlite3_adapter.rb +56 -0
  31. data/lib/apartment/console.rb +12 -0
  32. data/lib/apartment/deprecation.rb +10 -0
  33. data/lib/apartment/elevators/domain.rb +22 -0
  34. data/lib/apartment/elevators/first_subdomain.rb +17 -0
  35. data/lib/apartment/elevators/generic.rb +32 -0
  36. data/lib/apartment/elevators/host.rb +30 -0
  37. data/lib/apartment/elevators/host_hash.rb +22 -0
  38. data/lib/apartment/elevators/subdomain.rb +62 -0
  39. data/lib/apartment/migrator.rb +51 -0
  40. data/lib/apartment/railtie.rb +67 -0
  41. data/lib/apartment/reloader.rb +21 -0
  42. data/lib/apartment/tasks/enhancements.rb +57 -0
  43. data/lib/apartment/tenant.rb +66 -0
  44. data/lib/apartment/version.rb +3 -0
  45. data/lib/generators/apartment/install/USAGE +5 -0
  46. data/lib/generators/apartment/install/install_generator.rb +10 -0
  47. data/lib/generators/apartment/install/templates/apartment.rb +109 -0
  48. data/lib/tasks/apartment.rake +145 -0
  49. data/spec/adapters/jdbc_mysql_adapter_spec.rb +19 -0
  50. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +41 -0
  51. data/spec/adapters/mysql2_adapter_spec.rb +59 -0
  52. data/spec/adapters/postgresql_adapter_spec.rb +61 -0
  53. data/spec/adapters/sqlite3_adapter_spec.rb +83 -0
  54. data/spec/apartment_spec.rb +11 -0
  55. data/spec/config/database.yml.sample +49 -0
  56. data/spec/dummy/Rakefile +7 -0
  57. data/spec/dummy/app/controllers/application_controller.rb +6 -0
  58. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  59. data/spec/dummy/app/models/company.rb +3 -0
  60. data/spec/dummy/app/models/user.rb +3 -0
  61. data/spec/dummy/app/views/application/index.html.erb +1 -0
  62. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  63. data/spec/dummy/config.ru +4 -0
  64. data/spec/dummy/config/application.rb +49 -0
  65. data/spec/dummy/config/boot.rb +11 -0
  66. data/spec/dummy/config/database.yml.sample +44 -0
  67. data/spec/dummy/config/environment.rb +5 -0
  68. data/spec/dummy/config/environments/development.rb +28 -0
  69. data/spec/dummy/config/environments/production.rb +51 -0
  70. data/spec/dummy/config/environments/test.rb +34 -0
  71. data/spec/dummy/config/initializers/apartment.rb +4 -0
  72. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  73. data/spec/dummy/config/initializers/inflections.rb +10 -0
  74. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  75. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  76. data/spec/dummy/config/initializers/session_store.rb +8 -0
  77. data/spec/dummy/config/locales/en.yml +5 -0
  78. data/spec/dummy/config/routes.rb +3 -0
  79. data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +39 -0
  80. data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +14 -0
  81. data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +13 -0
  82. data/spec/dummy/db/schema.rb +55 -0
  83. data/spec/dummy/db/seeds.rb +5 -0
  84. data/spec/dummy/db/seeds/import.rb +5 -0
  85. data/spec/dummy/public/404.html +26 -0
  86. data/spec/dummy/public/422.html +26 -0
  87. data/spec/dummy/public/500.html +26 -0
  88. data/spec/dummy/public/favicon.ico +0 -0
  89. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  90. data/spec/dummy/script/rails +6 -0
  91. data/spec/dummy_engine/.gitignore +8 -0
  92. data/spec/dummy_engine/Gemfile +15 -0
  93. data/spec/dummy_engine/Rakefile +34 -0
  94. data/spec/dummy_engine/bin/rails +12 -0
  95. data/spec/dummy_engine/config/initializers/apartment.rb +51 -0
  96. data/spec/dummy_engine/dummy_engine.gemspec +24 -0
  97. data/spec/dummy_engine/lib/dummy_engine.rb +4 -0
  98. data/spec/dummy_engine/lib/dummy_engine/engine.rb +4 -0
  99. data/spec/dummy_engine/lib/dummy_engine/version.rb +3 -0
  100. data/spec/dummy_engine/test/dummy/Rakefile +6 -0
  101. data/spec/dummy_engine/test/dummy/config.ru +4 -0
  102. data/spec/dummy_engine/test/dummy/config/application.rb +22 -0
  103. data/spec/dummy_engine/test/dummy/config/boot.rb +5 -0
  104. data/spec/dummy_engine/test/dummy/config/database.yml +25 -0
  105. data/spec/dummy_engine/test/dummy/config/environment.rb +5 -0
  106. data/spec/dummy_engine/test/dummy/config/environments/development.rb +37 -0
  107. data/spec/dummy_engine/test/dummy/config/environments/production.rb +78 -0
  108. data/spec/dummy_engine/test/dummy/config/environments/test.rb +39 -0
  109. data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +8 -0
  110. data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  111. data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  112. data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  113. data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +16 -0
  114. data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +4 -0
  115. data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +3 -0
  116. data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  117. data/spec/dummy_engine/test/dummy/config/locales/en.yml +23 -0
  118. data/spec/dummy_engine/test/dummy/config/routes.rb +56 -0
  119. data/spec/dummy_engine/test/dummy/config/secrets.yml +22 -0
  120. data/spec/examples/connection_adapter_examples.rb +42 -0
  121. data/spec/examples/generic_adapter_custom_configuration_example.rb +95 -0
  122. data/spec/examples/generic_adapter_examples.rb +163 -0
  123. data/spec/examples/schema_adapter_examples.rb +234 -0
  124. data/spec/integration/apartment_rake_integration_spec.rb +107 -0
  125. data/spec/integration/query_caching_spec.rb +81 -0
  126. data/spec/integration/use_within_an_engine_spec.rb +28 -0
  127. data/spec/schemas/v1.rb +16 -0
  128. data/spec/schemas/v2.rb +43 -0
  129. data/spec/schemas/v3.rb +49 -0
  130. data/spec/spec_helper.rb +61 -0
  131. data/spec/support/apartment_helpers.rb +43 -0
  132. data/spec/support/capybara_sessions.rb +15 -0
  133. data/spec/support/config.rb +10 -0
  134. data/spec/support/contexts.rb +52 -0
  135. data/spec/support/requirements.rb +35 -0
  136. data/spec/support/setup.rb +46 -0
  137. data/spec/tasks/apartment_rake_spec.rb +129 -0
  138. data/spec/tenant_spec.rb +190 -0
  139. data/spec/unit/config_spec.rb +112 -0
  140. data/spec/unit/elevators/domain_spec.rb +32 -0
  141. data/spec/unit/elevators/first_subdomain_spec.rb +24 -0
  142. data/spec/unit/elevators/generic_spec.rb +54 -0
  143. data/spec/unit/elevators/host_hash_spec.rb +32 -0
  144. data/spec/unit/elevators/host_spec.rb +89 -0
  145. data/spec/unit/elevators/subdomain_spec.rb +76 -0
  146. data/spec/unit/migrator_spec.rb +77 -0
  147. data/spec/unit/reloader_spec.rb +24 -0
  148. metadata +487 -0
@@ -0,0 +1,236 @@
1
+ require 'apartment/adapters/abstract_adapter'
2
+
3
+ module Apartment
4
+ module Tenant
5
+
6
+ def self.postgresql_adapter(config)
7
+ adapter = Adapters::PostgresqlAdapter
8
+ adapter = Adapters::PostgresqlSchemaAdapter if Apartment.use_schemas
9
+ adapter = Adapters::PostgresqlSchemaFromSqlAdapter if Apartment.use_sql && Apartment.use_schemas
10
+ adapter.new(config)
11
+ end
12
+ end
13
+
14
+ module Adapters
15
+ # Default adapter when not using Postgresql Schemas
16
+ class PostgresqlAdapter < AbstractAdapter
17
+
18
+ private
19
+
20
+ def rescue_from
21
+ PG::Error
22
+ end
23
+ end
24
+
25
+ # Separate Adapter for Postgresql when using schemas
26
+ class PostgresqlSchemaAdapter < AbstractAdapter
27
+
28
+ def initialize(config)
29
+ super
30
+
31
+ reset
32
+ end
33
+
34
+ # Reset schema search path to the default schema_search_path
35
+ #
36
+ # @return {String} default schema search path
37
+ #
38
+ def reset
39
+ @current = default_tenant
40
+ Apartment.connection.schema_search_path = full_search_path
41
+ end
42
+
43
+ def current
44
+ @current || default_tenant
45
+ end
46
+
47
+ protected
48
+
49
+ def process_excluded_model(excluded_model)
50
+ excluded_model.constantize.tap do |klass|
51
+ # Ensure that if a schema *was* set, we override
52
+ table_name = klass.table_name.split('.', 2).last
53
+
54
+ klass.table_name = "#{default_tenant}.#{table_name}"
55
+ end
56
+ end
57
+
58
+ def drop_command(conn, tenant)
59
+ conn.execute(%{DROP SCHEMA "#{tenant}" CASCADE})
60
+ end
61
+
62
+ # Set schema search path to new schema
63
+ #
64
+ def connect_to_new(tenant = nil)
65
+ return reset if tenant.nil?
66
+ raise ActiveRecord::StatementInvalid.new("Could not find schema #{tenant}") unless Apartment.connection.schema_exists?(tenant.to_s)
67
+
68
+ @current = tenant.to_s
69
+ Apartment.connection.schema_search_path = full_search_path
70
+
71
+ # When the PostgreSQL version is < 9.3,
72
+ # there is a issue for prepared statement with changing search_path.
73
+ # https://www.postgresql.org/docs/9.3/static/sql-prepare.html
74
+ if postgresql_version < 90300
75
+ Apartment.connection.clear_cache!
76
+ end
77
+
78
+ rescue *rescuable_exceptions
79
+ raise TenantNotFound, "One of the following schema(s) is invalid: \"#{tenant}\" #{full_search_path}"
80
+ end
81
+
82
+ private
83
+
84
+ def create_tenant_command(conn, tenant)
85
+ conn.execute(%{CREATE SCHEMA "#{tenant}"})
86
+ end
87
+
88
+ # Generate the final search path to set including persistent_schemas
89
+ #
90
+ def full_search_path
91
+ persistent_schemas.map(&:inspect).join(", ")
92
+ end
93
+
94
+ def persistent_schemas
95
+ [@current, Apartment.persistent_schemas].flatten
96
+ end
97
+
98
+ def postgresql_version
99
+ # ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#postgresql_version is
100
+ # public from Rails 5.0.
101
+ Apartment.connection.send(:postgresql_version)
102
+ end
103
+ end
104
+
105
+ # Another Adapter for Postgresql when using schemas and SQL
106
+ class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter
107
+
108
+ PSQL_DUMP_BLACKLISTED_STATEMENTS= [
109
+ /SET search_path/i, # overridden later
110
+ /SET lock_timeout/i, # new in postgresql 9.3
111
+ /SET row_security/i, # new in postgresql 9.5
112
+ /SET idle_in_transaction_session_timeout/i, # new in postgresql 9.6
113
+ /CREATE SCHEMA public/i,
114
+ /COMMENT ON SCHEMA public/i,
115
+
116
+ ]
117
+
118
+ def import_database_schema
119
+ preserving_search_path do
120
+ clone_pg_schema
121
+ copy_schema_migrations
122
+ end
123
+ end
124
+
125
+ private
126
+
127
+ # Re-set search path after the schema is imported.
128
+ # Postgres now sets search path to empty before dumping the schema
129
+ # and it mut be reset
130
+ #
131
+ def preserving_search_path
132
+ search_path = Apartment.connection.execute("show search_path").first["search_path"]
133
+ yield
134
+ Apartment.connection.execute("set search_path = #{search_path}")
135
+ end
136
+
137
+ # Clone default schema into new schema named after current tenant
138
+ #
139
+ def clone_pg_schema
140
+ pg_schema_sql = patch_search_path(pg_dump_schema)
141
+ Apartment.connection.execute(pg_schema_sql)
142
+ end
143
+
144
+ # Copy data from schema_migrations into new schema
145
+ #
146
+ def copy_schema_migrations
147
+ pg_migrations_data = patch_search_path(pg_dump_schema_migrations_data)
148
+ Apartment.connection.execute(pg_migrations_data)
149
+ end
150
+
151
+ # Dump postgres default schema
152
+ #
153
+ # @return {String} raw SQL contaning only postgres schema dump
154
+ #
155
+ def pg_dump_schema
156
+
157
+ # Skip excluded tables? :/
158
+ # excluded_tables =
159
+ # collect_table_names(Apartment.excluded_models)
160
+ # .map! {|t| "-T #{t}"}
161
+ # .join(' ')
162
+
163
+ # `pg_dump -s -x -O -n #{default_tenant} #{excluded_tables} #{dbname}`
164
+
165
+ with_pg_env { `pg_dump -s -x -O -n #{default_tenant} #{dbname}` }
166
+ end
167
+
168
+ # Dump data from schema_migrations table
169
+ #
170
+ # @return {String} raw SQL contaning inserts with data from schema_migrations
171
+ #
172
+ def pg_dump_schema_migrations_data
173
+ with_pg_env { `pg_dump -a --inserts -t #{default_tenant}.schema_migrations -t #{default_tenant}.ar_internal_metadata #{dbname}` }
174
+ end
175
+
176
+ # Temporary set Postgresql related environment variables if there are in @config
177
+ #
178
+ def with_pg_env(&block)
179
+ pghost, pgport, pguser, pgpassword = ENV['PGHOST'], ENV['PGPORT'], ENV['PGUSER'], ENV['PGPASSWORD']
180
+
181
+ ENV['PGHOST'] = @config[:host] if @config[:host]
182
+ ENV['PGPORT'] = @config[:port].to_s if @config[:port]
183
+ ENV['PGUSER'] = @config[:username].to_s if @config[:username]
184
+ ENV['PGPASSWORD'] = @config[:password].to_s if @config[:password]
185
+
186
+ block.call
187
+ ensure
188
+ ENV['PGHOST'], ENV['PGPORT'], ENV['PGUSER'], ENV['PGPASSWORD'] = pghost, pgport, pguser, pgpassword
189
+ end
190
+
191
+ # Remove "SET search_path ..." line from SQL dump and prepend search_path set to current tenant
192
+ #
193
+ # @return {String} patched raw SQL dump
194
+ #
195
+ def patch_search_path(sql)
196
+ search_path = "SET search_path = \"#{current}\", #{default_tenant};"
197
+
198
+ swap_schema_qualifier(sql)
199
+ .split("\n")
200
+ .select {|line| check_input_against_regexps(line, PSQL_DUMP_BLACKLISTED_STATEMENTS).empty?}
201
+ .prepend(search_path)
202
+ .join("\n")
203
+ end
204
+
205
+ def swap_schema_qualifier(sql)
206
+ sql.gsub(/#{default_tenant}\.\w*/) do |match|
207
+ if Apartment.pg_excluded_names.any? { |name| match.include? name }
208
+ match
209
+ else
210
+ match.gsub("#{default_tenant}.", %{"#{current}".})
211
+ end
212
+ end
213
+ end
214
+
215
+ # Checks if any of regexps matches against input
216
+ #
217
+ def check_input_against_regexps(input, regexps)
218
+ regexps.select {|c| input.match c}
219
+ end
220
+
221
+ # Collect table names from AR Models
222
+ #
223
+ def collect_table_names(models)
224
+ models.map do |m|
225
+ m.constantize.table_name
226
+ end
227
+ end
228
+
229
+ # Convenience method for current database name
230
+ #
231
+ def dbname
232
+ Apartment.connection_config[:database]
233
+ end
234
+ end
235
+ end
236
+ end
@@ -0,0 +1,56 @@
1
+ require 'apartment/adapters/abstract_adapter'
2
+
3
+ module Apartment
4
+ module Tenant
5
+ def self.sqlite3_adapter(config)
6
+ Adapters::Sqlite3Adapter.new(config)
7
+ end
8
+ end
9
+
10
+ module Adapters
11
+ class Sqlite3Adapter < AbstractAdapter
12
+ def initialize(config)
13
+ @default_dir = File.expand_path(File.dirname(config[:database]))
14
+
15
+ super
16
+ end
17
+
18
+ def drop(tenant)
19
+ raise TenantNotFound,
20
+ "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant))
21
+
22
+ File.delete(database_file(tenant))
23
+ end
24
+
25
+ def current
26
+ File.basename(Apartment.connection.instance_variable_get(:@config)[:database], '.sqlite3')
27
+ end
28
+
29
+ protected
30
+
31
+ def connect_to_new(tenant)
32
+ raise TenantNotFound,
33
+ "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant))
34
+
35
+ super database_file(tenant)
36
+ end
37
+
38
+ def create_tenant(tenant)
39
+ raise TenantExists,
40
+ "The tenant #{environmentify(tenant)} already exists." if File.exists?(database_file(tenant))
41
+
42
+ begin
43
+ f = File.new(database_file(tenant), File::CREAT)
44
+ ensure
45
+ f.close
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def database_file(tenant)
52
+ "#{@default_dir}/#{environmentify(tenant)}.sqlite3"
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,12 @@
1
+ # A workaraound to get `reload!` to also call Apartment::Tenant.init
2
+ # This is unfortunate, but I haven't figured out how to hook into the reload process *after* files are reloaded
3
+
4
+ # reloads the environment
5
+ def reload!(print=true)
6
+ puts "Reloading..." if print
7
+ # This triggers the to_prepare callbacks
8
+ ActionDispatch::Callbacks.new(Proc.new {}).call({})
9
+ # Manually init Apartment again once classes are reloaded
10
+ Apartment::Tenant.init
11
+ true
12
+ end
@@ -0,0 +1,10 @@
1
+ require 'active_support/deprecation'
2
+
3
+ module Apartment
4
+ module Deprecation
5
+
6
+ def self.warn(message)
7
+ ActiveSupport::Deprecation.warn message
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,22 @@
1
+ require 'apartment/elevators/generic'
2
+
3
+ module Apartment
4
+ module Elevators
5
+ # Provides a rack based tenant switching solution based on domain
6
+ # Assumes that tenant name should match domain
7
+ # Parses request host for second level domain, ignoring www
8
+ # eg. example.com => example
9
+ # www.example.bc.ca => example
10
+ # a.example.bc.ca => a
11
+ #
12
+ #
13
+ class Domain < Generic
14
+
15
+ def parse_tenant_name(request)
16
+ return nil if request.host.blank?
17
+
18
+ request.host.match(/(www\.)?(?<sld>[^.]*)/)["sld"]
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ require 'apartment/elevators/subdomain'
2
+
3
+ module Apartment
4
+ module Elevators
5
+ # Provides a rack based tenant switching solution based on the first subdomain
6
+ # of a given domain name.
7
+ # eg:
8
+ # - example1.domain.com => example1
9
+ # - example2.something.domain.com => example2
10
+ class FirstSubdomain < Subdomain
11
+
12
+ def parse_tenant_name(request)
13
+ super.split('.')[0] unless super.nil?
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ require 'rack/request'
2
+ require 'apartment/tenant'
3
+
4
+ module Apartment
5
+ module Elevators
6
+ # Provides a rack based tenant switching solution based on request
7
+ #
8
+ class Generic
9
+
10
+ def initialize(app, processor = nil)
11
+ @app = app
12
+ @processor = processor || method(:parse_tenant_name)
13
+ end
14
+
15
+ def call(env)
16
+ request = Rack::Request.new(env)
17
+
18
+ database = @processor.call(request)
19
+
20
+ if database
21
+ Apartment::Tenant.switch(database) { @app.call(env) }
22
+ else
23
+ @app.call(env)
24
+ end
25
+ end
26
+
27
+ def parse_tenant_name(request)
28
+ raise "Override"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ require 'apartment/elevators/generic'
2
+
3
+ module Apartment
4
+ module Elevators
5
+ # Provides a rack based tenant switching solution based on the host
6
+ # Assumes that tenant name should match host
7
+ # Strips/ignores first subdomains in ignored_first_subdomains
8
+ # eg. example.com => example.com
9
+ # www.example.bc.ca => www.example.bc.ca
10
+ # if ignored_first_subdomains = ['www']
11
+ # www.example.bc.ca => example.bc.ca
12
+ # www.a.b.c.d.com => a.b.c.d.com
13
+ #
14
+ class Host < Generic
15
+ def self.ignored_first_subdomains
16
+ @ignored_first_subdomains ||= []
17
+ end
18
+
19
+ def self.ignored_first_subdomains=(arg)
20
+ @ignored_first_subdomains = arg
21
+ end
22
+
23
+ def parse_tenant_name(request)
24
+ return nil if request.host.blank?
25
+ parts = request.host.split('.')
26
+ self.class.ignored_first_subdomains.include?(parts[0]) ? parts.drop(1).join('.') : request.host
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ require 'apartment/elevators/generic'
2
+
3
+ module Apartment
4
+ module Elevators
5
+ # Provides a rack based tenant switching solution based on hosts
6
+ # Uses a hash to find the corresponding tenant name for the host
7
+ #
8
+ class HostHash < Generic
9
+ def initialize(app, hash = {}, processor = nil)
10
+ super app, processor
11
+ @hash = hash
12
+ end
13
+
14
+ def parse_tenant_name(request)
15
+ raise TenantNotFound,
16
+ "Cannot find tenant for host #{request.host}" unless @hash.has_key?(request.host)
17
+
18
+ @hash[request.host]
19
+ end
20
+ end
21
+ end
22
+ end