ros-apartment 2.3.0 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/.pryrc +5 -3
  3. data/.rubocop.yml +22 -0
  4. data/.rubocop_todo.yml +29 -0
  5. data/.story_branch.yml +5 -0
  6. data/.travis.yml +20 -36
  7. data/Appraisals +16 -29
  8. data/Gemfile +5 -2
  9. data/Guardfile +3 -1
  10. data/HISTORY.md +57 -0
  11. data/README.md +64 -21
  12. data/Rakefile +36 -22
  13. data/TODO.md +0 -1
  14. data/apartment.gemspec +17 -10
  15. data/gemfiles/rails_4_2.gemfile +12 -10
  16. data/gemfiles/rails_5_0.gemfile +2 -1
  17. data/gemfiles/rails_5_1.gemfile +2 -1
  18. data/gemfiles/rails_5_2.gemfile +2 -1
  19. data/gemfiles/rails_6_0.gemfile +6 -5
  20. data/gemfiles/rails_master.gemfile +2 -1
  21. data/lib/apartment.rb +38 -14
  22. data/lib/apartment/active_record/connection_handling.rb +17 -0
  23. data/lib/apartment/active_record/internal_metadata.rb +11 -0
  24. data/lib/apartment/active_record/log_subscriber.rb +41 -0
  25. data/lib/apartment/active_record/schema_migration.rb +13 -0
  26. data/lib/apartment/adapters/abstract_adapter.rb +49 -45
  27. data/lib/apartment/adapters/abstract_jdbc_adapter.rb +4 -3
  28. data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
  29. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +20 -13
  30. data/lib/apartment/adapters/mysql2_adapter.rb +12 -9
  31. data/lib/apartment/adapters/postgis_adapter.rb +3 -2
  32. data/lib/apartment/adapters/postgresql_adapter.rb +59 -27
  33. data/lib/apartment/adapters/sqlite3_adapter.rb +18 -8
  34. data/lib/apartment/console.rb +35 -3
  35. data/lib/apartment/custom_console.rb +42 -0
  36. data/lib/apartment/deprecation.rb +2 -1
  37. data/lib/apartment/elevators/domain.rb +4 -3
  38. data/lib/apartment/elevators/first_subdomain.rb +3 -2
  39. data/lib/apartment/elevators/generic.rb +4 -3
  40. data/lib/apartment/elevators/host.rb +6 -1
  41. data/lib/apartment/elevators/host_hash.rb +6 -2
  42. data/lib/apartment/elevators/subdomain.rb +9 -5
  43. data/lib/apartment/migrator.rb +4 -3
  44. data/lib/apartment/model.rb +27 -0
  45. data/lib/apartment/railtie.rb +27 -15
  46. data/lib/apartment/reloader.rb +2 -1
  47. data/lib/apartment/tasks/enhancements.rb +4 -6
  48. data/lib/apartment/tasks/task_helper.rb +35 -0
  49. data/lib/apartment/tenant.rb +19 -9
  50. data/lib/apartment/version.rb +3 -1
  51. data/lib/generators/apartment/install/install_generator.rb +4 -3
  52. data/lib/generators/apartment/install/templates/apartment.rb +8 -2
  53. data/lib/tasks/apartment.rake +22 -44
  54. metadata +51 -230
  55. data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -19
  56. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -41
  57. data/spec/adapters/mysql2_adapter_spec.rb +0 -59
  58. data/spec/adapters/postgresql_adapter_spec.rb +0 -61
  59. data/spec/adapters/sqlite3_adapter_spec.rb +0 -83
  60. data/spec/apartment_spec.rb +0 -11
  61. data/spec/config/database.yml.sample +0 -49
  62. data/spec/dummy/Rakefile +0 -7
  63. data/spec/dummy/app/controllers/application_controller.rb +0 -6
  64. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  65. data/spec/dummy/app/models/company.rb +0 -3
  66. data/spec/dummy/app/models/user.rb +0 -3
  67. data/spec/dummy/app/views/application/index.html.erb +0 -1
  68. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  69. data/spec/dummy/config.ru +0 -4
  70. data/spec/dummy/config/application.rb +0 -49
  71. data/spec/dummy/config/boot.rb +0 -11
  72. data/spec/dummy/config/database.yml.sample +0 -44
  73. data/spec/dummy/config/environment.rb +0 -5
  74. data/spec/dummy/config/environments/development.rb +0 -28
  75. data/spec/dummy/config/environments/production.rb +0 -51
  76. data/spec/dummy/config/environments/test.rb +0 -34
  77. data/spec/dummy/config/initializers/apartment.rb +0 -4
  78. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  79. data/spec/dummy/config/initializers/inflections.rb +0 -10
  80. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  81. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  82. data/spec/dummy/config/initializers/session_store.rb +0 -8
  83. data/spec/dummy/config/locales/en.yml +0 -5
  84. data/spec/dummy/config/routes.rb +0 -3
  85. data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
  86. data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
  87. data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
  88. data/spec/dummy/db/schema.rb +0 -55
  89. data/spec/dummy/db/seeds.rb +0 -5
  90. data/spec/dummy/db/seeds/import.rb +0 -5
  91. data/spec/dummy/public/404.html +0 -26
  92. data/spec/dummy/public/422.html +0 -26
  93. data/spec/dummy/public/500.html +0 -26
  94. data/spec/dummy/public/favicon.ico +0 -0
  95. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  96. data/spec/dummy/script/rails +0 -6
  97. data/spec/dummy_engine/.gitignore +0 -8
  98. data/spec/dummy_engine/Gemfile +0 -15
  99. data/spec/dummy_engine/Rakefile +0 -34
  100. data/spec/dummy_engine/bin/rails +0 -12
  101. data/spec/dummy_engine/config/initializers/apartment.rb +0 -51
  102. data/spec/dummy_engine/dummy_engine.gemspec +0 -24
  103. data/spec/dummy_engine/lib/dummy_engine.rb +0 -4
  104. data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -4
  105. data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -3
  106. data/spec/dummy_engine/test/dummy/Rakefile +0 -6
  107. data/spec/dummy_engine/test/dummy/config.ru +0 -4
  108. data/spec/dummy_engine/test/dummy/config/application.rb +0 -22
  109. data/spec/dummy_engine/test/dummy/config/boot.rb +0 -5
  110. data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
  111. data/spec/dummy_engine/test/dummy/config/environment.rb +0 -5
  112. data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -37
  113. data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -78
  114. data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -39
  115. data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -8
  116. data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  117. data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  118. data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  119. data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -16
  120. data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -4
  121. data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -3
  122. data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  123. data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
  124. data/spec/dummy_engine/test/dummy/config/routes.rb +0 -56
  125. data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
  126. data/spec/examples/connection_adapter_examples.rb +0 -42
  127. data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -95
  128. data/spec/examples/generic_adapter_examples.rb +0 -163
  129. data/spec/examples/schema_adapter_examples.rb +0 -234
  130. data/spec/integration/apartment_rake_integration_spec.rb +0 -107
  131. data/spec/integration/query_caching_spec.rb +0 -81
  132. data/spec/integration/use_within_an_engine_spec.rb +0 -28
  133. data/spec/schemas/v1.rb +0 -16
  134. data/spec/schemas/v2.rb +0 -43
  135. data/spec/schemas/v3.rb +0 -49
  136. data/spec/spec_helper.rb +0 -61
  137. data/spec/support/apartment_helpers.rb +0 -43
  138. data/spec/support/capybara_sessions.rb +0 -15
  139. data/spec/support/config.rb +0 -10
  140. data/spec/support/contexts.rb +0 -52
  141. data/spec/support/requirements.rb +0 -35
  142. data/spec/support/setup.rb +0 -46
  143. data/spec/tasks/apartment_rake_spec.rb +0 -129
  144. data/spec/tenant_spec.rb +0 -190
  145. data/spec/unit/config_spec.rb +0 -112
  146. data/spec/unit/elevators/domain_spec.rb +0 -32
  147. data/spec/unit/elevators/first_subdomain_spec.rb +0 -24
  148. data/spec/unit/elevators/generic_spec.rb +0 -54
  149. data/spec/unit/elevators/host_hash_spec.rb +0 -32
  150. data/spec/unit/elevators/host_spec.rb +0 -89
  151. data/spec/unit/elevators/subdomain_spec.rb +0 -76
  152. data/spec/unit/migrator_spec.rb +0 -77
  153. data/spec/unit/reloader_spec.rb +0 -24
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # handle postgis adapter as if it were postgresql,
2
4
  # only override the adapter_method used for initialization
3
- require "apartment/adapters/postgresql_adapter"
5
+ require 'apartment/adapters/postgresql_adapter'
4
6
 
5
7
  module Apartment
6
8
  module Tenant
7
-
8
9
  def self.postgis_adapter(config)
9
10
  postgresql_adapter(config)
10
11
  end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'apartment/adapters/abstract_adapter'
2
4
 
3
5
  module Apartment
4
6
  module Tenant
5
-
6
7
  def self.postgresql_adapter(config)
7
8
  adapter = Adapters::PostgresqlAdapter
8
9
  adapter = Adapters::PostgresqlSchemaAdapter if Apartment.use_schemas
@@ -14,8 +15,7 @@ module Apartment
14
15
  module Adapters
15
16
  # Default adapter when not using Postgresql Schemas
16
17
  class PostgresqlAdapter < AbstractAdapter
17
-
18
- private
18
+ private
19
19
 
20
20
  def rescue_from
21
21
  PG::Error
@@ -24,13 +24,16 @@ module Apartment
24
24
 
25
25
  # Separate Adapter for Postgresql when using schemas
26
26
  class PostgresqlSchemaAdapter < AbstractAdapter
27
-
28
27
  def initialize(config)
29
28
  super
30
29
 
31
30
  reset
32
31
  end
33
32
 
33
+ def default_tenant
34
+ @default_tenant = Apartment.default_tenant || 'public'
35
+ end
36
+
34
37
  # Reset schema search path to the default schema_search_path
35
38
  #
36
39
  # @return {String} default schema search path
@@ -38,13 +41,19 @@ module Apartment
38
41
  def reset
39
42
  @current = default_tenant
40
43
  Apartment.connection.schema_search_path = full_search_path
44
+ reset_sequence_names
45
+ end
46
+
47
+ def init
48
+ super
49
+ Apartment.connection.schema_search_path = full_search_path
41
50
  end
42
51
 
43
52
  def current
44
53
  @current || default_tenant
45
54
  end
46
55
 
47
- protected
56
+ protected
48
57
 
49
58
  def process_excluded_model(excluded_model)
50
59
  excluded_model.constantize.tap do |klass|
@@ -56,39 +65,45 @@ module Apartment
56
65
  end
57
66
 
58
67
  def drop_command(conn, tenant)
59
- conn.execute(%{DROP SCHEMA "#{tenant}" CASCADE})
68
+ conn.execute(%(DROP SCHEMA "#{tenant}" CASCADE))
60
69
  end
61
70
 
62
71
  # Set schema search path to new schema
63
72
  #
64
73
  def connect_to_new(tenant = nil)
65
74
  return reset if tenant.nil?
66
- raise ActiveRecord::StatementInvalid.new("Could not find schema #{tenant}") unless Apartment.connection.schema_exists?(tenant.to_s)
67
75
 
68
- @current = tenant.to_s
76
+ tenant = tenant.to_s
77
+ raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant)
78
+
79
+ @current = tenant
69
80
  Apartment.connection.schema_search_path = full_search_path
70
81
 
71
82
  # When the PostgreSQL version is < 9.3,
72
83
  # there is a issue for prepared statement with changing search_path.
73
84
  # https://www.postgresql.org/docs/9.3/static/sql-prepare.html
74
- if postgresql_version < 90300
75
- Apartment.connection.clear_cache!
76
- end
77
-
85
+ Apartment.connection.clear_cache! if postgresql_version < 90_300
86
+ reset_sequence_names
78
87
  rescue *rescuable_exceptions
79
88
  raise TenantNotFound, "One of the following schema(s) is invalid: \"#{tenant}\" #{full_search_path}"
80
89
  end
81
90
 
82
- private
91
+ private
92
+
93
+ def tenant_exists?(tenant)
94
+ return true unless Apartment.tenant_presence_check
95
+
96
+ Apartment.connection.schema_exists?(tenant)
97
+ end
83
98
 
84
99
  def create_tenant_command(conn, tenant)
85
- conn.execute(%{CREATE SCHEMA "#{tenant}"})
100
+ conn.execute(%(CREATE SCHEMA "#{tenant}"))
86
101
  end
87
102
 
88
103
  # Generate the final search path to set including persistent_schemas
89
104
  #
90
105
  def full_search_path
91
- persistent_schemas.map(&:inspect).join(", ")
106
+ persistent_schemas.map(&:inspect).join(', ')
92
107
  end
93
108
 
94
109
  def persistent_schemas
@@ -100,20 +115,32 @@ module Apartment
100
115
  # public from Rails 5.0.
101
116
  Apartment.connection.send(:postgresql_version)
102
117
  end
118
+
119
+ def reset_sequence_names
120
+ # sequence_name contains the schema, so it must be reset after switch
121
+ # There is `reset_sequence_name`, but that method actually goes to the database
122
+ # to find out the new name. Therefore, we do this hack to only unset the name,
123
+ # and it will be dynamically found the next time it is needed
124
+ ActiveRecord::Base.descendants
125
+ .select { |c| c.instance_variable_defined?(:@sequence_name) }
126
+ .reject { |c| c.instance_variable_defined?(:@explicit_sequence_name) && c.instance_variable_get(:@explicit_sequence_name) }
127
+ .each do |c|
128
+ c.remove_instance_variable :@sequence_name
129
+ end
130
+ end
103
131
  end
104
132
 
105
133
  # Another Adapter for Postgresql when using schemas and SQL
106
134
  class PostgresqlSchemaFromSqlAdapter < PostgresqlSchemaAdapter
107
-
108
- PSQL_DUMP_BLACKLISTED_STATEMENTS= [
135
+ PSQL_DUMP_BLACKLISTED_STATEMENTS = [
109
136
  /SET search_path/i, # overridden later
110
137
  /SET lock_timeout/i, # new in postgresql 9.3
111
138
  /SET row_security/i, # new in postgresql 9.5
112
139
  /SET idle_in_transaction_session_timeout/i, # new in postgresql 9.6
113
140
  /CREATE SCHEMA public/i,
114
- /COMMENT ON SCHEMA public/i,
141
+ /COMMENT ON SCHEMA public/i
115
142
 
116
- ]
143
+ ].freeze
117
144
 
118
145
  def import_database_schema
119
146
  preserving_search_path do
@@ -122,14 +149,14 @@ module Apartment
122
149
  end
123
150
  end
124
151
 
125
- private
152
+ private
126
153
 
127
154
  # Re-set search path after the schema is imported.
128
155
  # Postgres now sets search path to empty before dumping the schema
129
156
  # and it mut be reset
130
157
  #
131
158
  def preserving_search_path
132
- search_path = Apartment.connection.execute("show search_path").first["search_path"]
159
+ search_path = Apartment.connection.execute('show search_path').first['search_path']
133
160
  yield
134
161
  Apartment.connection.execute("set search_path = #{search_path}")
135
162
  end
@@ -153,7 +180,6 @@ module Apartment
153
180
  # @return {String} raw SQL contaning only postgres schema dump
154
181
  #
155
182
  def pg_dump_schema
156
-
157
183
  # Skip excluded tables? :/
158
184
  # excluded_tables =
159
185
  # collect_table_names(Apartment.excluded_models)
@@ -176,7 +202,10 @@ module Apartment
176
202
  # Temporary set Postgresql related environment variables if there are in @config
177
203
  #
178
204
  def with_pg_env(&block)
179
- pghost, pgport, pguser, pgpassword = ENV['PGHOST'], ENV['PGPORT'], ENV['PGUSER'], ENV['PGPASSWORD']
205
+ pghost = ENV['PGHOST']
206
+ pgport = ENV['PGPORT']
207
+ pguser = ENV['PGUSER']
208
+ pgpassword = ENV['PGPASSWORD']
180
209
 
181
210
  ENV['PGHOST'] = @config[:host] if @config[:host]
182
211
  ENV['PGPORT'] = @config[:port].to_s if @config[:port]
@@ -185,7 +214,10 @@ module Apartment
185
214
 
186
215
  block.call
187
216
  ensure
188
- ENV['PGHOST'], ENV['PGPORT'], ENV['PGUSER'], ENV['PGPASSWORD'] = pghost, pgport, pguser, pgpassword
217
+ ENV['PGHOST'] = pghost
218
+ ENV['PGPORT'] = pgport
219
+ ENV['PGUSER'] = pguser
220
+ ENV['PGPASSWORD'] = pgpassword
189
221
  end
190
222
 
191
223
  # Remove "SET search_path ..." line from SQL dump and prepend search_path set to current tenant
@@ -197,7 +229,7 @@ module Apartment
197
229
 
198
230
  swap_schema_qualifier(sql)
199
231
  .split("\n")
200
- .select {|line| check_input_against_regexps(line, PSQL_DUMP_BLACKLISTED_STATEMENTS).empty?}
232
+ .select { |line| check_input_against_regexps(line, PSQL_DUMP_BLACKLISTED_STATEMENTS).empty? }
201
233
  .prepend(search_path)
202
234
  .join("\n")
203
235
  end
@@ -207,7 +239,7 @@ module Apartment
207
239
  if Apartment.pg_excluded_names.any? { |name| match.include? name }
208
240
  match
209
241
  else
210
- match.gsub("#{default_tenant}.", %{"#{current}".})
242
+ match.gsub("#{default_tenant}.", %("#{current}".))
211
243
  end
212
244
  end
213
245
  end
@@ -215,7 +247,7 @@ module Apartment
215
247
  # Checks if any of regexps matches against input
216
248
  #
217
249
  def check_input_against_regexps(input, regexps)
218
- regexps.select {|c| input.match c}
250
+ regexps.select { |c| input.match c }
219
251
  end
220
252
 
221
253
  # Collect table names from AR Models
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'apartment/adapters/abstract_adapter'
2
4
 
3
5
  module Apartment
@@ -16,8 +18,10 @@ module Apartment
16
18
  end
17
19
 
18
20
  def drop(tenant)
19
- raise TenantNotFound,
20
- "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant))
21
+ unless File.exist?(database_file(tenant))
22
+ raise TenantNotFound,
23
+ "The tenant #{environmentify(tenant)} cannot be found."
24
+ end
21
25
 
22
26
  File.delete(database_file(tenant))
23
27
  end
@@ -26,18 +30,24 @@ module Apartment
26
30
  File.basename(Apartment.connection.instance_variable_get(:@config)[:database], '.sqlite3')
27
31
  end
28
32
 
29
- protected
33
+ protected
30
34
 
31
35
  def connect_to_new(tenant)
32
- raise TenantNotFound,
33
- "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant))
36
+ return reset if tenant.nil?
37
+
38
+ unless File.exist?(database_file(tenant))
39
+ raise TenantNotFound,
40
+ "The tenant #{environmentify(tenant)} cannot be found."
41
+ end
34
42
 
35
43
  super database_file(tenant)
36
44
  end
37
45
 
38
46
  def create_tenant(tenant)
39
- raise TenantExists,
40
- "The tenant #{environmentify(tenant)} already exists." if File.exists?(database_file(tenant))
47
+ if File.exist?(database_file(tenant))
48
+ raise TenantExists,
49
+ "The tenant #{environmentify(tenant)} already exists."
50
+ end
41
51
 
42
52
  begin
43
53
  f = File.new(database_file(tenant), File::CREAT)
@@ -46,7 +56,7 @@ module Apartment
46
56
  end
47
57
  end
48
58
 
49
- private
59
+ private
50
60
 
51
61
  def database_file(tenant)
52
62
  "#{@default_dir}/#{environmentify(tenant)}.sqlite3"
@@ -1,12 +1,44 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # A workaraound to get `reload!` to also call Apartment::Tenant.init
2
4
  # This is unfortunate, but I haven't figured out how to hook into the reload process *after* files are reloaded
3
5
 
4
6
  # reloads the environment
5
- def reload!(print=true)
6
- puts "Reloading..." if print
7
+ def reload!(print = true)
8
+ # rubocop:disable Rails/Output
9
+ puts 'Reloading...' if print
10
+ # rubocop:enable Rails/Output
11
+
7
12
  # This triggers the to_prepare callbacks
8
- ActionDispatch::Callbacks.new(Proc.new {}).call({})
13
+ ActionDispatch::Callbacks.new(proc {}).call({})
9
14
  # Manually init Apartment again once classes are reloaded
10
15
  Apartment::Tenant.init
11
16
  true
12
17
  end
18
+
19
+ def st(schema_name = nil)
20
+ if schema_name.nil?
21
+ # rubocop:disable Rails/Output
22
+ tenant_list.each { |t| puts t }
23
+ # rubocop:enable Rails/Output
24
+ elsif tenant_list.include? schema_name
25
+ Apartment::Tenant.switch!(schema_name)
26
+ else
27
+ # rubocop:disable Rails/Output
28
+ puts "Tenant #{schema_name} is not part of the tenant list"
29
+ # rubocop:enable Rails/Output
30
+ end
31
+ end
32
+
33
+ def tenant_list
34
+ tenant_list = [Apartment.default_tenant]
35
+ tenant_list += Apartment.tenant_names
36
+ tenant_list.uniq
37
+ end
38
+
39
+ def tenant_info_msg
40
+ # rubocop:disable Rails/Output
41
+ puts "Available Tenants: #{tenant_list}\n"
42
+ puts "Use `st 'tenant'` to switch tenants & `tenant_list` to see list\n"
43
+ # rubocop:enable Rails/Output
44
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'console'
4
+
5
+ module Apartment
6
+ module CustomConsole
7
+ begin
8
+ require 'pry-rails'
9
+ rescue LoadError
10
+ # rubocop:disable Rails/Output
11
+ puts '[Failed to load pry-rails] If you want to use Apartment custom prompt you need to add pry-rails to your gemfile'
12
+ # rubocop:enable Rails/Output
13
+ end
14
+
15
+ desc = "Includes the current Rails environment and project folder name.\n" \
16
+ '[1] [project_name][Rails.env][Apartment::Tenant.current] pry(main)>'
17
+
18
+ prompt_procs = [
19
+ proc { |target_self, nest_level, pry| prompt_contents(pry, target_self, nest_level, '>') },
20
+ proc { |target_self, nest_level, pry| prompt_contents(pry, target_self, nest_level, '*') }
21
+ ]
22
+
23
+ if Gem::Version.new(Pry::VERSION) >= Gem::Version.new('0.13')
24
+ Pry.config.prompt = Pry::Prompt.new 'ros', desc, prompt_procs
25
+ else
26
+ Pry::Prompt.add 'ros', desc, %w[> *] do |target_self, nest_level, pry, sep|
27
+ prompt_contents(pry, target_self, nest_level, sep)
28
+ end
29
+ Pry.config.prompt = Pry::Prompt[:ros][:value]
30
+ end
31
+
32
+ Pry.config.hooks.add_hook(:when_started, 'startup message') do
33
+ tenant_info_msg
34
+ end
35
+
36
+ def self.prompt_contents(pry, target_self, nest_level, sep)
37
+ "[#{pry.input_ring.size}] [#{PryRails::Prompt.formatted_env}][#{Apartment::Tenant.current}] " \
38
+ "#{pry.config.prompt_name}(#{Pry.view_clip(target_self)})" \
39
+ "#{":#{nest_level}" unless nest_level.zero?}#{sep} "
40
+ end
41
+ end
42
+ end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/deprecation'
2
4
 
3
5
  module Apartment
4
6
  module Deprecation
5
-
6
7
  def self.warn(message)
7
8
  ActiveSupport::Deprecation.warn message
8
9
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'apartment/elevators/generic'
2
4
 
3
5
  module Apartment
@@ -8,14 +10,13 @@ module Apartment
8
10
  # eg. example.com => example
9
11
  # www.example.bc.ca => example
10
12
  # a.example.bc.ca => a
11
- #
13
+ #
12
14
  #
13
15
  class Domain < Generic
14
-
15
16
  def parse_tenant_name(request)
16
17
  return nil if request.host.blank?
17
18
 
18
- request.host.match(/(www\.)?(?<sld>[^.]*)/)["sld"]
19
+ request.host.match(/(www\.)?(?<sld>[^.]*)/)['sld']
19
20
  end
20
21
  end
21
22
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'apartment/elevators/subdomain'
2
4
 
3
5
  module Apartment
@@ -8,10 +10,9 @@ module Apartment
8
10
  # - example1.domain.com => example1
9
11
  # - example2.something.domain.com => example2
10
12
  class FirstSubdomain < Subdomain
11
-
12
13
  def parse_tenant_name(request)
13
14
  super.split('.')[0] unless super.nil?
14
15
  end
15
16
  end
16
17
  end
17
- end
18
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rack/request'
2
4
  require 'apartment/tenant'
3
5
 
@@ -6,7 +8,6 @@ module Apartment
6
8
  # Provides a rack based tenant switching solution based on request
7
9
  #
8
10
  class Generic
9
-
10
11
  def initialize(app, processor = nil)
11
12
  @app = app
12
13
  @processor = processor || method(:parse_tenant_name)
@@ -24,8 +25,8 @@ module Apartment
24
25
  end
25
26
  end
26
27
 
27
- def parse_tenant_name(request)
28
- raise "Override"
28
+ def parse_tenant_name(_request)
29
+ raise 'Override'
29
30
  end
30
31
  end
31
32
  end