company_scope 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 741c6e4381fb5089d8e46d7cc20e6d54f19a5a50
4
- data.tar.gz: 6364437a929c37db0b99a2d15735f70c07902b3d
3
+ metadata.gz: 3bd35c521be7b29426a6d51ec979dc35d5a8b3c9
4
+ data.tar.gz: 3abaadbce17bdb2c69914549909f8b53099a62f6
5
5
  SHA512:
6
- metadata.gz: e3948930bf2c57915fa2de4d73c6708804ba48df52b0dd4ec3e9a7ac1d9c398fbbd97442586c6bf9950bb64c24f86636512cbbc8cb9ad8f440d938dfda1563da
7
- data.tar.gz: 55d75b25036746328dfc330448af2b596b2fafba813b79993dddacaafb5c354b63c980fd87a4447789468d5e5b97a74c32b14e7756ed7ca640fea80021957f80
6
+ metadata.gz: 42cf8b1a6a123690459be1d8ee51d3cfdb3cb35e163adb4c8a2dbc9972b7d21eeae2123c56356570978f97f736b1542889273ef742cb10a20c96ba9098547986
7
+ data.tar.gz: b7d380a65b9f17a33092e0369cba7b81dad3fc5445f93143718236fa59e1e6621f88cbcd3ca1af58fc32ce3685720e49f4d1e5556ddd7225db68eb2b43c02545
data/README.md CHANGED
@@ -48,23 +48,55 @@ In the current version a helper_method called "current_company" is added to the
48
48
  where you add the method "company_setup". The gem includes Rack Middleware that injects the
49
49
  "Company ID" into "request.env".
50
50
 
51
- The included Rack Middleware is inserted into the Rails Application stack automatically and
52
- you need to let it know what the model is called that is handling the scoping - see example
53
- below: (i.e. adding the company_scope.company_model name as a sym)
51
+ The included Rack Middleware is inserted into the Rails Application stack automatically.
52
+ The defaults are to use the an ActiveRecord model called company for the scoping model.
53
+ The process of determining the company name from the request is done by extracting the
54
+ subdomain i.e. the first section in dotted url notation.
55
+
56
+ The example shows the default settings. If you use the default you do NOT need to set this
57
+ in the application.rb file. If however you want to use a different scoping model and a
58
+ different matching process you need to change these.
54
59
 
55
60
  ```ruby
56
61
  module YourRailsApp
57
62
  class Application < Rails::Application
58
63
  ...
59
- # - add Company Scope Model name (that your app will be scoped by)
60
- config.company_scope.company_model = :my_company
64
+ # - These are the DEFAULTS and are set automatically..
65
+ # - you only need to set these if you change them!
66
+ config.company_scope.company_model = :company
67
+ config.company_scope.company_name_matcher = :subdomain_matcher
61
68
  ...
62
69
  end
63
70
  end
64
71
  ```
65
72
 
66
- The parameter in the example above needs to be a symbol of the model of your application
67
- that will act as the account i.e. company, tenant etc.
73
+ The symbol parameter passed to the .company_model configuration in the example above needs
74
+ to be a symbol of the model of your application that will act as the account i.e. company,
75
+ tenant etc.
76
+
77
+ The symbol parameter passed to the .company_name_matcher configuration is the underscored
78
+ version of the class that is used to handle the matching. All it needs to implement is a
79
+ method called to_domain(request) that takes the request as the parameter. It needs to return
80
+ the company name as a string as:
81
+
82
+ ```ruby
83
+ class MyCustomDomainMatcher
84
+ def to_domain(request)
85
+ .... my custom logic to extrapoulate the company name
86
+ company_name # the return value must be the company_name as a string.
87
+ end
88
+ ```
89
+ And then include this in the application.rb
90
+
91
+ ```ruby
92
+ module YourRailsApp
93
+ class Application < Rails::Application
94
+ ...
95
+ config.company_scope.company_name_matcher = :my_custom_domain_matcher
96
+ ...
97
+ end
98
+ end
99
+ ```
68
100
 
69
101
  The domain 'lvh.me' points to 127.0.0.1 and is therefore an ideal candidate for using with local development and subdomains since they will also all point to your localhost.
70
102
 
@@ -40,7 +40,8 @@ module CompanyScope
40
40
  #
41
41
  def company_setup
42
42
  helper_method :current_company
43
- set_scoping_class Rails.application.config.company_scope[:company_model]
43
+ #set_scoping_class Rails.application.config.company_scope[:company_model]
44
+ set_scoping_class CompanyScope.config.company_model
44
45
  end
45
46
  #
46
47
  def acts_as_company_filter
@@ -5,18 +5,17 @@ module CompanyScope
5
5
  config.company_scope = ActiveSupport::OrderedOptions.new
6
6
  #
7
7
  initializer :after_initialize do |app|
8
- #
9
- puts "\nLoading Railtie after_initialize..\n"
10
- #
8
+
11
9
  CompanyScope.configure do |config|
12
- config.company_model = app.config.company_scope[:company_model]
10
+ config.company_model = app.config.company_scope[:company_model] || :company
11
+ config.company_name_matcher = app.config.company_scope[:company_name_matcher] || :subdomain_matcher
13
12
  end
14
13
 
15
- # - retrieve the company_scope - model name - usually set in application.rb
16
- company_config = app.config.company_scope[:company_model] || :company
14
+ company_config = CompanyScope.config.company_model
15
+ company_name_matcher = CompanyScope.config.company_name_matcher
17
16
 
18
17
  # - add MultiCompany Rack middleware to detect the company_name from the subdomain
19
- app.config.middleware.insert_after Rack::Sendfile, Custom::MultiCompany, company_config
18
+ app.config.middleware.insert_after Rack::Sendfile, Custom::MultiCompany, company_config, company_name_matcher
20
19
 
21
20
  # - the base module injects the default scope into company dependant models
22
21
  ActiveRecord::Base.send(:include, CompanyScope::Base)
@@ -1,3 +1,3 @@
1
1
  module CompanyScope
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
data/lib/company_scope.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "request_store"
2
2
  #
3
+ require File.dirname(__FILE__) + '/subdomain_matcher'
3
4
  require File.dirname(__FILE__) + '/custom/multi_company'
4
5
  require File.dirname(__FILE__) + '/company_scope/base'
5
6
  require File.dirname(__FILE__) + '/company_scope/guardian'
@@ -12,6 +13,7 @@ require File.dirname(__FILE__) + '/company_scope/railtie' if defined? ::Rails::R
12
13
  module CompanyScope
13
14
  class Config
14
15
  attr_accessor :company_model
16
+ attr_accessor :company_name_matcher
15
17
  end
16
18
 
17
19
  def self.config
@@ -5,19 +5,23 @@ module Custom
5
5
  #
6
6
  attr_reader :company_class_name
7
7
  #
8
- def initialize(app, company_class)
8
+ def initialize(app, company_class, company_name_matcher)
9
9
  @app = app
10
10
  @company_class_name = company_class.to_s.split('_').collect!{ |w| w.capitalize }.join
11
11
  @company = Module.const_get(@company_class_name).find_by_company_name('DEFAULT') if db_configured
12
+ @company_name_matcher = Module.const_get(
13
+ company_name_matcher.to_s.split('_').collect!{ |w| w.capitalize }.join)
12
14
  end
13
15
 
14
16
  def call(env)
15
17
  request = Rack::Request.new(env)
16
- first_sub_domain = request.host.split('.').first
17
- # disallow any non alphabetic chars!
18
- domain = first_sub_domain.upcase if first_sub_domain.match(/\A[a-zA-Z0-9]*\z/)
18
+
19
+ # - the matcher is an injected class with a to_domain method that makes it easy to replace
20
+ # - with a different scheme of obtaining the company name from Request/Url.
21
+ domain = @company_name_matcher.to_domain(request)
22
+
19
23
  # insert the company into ENV - for the controller helper_method 'current_company'
20
- retrieve_company_from_subdomain(domain)
24
+ retrieve_company_from_subdomain(domain.upcase)
21
25
  env['COMPANY_ID'] = @company.id unless @company.nil?
22
26
  @app.call(env)
23
27
  end
@@ -0,0 +1,8 @@
1
+ #
2
+ class SubdomainMatcher
3
+ def self.to_domain(request)
4
+ first_sub_domain = request.host.split('.').first
5
+ # disallow any non alphabetic chars!
6
+ domain = first_sub_domain.upcase if first_sub_domain.match(/\A[a-zA-Z0-9]*\z/)
7
+ end
8
+ end
data/log/test.log CHANGED
@@ -182,3 +182,155 @@ Processing by DummyApplicationController#index as HTML
182
182
  Completed 200 OK in 0ms (Views: 0.1ms)
183
183
  Processing by DummyApplicationController#index as HTML
184
184
  Completed 200 OK in 0ms (Views: 0.1ms)
185
+ Processing by DummyApplicationController#index as HTML
186
+ Completed 200 OK in 1ms (Views: 0.3ms)
187
+ Processing by DummyApplicationController#index as HTML
188
+ Completed 200 OK in 0ms (Views: 0.2ms)
189
+ Processing by DummyApplicationController#index as HTML
190
+ Completed 200 OK in 0ms (Views: 0.2ms)
191
+ Processing by DummyApplicationController#index as HTML
192
+ Completed 500 Internal Server Error in 0ms
193
+ Processing by DummyApplicationController#index as HTML
194
+ Completed 200 OK in 0ms (Views: 0.1ms)
195
+ Processing by DummyApplicationController#index as HTML
196
+ Completed 200 OK in 0ms (Views: 0.2ms)
197
+ Processing by DummyApplicationController#index as HTML
198
+ Completed 200 OK in 0ms (Views: 0.1ms)
199
+ Processing by DummyApplicationController#index as HTML
200
+ Completed 500 Internal Server Error in 0ms
201
+ Processing by DummyApplicationController#index as HTML
202
+ Completed 200 OK in 1ms (Views: 0.3ms)
203
+ Processing by DummyApplicationController#index as HTML
204
+ Completed 200 OK in 0ms (Views: 0.1ms)
205
+ Processing by DummyApplicationController#index as HTML
206
+ Completed 200 OK in 0ms (Views: 0.1ms)
207
+ Processing by DummyApplicationController#index as HTML
208
+ Completed 500 Internal Server Error in 0ms
209
+ Processing by DummyApplicationController#index as HTML
210
+ Completed 200 OK in 0ms (Views: 0.3ms)
211
+ Processing by DummyApplicationController#index as HTML
212
+ Completed 200 OK in 0ms (Views: 0.1ms)
213
+ Processing by DummyApplicationController#index as HTML
214
+ Completed 200 OK in 0ms (Views: 0.1ms)
215
+ Processing by DummyApplicationController#index as HTML
216
+ Completed 500 Internal Server Error in 0ms
217
+ Processing by DummyApplicationController#index as HTML
218
+ Completed 200 OK in 0ms (Views: 0.2ms)
219
+ Processing by DummyApplicationController#index as HTML
220
+ Completed 200 OK in 0ms (Views: 0.1ms)
221
+ Processing by DummyApplicationController#index as HTML
222
+ Completed 200 OK in 0ms (Views: 0.2ms)
223
+ Processing by DummyApplicationController#index as HTML
224
+ Completed 500 Internal Server Error in 0ms
225
+ Processing by DummyApplicationController#index as HTML
226
+ Completed 200 OK in 0ms (Views: 0.2ms)
227
+ Processing by DummyApplicationController#index as HTML
228
+ Completed 200 OK in 0ms (Views: 0.1ms)
229
+ Processing by DummyApplicationController#index as HTML
230
+ Completed 200 OK in 0ms (Views: 0.1ms)
231
+ Processing by DummyApplicationController#index as HTML
232
+ Completed 500 Internal Server Error in 0ms
233
+ Processing by DummyApplicationController#index as HTML
234
+ Completed 200 OK in 0ms (Views: 0.2ms)
235
+ Processing by DummyApplicationController#index as HTML
236
+ Completed 200 OK in 0ms (Views: 0.1ms)
237
+ Processing by DummyApplicationController#index as HTML
238
+ Completed 200 OK in 0ms (Views: 0.2ms)
239
+ Processing by DummyApplicationController#index as HTML
240
+ Completed 500 Internal Server Error in 0ms
241
+ Processing by DummyApplicationController#index as HTML
242
+ Completed 200 OK in 0ms (Views: 0.2ms)
243
+ Processing by DummyApplicationController#index as HTML
244
+ Completed 200 OK in 0ms (Views: 0.1ms)
245
+ Processing by DummyApplicationController#index as HTML
246
+ Completed 200 OK in 0ms (Views: 0.1ms)
247
+ Processing by DummyApplicationController#index as HTML
248
+ Completed 500 Internal Server Error in 0ms
249
+ Processing by DummyApplicationController#index as HTML
250
+ Completed 200 OK in 1ms (Views: 0.3ms)
251
+ Processing by DummyApplicationController#index as HTML
252
+ Completed 200 OK in 0ms (Views: 0.1ms)
253
+ Processing by DummyApplicationController#index as HTML
254
+ Completed 200 OK in 0ms (Views: 0.1ms)
255
+ Processing by DummyApplicationController#index as HTML
256
+ Completed 500 Internal Server Error in 0ms
257
+ Processing by DummyApplicationController#index as HTML
258
+ Completed 200 OK in 0ms (Views: 0.2ms)
259
+ Processing by DummyApplicationController#index as HTML
260
+ Completed 200 OK in 0ms (Views: 0.2ms)
261
+ Processing by DummyApplicationController#index as HTML
262
+ Completed 200 OK in 0ms (Views: 0.1ms)
263
+ Processing by DummyApplicationController#index as HTML
264
+ Completed 500 Internal Server Error in 0ms
265
+ Processing by DummyApplicationController#index as HTML
266
+ Completed 200 OK in 0ms (Views: 0.2ms)
267
+ Processing by DummyApplicationController#index as HTML
268
+ Completed 200 OK in 0ms (Views: 0.1ms)
269
+ Processing by DummyApplicationController#index as HTML
270
+ Completed 200 OK in 0ms (Views: 0.1ms)
271
+ Processing by DummyApplicationController#index as HTML
272
+ Completed 500 Internal Server Error in 0ms
273
+ Processing by DummyApplicationController#index as HTML
274
+ Completed 200 OK in 0ms (Views: 0.2ms)
275
+ Processing by DummyApplicationController#index as HTML
276
+ Completed 200 OK in 0ms (Views: 0.2ms)
277
+ Processing by DummyApplicationController#index as HTML
278
+ Completed 200 OK in 0ms (Views: 0.1ms)
279
+ Processing by DummyApplicationController#index as HTML
280
+ Completed 500 Internal Server Error in 0ms
281
+ Processing by DummyApplicationController#index as HTML
282
+ Completed 200 OK in 0ms (Views: 0.2ms)
283
+ Processing by DummyApplicationController#index as HTML
284
+ Completed 200 OK in 0ms (Views: 0.2ms)
285
+ Processing by DummyApplicationController#index as HTML
286
+ Completed 200 OK in 0ms (Views: 0.1ms)
287
+ Processing by DummyApplicationController#index as HTML
288
+ Completed 500 Internal Server Error in 0ms
289
+ Processing by DummyApplicationController#index as HTML
290
+ Completed 200 OK in 0ms (Views: 0.3ms)
291
+ Processing by DummyApplicationController#index as HTML
292
+ Completed 200 OK in 0ms (Views: 0.1ms)
293
+ Processing by DummyApplicationController#index as HTML
294
+ Completed 200 OK in 0ms (Views: 0.1ms)
295
+ Processing by DummyApplicationController#index as HTML
296
+ Completed 500 Internal Server Error in 0ms
297
+ Processing by DummyApplicationController#index as HTML
298
+ Completed 200 OK in 0ms (Views: 0.2ms)
299
+ Processing by DummyApplicationController#index as HTML
300
+ Completed 200 OK in 0ms (Views: 0.1ms)
301
+ Processing by DummyApplicationController#index as HTML
302
+ Completed 200 OK in 0ms (Views: 0.2ms)
303
+ Processing by DummyApplicationController#index as HTML
304
+ Completed 500 Internal Server Error in 0ms
305
+ Processing by DummyApplicationController#index as HTML
306
+ Completed 200 OK in 0ms (Views: 0.2ms)
307
+ Processing by DummyApplicationController#index as HTML
308
+ Completed 200 OK in 0ms (Views: 0.2ms)
309
+ Processing by DummyApplicationController#index as HTML
310
+ Completed 200 OK in 0ms (Views: 0.1ms)
311
+ Processing by DummyApplicationController#index as HTML
312
+ Completed 500 Internal Server Error in 0ms
313
+ Processing by DummyApplicationController#index as HTML
314
+ Completed 200 OK in 0ms (Views: 0.2ms)
315
+ Processing by DummyApplicationController#index as HTML
316
+ Completed 200 OK in 0ms (Views: 0.2ms)
317
+ Processing by DummyApplicationController#index as HTML
318
+ Completed 200 OK in 0ms (Views: 0.1ms)
319
+ Processing by DummyApplicationController#index as HTML
320
+ Completed 500 Internal Server Error in 0ms
321
+ Processing by DummyApplicationController#index as HTML
322
+ Completed 200 OK in 0ms (Views: 0.2ms)
323
+ Processing by DummyApplicationController#index as HTML
324
+ Completed 200 OK in 0ms (Views: 0.2ms)
325
+ Processing by DummyApplicationController#index as HTML
326
+ Completed 200 OK in 0ms (Views: 0.2ms)
327
+ Processing by DummyApplicationController#index as HTML
328
+ Completed 500 Internal Server Error in 0ms
329
+ Processing by DummyApplicationController#index as HTML
330
+ Completed 200 OK in 0ms (Views: 0.2ms)
331
+ Processing by DummyApplicationController#index as HTML
332
+ Completed 200 OK in 0ms (Views: 0.2ms)
333
+ Processing by DummyApplicationController#index as HTML
334
+ Completed 200 OK in 0ms (Views: 0.2ms)
335
+ Processing by DummyApplicationController#index as HTML
336
+ Completed 500 Internal Server Error in 0ms
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: company_scope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Forkin
@@ -206,6 +206,7 @@ files:
206
206
  - lib/company_scope/railtie.rb
207
207
  - lib/company_scope/version.rb
208
208
  - lib/custom/multi_company.rb
209
+ - lib/subdomain_matcher.rb
209
210
  - log/test.log
210
211
  homepage: http://github.com/netflakes/company_scope
211
212
  licenses: []