rails_multisite 8.0.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff0147d2b57bef0b6daf4cd53adac973cd87dd1a7727c152a7241a29d36e44ef
4
- data.tar.gz: b8c1c8994c145bc3545527469dc8431f2d9b0226d2e4773de427397603cc99d9
3
+ metadata.gz: 42d7959bbb9536ba5902f2532aa20c14db3365de98bf8b6b20c0612c12be583e
4
+ data.tar.gz: bc05e6e85d428d3b4785b58e3468d884df30224a47f63f032620e66d854abb57
5
5
  SHA512:
6
- metadata.gz: b294e10922a81cc55cab1514edc26cfc4bcb0ce575b89267cb523f4d4b6666a8486cef62637c8569a0aff0d3b79ce8287a2f43b214582fb9f029bad41881d5f6
7
- data.tar.gz: 727deaa92d13b9c26d4a9482f4c36ab772e05e63eacff8e27f972fa61c581fdff9e0d63590a22fdc8160a21754a595ad3a7420a950788db2e35af54c73fd06fd
6
+ metadata.gz: e3fc63f60cd559f3ed57895a30620b0cc7b2a0de8a19124a40f8e79f04985805eac39d95edb0873c907ca4488badc2985ddade7b07851d5205226a5cb61621ef
7
+ data.tar.gz: 960da8f7bbe5534911f6269ff493250e6520b927d238299ad17efcdc5f0b357b02cd11267433fb618e4d3853a0ab159b4ab3c9f0fcd0680783928549239343c8
@@ -14,7 +14,10 @@ module RailsMultisite
14
14
  def default_connection_handler=(_connection_handler)
15
15
  end
16
16
 
17
- def establish_connection(_opts)
17
+ def establish_connection(db: nil, host: nil, raise_on_missing: true)
18
+ if !host && !has_db?(db.to_s) && raise_on_missing
19
+ raise UnknownSiteError.new(db)
20
+ end
18
21
  end
19
22
 
20
23
  def reload
@@ -44,16 +47,18 @@ module RailsMultisite
44
47
  env["HTTP_HOST"]
45
48
  end
46
49
 
47
- def with_connection(db = DEFAULT, &blk)
50
+ def with_connection(db = DEFAULT, raise_on_missing: true, &blk)
51
+ raise UnknownSiteError.new(db) if !has_db?(db.to_s) && raise_on_missing
52
+
48
53
  connected = ActiveRecord::Base.connection_pool.connected?
49
54
  result = blk.call(db)
50
- unless connected
55
+ if !connected
51
56
  ActiveRecord::Base.connection_handler.clear_active_connections!
52
57
  end
53
58
  result
54
59
  end
55
60
 
56
- def with_hostname(hostname, &blk)
61
+ def with_hostname(hostname, raise_on_missing: true, &blk)
57
62
  blk.call(hostname)
58
63
  end
59
64
  end
@@ -4,6 +4,12 @@ require "rails_multisite/connection_management/connection_specification"
4
4
  require "rails_multisite/connection_management/null_instance"
5
5
 
6
6
  module RailsMultisite
7
+ class UnknownSiteError < StandardError
8
+ def initialize(db)
9
+ super("ERROR: #{db} not found!")
10
+ end
11
+ end
12
+
7
13
  class ConnectionManagement
8
14
  DEFAULT = "default"
9
15
 
@@ -149,18 +155,20 @@ module RailsMultisite
149
155
  db == DEFAULT || !!db_spec_cache[db]
150
156
  end
151
157
 
152
- def establish_connection(opts)
153
- opts[:db] = opts[:db].to_s
158
+ def establish_connection(db: nil, host: nil, raise_on_missing: true)
159
+ db = db.to_s
154
160
 
155
- if opts[:db] != DEFAULT
156
- spec = connection_spec(opts)
157
-
158
- if (!spec && opts[:raise_on_missing])
159
- raise "ERROR: #{opts[:db]} not found!"
161
+ spec =
162
+ if db == DEFAULT
163
+ @default_spec
164
+ elsif (found = connection_spec(db: db, host: host))
165
+ found
166
+ elsif raise_on_missing
167
+ raise UnknownSiteError.new(host || db)
168
+ else
169
+ @default_spec
160
170
  end
161
- end
162
171
 
163
- spec ||= @default_spec
164
172
  handler = nil
165
173
  if spec != @default_spec
166
174
  handler = connection_handlers[handler_key(spec)]
@@ -176,18 +184,20 @@ module RailsMultisite
176
184
  ActiveRecord::Base.connection_handler = handler
177
185
  end
178
186
 
179
- def with_hostname(hostname)
187
+ def with_hostname(hostname, raise_on_missing: true)
180
188
  old = current_hostname
181
189
  connected = ActiveRecord::Base.connection_pool.connected?
182
190
 
183
- establish_connection(host: hostname) unless connected && hostname == old
191
+ if !(connected && hostname == old)
192
+ establish_connection(host: hostname, raise_on_missing: raise_on_missing)
193
+ end
184
194
  rval = yield hostname
185
195
 
186
- unless connected && hostname == old
196
+ if !(connected && hostname == old)
187
197
  ActiveRecord::Base.connection_handler.clear_active_connections!
188
198
 
189
- establish_connection(host: old)
190
- unless connected
199
+ establish_connection(host: old, raise_on_missing: false)
200
+ if !connected
191
201
  ActiveRecord::Base.connection_handler.clear_active_connections!
192
202
  end
193
203
  end
@@ -195,17 +205,19 @@ module RailsMultisite
195
205
  rval
196
206
  end
197
207
 
198
- def with_connection(db = DEFAULT)
208
+ def with_connection(db = DEFAULT, raise_on_missing: true)
199
209
  old = current_db
200
210
  connected = ActiveRecord::Base.connection_pool.connected?
201
211
 
202
- establish_connection(db: db) unless connected && db == old
212
+ if !(connected && db == old)
213
+ establish_connection(db: db, raise_on_missing: raise_on_missing)
214
+ end
203
215
  rval = yield db
204
216
 
205
- unless connected && db == old
217
+ if !(connected && db == old)
206
218
  ActiveRecord::Base.connection_handler.clear_active_connections!
207
219
 
208
- establish_connection(db: old)
220
+ establish_connection(db: old, raise_on_missing: false)
209
221
  unless connected
210
222
  ActiveRecord::Base.connection_handler.clear_active_connections!
211
223
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  module RailsMultisite
4
- VERSION = "8.0.0"
4
+ VERSION = "9.0.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_multisite
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.0.0
4
+ version: 9.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron