rails_multisite 2.1.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails_multisite might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4520b9fc9764176e8b5da30cb855e9326df49403655f79d521574140b6e85b9
4
- data.tar.gz: 8402996c6905b34c67b68d78ad4bfae8d2d8d440e0e71f884ff5d18ad68e62d0
3
+ metadata.gz: e38f660f7fbb4a5505adeb4d46f73a02f290cc65f9f566a03fc4fc7051e68392
4
+ data.tar.gz: 90242e454a09c2faed90d8561ac6b3de61c0d723344ac107e1fc89e23c20f0cd
5
5
  SHA512:
6
- metadata.gz: f53da66c5fee3e2871a90051d72b78a87c13307a0efa3dfe443bdee4693f46344d18545e405a6079cd41f7596909a645bfdd3c7b32a362be6a61ccdc7b64f1ef
7
- data.tar.gz: 9e10e10e824f77a79ed1cece3cbacc139d1c13362ed3b9d99e750aac1daca8066971ef8566b0d3df1cf389c9c2619cdefa9c7426ee9fd6e84f0ab6bbaf57b1a8
6
+ metadata.gz: 8a9bccdd03ddb205a20325ed127cc0e40b6e7c790147334ac9154350f5bc802e0462893541a39ab1ac0c54939d3cabadd7af455af5c779b882d93d5a4b5e4037
7
+ data.tar.gz: 7b0065714791944b3cabf2673f93a18277ec2cc721d85e8b5609ab94b7ff5928f8adf1500219aad4f659c27f5774e465739960c5a84468d6a0759ac07862c43b
data/README.md CHANGED
@@ -93,6 +93,19 @@ To get a Rails console that is connected to `some_database_1` database:
93
93
  RAILS_DB=db_one rails console
94
94
  ```
95
95
 
96
+ ### CDN origin support
97
+
98
+ To avoid needing to configure many origins you can consider using `RailsMultisite::ConnectionManagement.asset_hostnames`
99
+
100
+ When configured, requests to `asset_hostname`?__ws=another.host.name will be re-routed to the correct site. Cookies will
101
+ be stripped on all incoming requests.
102
+
103
+ Example:
104
+
105
+ - Multisite serves `sub.example.com` and `assets.example.com`
106
+ - `RailsMultisite::ConnectionManagement.asset_hostnames = ['assets.example.com']`
107
+ - Requests to `https://assets.example.com/route/?__ws=sub.example.com` will be routed to the `sub.example.com`
108
+
96
109
 
97
110
  ## Contributing
98
111
 
@@ -11,6 +11,10 @@ module RailsMultisite
11
11
  end
12
12
 
13
13
  def self.clear_settings!
14
+ @instance&.db_spec_cache&.each do |key, spec|
15
+ @instance.connection_handlers.delete(self.handler_key(spec))
16
+ end
17
+
14
18
  @instance = nil
15
19
  end
16
20
 
@@ -31,6 +35,14 @@ module RailsMultisite
31
35
  end
32
36
  end
33
37
 
38
+ def self.asset_hostnames
39
+ @asset_hostnames
40
+ end
41
+
42
+ def self.asset_hostnames=(h)
43
+ @asset_hostnames = h
44
+ end
45
+
34
46
  def self.config_filename
35
47
  @instance.config_filename
36
48
  end
@@ -121,17 +133,45 @@ module RailsMultisite
121
133
  end
122
134
  end
123
135
 
124
- attr_reader :config_filename
136
+ def self.handler_key(spec)
137
+ @handler_key_suffix ||= begin
138
+ if ActiveRecord::Base.respond_to?(:writing_role)
139
+ "_#{ActiveRecord::Base.writing_role}"
140
+ else
141
+ ""
142
+ end
143
+ end
144
+
145
+ :"#{spec.name}#{@handler_key_suffix}"
146
+ end
147
+
148
+ def self.default_connection_handler=(connection_handler)
149
+ if @instance
150
+ unless connection_handler.is_a?(ActiveRecord::ConnectionAdapters::ConnectionHandler)
151
+ raise ArgumentError.new("Invalid connection handler")
152
+ end
153
+
154
+ @instance.default_connection_handler = connection_handler
155
+ end
156
+ end
157
+
158
+ attr_reader :config_filename, :db_spec_cache, :connection_handlers
159
+ attr_writer :default_connection_handler
125
160
 
126
161
  def initialize(config_filename)
127
162
  @config_filename = config_filename
128
163
 
129
- @connection_handlers = {}
130
- @db_spec_cache = {}
164
+ @connection_handlers = begin
165
+ if ActiveRecord::Base.respond_to?(:connection_handlers)
166
+ ActiveRecord::Base.connection_handlers
167
+ else
168
+ {}
169
+ end
170
+ end
131
171
 
172
+ @db_spec_cache = {}
132
173
  @default_spec = SPEC_KLASS::Resolver.new(ActiveRecord::Base.configurations).spec(Rails.env.to_sym)
133
174
  @default_connection_handler = ActiveRecord::Base.connection_handler
134
- @established_default = false
135
175
 
136
176
  @reload_mutex = Mutex.new
137
177
 
@@ -188,8 +228,7 @@ module RailsMultisite
188
228
  @db_spec_cache = new_db_spec_cache
189
229
 
190
230
  # Clean up connection handler cache.
191
- # (@connection_handlers is a hash of spec => handler)
192
- removed_specs.each { |s| @connection_handlers.delete(s) }
231
+ removed_specs.each { |s| @connection_handlers.delete(handler_key(s)) }
193
232
  end
194
233
 
195
234
  def reload
@@ -217,18 +256,14 @@ module RailsMultisite
217
256
  spec ||= @default_spec
218
257
  handler = nil
219
258
  if spec != @default_spec
220
- handler = @connection_handlers[spec]
259
+ handler = @connection_handlers[handler_key(spec)]
221
260
  unless handler
222
261
  handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
223
262
  handler_establish_connection(handler, spec)
224
- @connection_handlers[spec] = handler
263
+ @connection_handlers[handler_key(spec)] = handler
225
264
  end
226
265
  else
227
266
  handler = @default_connection_handler
228
- if !@established_default
229
- handler_establish_connection(handler, spec)
230
- @established_default = true
231
- end
232
267
  end
233
268
 
234
269
  ActiveRecord::Base.connection_handler = handler
@@ -343,7 +378,14 @@ module RailsMultisite
343
378
  end
344
379
 
345
380
  request = Rack::Request.new(env)
346
- host = request['__ws'] || request.host
381
+
382
+ host =
383
+ if request['__ws'] && self.class.asset_hostnames&.include?(request.host)
384
+ request.cookies.clear
385
+ request['__ws']
386
+ else
387
+ request.host
388
+ end
347
389
 
348
390
  env["RAILS_MULTISITE_HOST"] = host
349
391
  end
@@ -362,5 +404,9 @@ module RailsMultisite
362
404
  handler.establish_connection(spec.config)
363
405
  end
364
406
 
407
+ def handler_key(spec)
408
+ self.class.handler_key(spec)
409
+ end
410
+
365
411
  end
366
412
  end
@@ -7,20 +7,28 @@ module RailsMultisite
7
7
  end
8
8
 
9
9
  initializer "RailsMultisite.init" do |app|
10
- Rails.configuration.multisite = false
10
+ app.config.multisite = false
11
11
 
12
12
  config_file = ConnectionManagement.default_config_filename
13
13
  if File.exist?(config_file)
14
14
  ConnectionManagement.config_filename = ConnectionManagement.default_config_filename
15
- Rails.configuration.multisite = true
15
+ app.config.multisite = true
16
16
  Rails.logger.formatter = RailsMultisite::Formatter.new
17
- app.middleware.insert_after(ActionDispatch::Executor, RailsMultisite::Middleware)
18
- app.middleware.delete(ActionDispatch::Executor)
17
+
18
+ if !skip_middleware?(app.config)
19
+ app.middleware.insert_after(ActionDispatch::Executor, RailsMultisite::Middleware)
20
+ app.middleware.delete(ActionDispatch::Executor)
21
+ end
19
22
 
20
23
  if ENV['RAILS_DB'].present?
21
24
  ConnectionManagement.establish_connection(db: ENV['RAILS_DB'], raise_on_missing: true)
22
25
  end
23
26
  end
24
27
  end
28
+
29
+ def skip_middleware?(config)
30
+ return false if !config.respond_to?(:skip_multisite_middleware)
31
+ config.skip_multisite_middleware
32
+ end
25
33
  end
26
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  module RailsMultisite
4
- VERSION = "2.1.1"
4
+ VERSION = "2.5.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_multisite
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-13 00:00:00.000000000 Z
11
+ date: 2020-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -70,7 +70,7 @@ files:
70
70
  homepage: ''
71
71
  licenses: []
72
72
  metadata: {}
73
- post_install_message:
73
+ post_install_message:
74
74
  rdoc_options: []
75
75
  require_paths:
76
76
  - lib
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  version: '0'
87
87
  requirements: []
88
88
  rubygems_version: 3.0.3
89
- signing_key:
89
+ signing_key:
90
90
  specification_version: 4
91
91
  summary: Multi tenancy support for Rails
92
92
  test_files: []