company_scope 0.2.2 → 0.6

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
  SHA1:
3
- metadata.gz: 4828ee3b27fc705144a5ea007d19da75cf2532e9
4
- data.tar.gz: 19bfe7b31a86d5b17e57345be24c170ce2b2af23
3
+ metadata.gz: 6ca30c47ec58cf778a1915aa695984a1885fe526
4
+ data.tar.gz: f0eee1abd0221430ebfbec22f0b53196b80e6406
5
5
  SHA512:
6
- metadata.gz: 07afdd531247d5a265d8782f342b75448012529656306b0f173d8d35aa0beb8ef0d39b5afc552f28e2f94eaacbbc340611786003e04f0e627aa35872e63b6ab8
7
- data.tar.gz: c7209d0567261ffb7b07232d8b78e153f6bd12ac946c64ef137bc4490e3cf1ff4c22431015e54348e8027c6b61d01afc4818ecbf1acef9d9870800fa92f9a248
6
+ metadata.gz: dcce31b93bc962421df3e84508906344eeda3e041513c74c37bff91d0d33ebc32166741542a3863a6e79ed0a8305530beadb312cfee12e7df2f67fca9f9a57a8
7
+ data.tar.gz: 694d469030842e9a45603081460f2e7090c37efa3d0446e0233414dd031a7d2bf380f0bb8fe3cf202f683cd7ec68edee090080dbb75ee1adde99b857c9927de5
data/README.md CHANGED
@@ -37,10 +37,42 @@ Getting started
37
37
  ===============
38
38
  There are three main steps in adding multi-tenancy/company to your app with company_scope:
39
39
 
40
- 1. Determining the company/account. Such as using the sub-domain.
41
- 2. Setting the current company and controller based setup.
42
- 3. Scoping your models.
40
+ 1. Run the install generator
41
+ 2. Determine the company/account. Such as using the sub-domain.
42
+ 3. Setting the current company and controller based setup.
43
+ 4. Scoping your models.
43
44
 
45
+ ### Run the install generator
46
+
47
+ The gem has an install generator that adds a configuration setting to config/application.rb
48
+ and also creates two models with associated migrations. The models are company and user.
49
+ The company has the acts_as_guardian company_scope module inserted, and the user has
50
+ the acts_as_company company_scope module inserted.
51
+
52
+ ```
53
+ rails generate company_scope:install
54
+ ```
55
+
56
+ If you are planning to use different models or to modify existing models you can run the
57
+ install generator without the migrations and model creation as follows
58
+
59
+ ```
60
+ rails generate company_scope:install --no_migrations
61
+ ```
62
+
63
+ ### Enabling the company_scope gem!
64
+
65
+ The install generator also added a configuration setting into config/application.rb that needs
66
+ to be enabled once the configuration is completed.
67
+
68
+ ```
69
+ class Application < Rails::Application
70
+ config.company_scope[:configured] = false
71
+
72
+ ```
73
+
74
+ This is deliberately set to false at the outset. Set this to true AFTER all the configuration of
75
+ the gem is completed.
44
76
 
45
77
  ### Process of determining the company/account
46
78
 
@@ -5,16 +5,18 @@ module CompanyScope
5
5
  config.company_scope = ActiveSupport::OrderedOptions.new
6
6
  #
7
7
  initializer :after_initialize do |app|
8
+ company_scope_enabled = app.config.company_scope[:configured] || false
9
+ company_scope_model = app.config.company_scope[:company_model] || :company
10
+ company_scope_matcher = app.config.company_scope[:company_name_matcher] || :subdomain_matcher
8
11
  #
9
- CompanyScope.configure do |config|
10
- config.enabled = app.config.company_scope[:configured] || false
11
- config.company_model = app.config.company_scope[:company_model] || :company
12
- config.company_name_matcher = app.config.company_scope[:company_name_matcher] || :subdomain_matcher
12
+ CompanyScope.configure do |c|
13
+ c.enabled = company_scope_enabled
14
+ c.company_model = company_scope_model
15
+ c.company_name_matcher = company_scope_matcher
13
16
  end
14
17
  #
15
- company_scope_configured = CompanyScope.config.enabled
16
18
  # - this is set in the template initializer - if not by default it is disabled!
17
- if company_scope_configured
19
+ if CompanyScope.config.enabled
18
20
  company_config = CompanyScope.config.company_model
19
21
  company_name_matcher = CompanyScope.config.company_name_matcher
20
22
  # - add MultiCompany Rack middleware to detect the company_name from the subdomain
@@ -1,3 +1,3 @@
1
1
  module CompanyScope
2
- VERSION = "0.2.2"
2
+ VERSION = "0.6"
3
3
  end
@@ -3,23 +3,77 @@ module CompanyScope
3
3
  class InstallGenerator < Rails::Generators::Base
4
4
  desc "CompanyScope installs the relevant database migrations"
5
5
  source_root File.expand_path("../templates", __FILE__)
6
-
6
+
7
7
  class_option :company_class_name, :type => :string, :default => 'company', :desc => "Name of Company Model"
8
8
  class_option :user_class_name, :type => :string, :default => 'user', :desc => "Name of User Model"
9
- class_option :enable_uuid, :type => boolean, :default => false, :desc => "Enable UUID if the extension is used"
9
+ class_option :no_migrations, :type => :boolean, :default => false, :desc => "Disable the creation of migrations"
10
10
 
11
11
  def self.source_root
12
12
  File.expand_path("../templates", __FILE__)
13
13
  end
14
14
 
15
15
  def modify_application_rb
16
+ # add the company_scope configuration enabler into config/application.rb
17
+ config_file = 'config/application.rb'
18
+ line = "class Application < Rails::Application"
19
+ insert_config = <<-RUBY
20
+ config.company_scope[:configured] = false
21
+ RUBY
22
+ if File.readlines(config_file).grep(/config.company_scope\[:configured\] = false/).size == 0
23
+ gsub_file config_file, /(#{Regexp.escape(line)})/mi do |match|
24
+ match << "\n#{insert_config}"
25
+ end
26
+ end
27
+ end
28
+
29
+ def generate_company_migration
30
+ unless options.no_migrations?
31
+ # - generate a company model and migration with a 50 char max on the company name and unique index
32
+ generate(:model, :company, 'company_name:string{50}:uniq' )
33
+ end
34
+ end
35
+
36
+ def generate_user_migration
37
+ unless options.no_migrations?
38
+ # - generate a user model and migration with a company_id reference a few basic user auth fields
39
+ migrate_user_model = <<-RUBY
40
+ company_id:id
41
+ password_hash:string
42
+ password_salt:string
43
+ first_name:string{50}
44
+ last_name:string{50}
45
+ user_name:string{50}:uniq
46
+ email_address:string{100}:uniq
47
+ RUBY
48
+ generate(:model, :user, "#{migrate_user_model.gsub("\n", " ")}")
49
+ end
50
+ end
51
+
52
+ def make_company_the_guardian
53
+ # - add the acts_as_guardian company_scope module into the company model
54
+ unless options.no_migrations?
55
+ config_file = 'app/models/company.rb'
56
+ line = "class Company < ActiveRecord::Base"
57
+ insert_guardian_scope = "acts_as_guardian"
58
+ if File.readlines(config_file).grep(/acts_as_guardian/).size == 0
59
+ gsub_file config_file, /(#{Regexp.escape(line)})/mi do |match|
60
+ match << "\n#{insert_guardian_scope}"
61
+ end
62
+ end
63
+ end
16
64
  end
17
65
 
18
- def create_migrations
19
- Dir["#{self.class.source_root}/migrations/*.rb"].sort.each do |filepath|
20
- name = File.basename(filepath)
21
- template "migrations/#{name}", "db/migrate/#{name}"
22
- sleep 1
66
+ def make_user_a_tenant
67
+ # - add the acts_as_company company_scope module into the user model
68
+ unless options.no_migrations?
69
+ config_file = 'app/models/user.rb'
70
+ line = "class User < ActiveRecord::Base"
71
+ insert_tenant_scope = "acts_as_company"
72
+ if File.readlines(config_file).grep(/acts_as_company/).size == 0
73
+ gsub_file config_file, /(#{Regexp.escape(line)})/mi do |match|
74
+ match << "\n#{insert_tenant_scope}"
75
+ end
76
+ end
23
77
  end
24
78
  end
25
79
  end
@@ -0,0 +1,13 @@
1
+ class CreateCompanies < ActiveRecord::Migration
2
+ def change
3
+ <% if options.enable_uuid? %>
4
+ create_table :companies, :id => :uuid do |t|
5
+ <% else %>
6
+ create_table :companies do |t|
7
+ <% end %>
8
+ t.string :company_name, :limit => 50
9
+ t.boolean :is_active, :default => true, null: false
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
data/log/test.log CHANGED
@@ -398,3 +398,115 @@ Processing by DummyApplicationController#index as HTML
398
398
  Completed 200 OK in 0ms (Views: 0.1ms)
399
399
  Processing by DummyApplicationController#index as HTML
400
400
  Completed 500 Internal Server Error in 0ms
401
+ Processing by DummyApplicationController#index as HTML
402
+ Completed 200 OK in 1ms (Views: 0.3ms)
403
+ Processing by DummyApplicationController#index as HTML
404
+ Completed 200 OK in 0ms (Views: 0.1ms)
405
+ Processing by DummyApplicationController#index as HTML
406
+ Completed 200 OK in 0ms (Views: 0.1ms)
407
+ Processing by DummyApplicationController#index as HTML
408
+ Completed 500 Internal Server Error in 0ms
409
+ Processing by DummyApplicationController#index as HTML
410
+ Completed 200 OK in 0ms (Views: 0.2ms)
411
+ Processing by DummyApplicationController#index as HTML
412
+ Completed 200 OK in 0ms (Views: 0.2ms)
413
+ Processing by DummyApplicationController#index as HTML
414
+ Completed 200 OK in 0ms (Views: 0.1ms)
415
+ Processing by DummyApplicationController#index as HTML
416
+ Completed 500 Internal Server Error in 0ms
417
+ Processing by DummyApplicationController#index as HTML
418
+ Completed 200 OK in 0ms (Views: 0.2ms)
419
+ Processing by DummyApplicationController#index as HTML
420
+ Completed 200 OK in 0ms (Views: 0.1ms)
421
+ Processing by DummyApplicationController#index as HTML
422
+ Completed 200 OK in 0ms (Views: 0.1ms)
423
+ Processing by DummyApplicationController#index as HTML
424
+ Completed 500 Internal Server Error in 0ms
425
+ Processing by DummyApplicationController#index as HTML
426
+ Completed 200 OK in 0ms (Views: 0.2ms)
427
+ Processing by DummyApplicationController#index as HTML
428
+ Completed 200 OK in 0ms (Views: 0.1ms)
429
+ Processing by DummyApplicationController#index as HTML
430
+ Completed 200 OK in 0ms (Views: 0.1ms)
431
+ Processing by DummyApplicationController#index as HTML
432
+ Completed 500 Internal Server Error in 0ms
433
+ Processing by DummyApplicationController#index as HTML
434
+ Completed 200 OK in 0ms (Views: 0.2ms)
435
+ Processing by DummyApplicationController#index as HTML
436
+ Completed 200 OK in 0ms (Views: 0.2ms)
437
+ Processing by DummyApplicationController#index as HTML
438
+ Completed 200 OK in 0ms (Views: 0.2ms)
439
+ Processing by DummyApplicationController#index as HTML
440
+ Completed 500 Internal Server Error in 0ms
441
+ Processing by DummyApplicationController#index as HTML
442
+ Completed 200 OK in 0ms (Views: 0.3ms)
443
+ Processing by DummyApplicationController#index as HTML
444
+ Completed 200 OK in 0ms (Views: 0.1ms)
445
+ Processing by DummyApplicationController#index as HTML
446
+ Completed 200 OK in 0ms (Views: 0.1ms)
447
+ Processing by DummyApplicationController#index as HTML
448
+ Completed 500 Internal Server Error in 0ms
449
+ Processing by DummyApplicationController#index as HTML
450
+ Completed 200 OK in 1ms (Views: 0.3ms)
451
+ Processing by DummyApplicationController#index as HTML
452
+ Completed 200 OK in 0ms (Views: 0.2ms)
453
+ Processing by DummyApplicationController#index as HTML
454
+ Completed 200 OK in 0ms (Views: 0.2ms)
455
+ Processing by DummyApplicationController#index as HTML
456
+ Completed 500 Internal Server Error in 0ms
457
+ Processing by DummyApplicationController#index as HTML
458
+ Completed 200 OK in 1ms (Views: 0.3ms)
459
+ Processing by DummyApplicationController#index as HTML
460
+ Completed 200 OK in 0ms (Views: 0.1ms)
461
+ Processing by DummyApplicationController#index as HTML
462
+ Completed 200 OK in 0ms (Views: 0.1ms)
463
+ Processing by DummyApplicationController#index as HTML
464
+ Completed 500 Internal Server Error in 0ms
465
+ Processing by DummyApplicationController#index as HTML
466
+ Completed 200 OK in 1ms (Views: 0.4ms)
467
+ Processing by DummyApplicationController#index as HTML
468
+ Completed 200 OK in 0ms (Views: 0.1ms)
469
+ Processing by DummyApplicationController#index as HTML
470
+ Completed 200 OK in 0ms (Views: 0.1ms)
471
+ Processing by DummyApplicationController#index as HTML
472
+ Completed 500 Internal Server Error in 0ms
473
+ Processing by DummyApplicationController#index as HTML
474
+ Completed 200 OK in 0ms (Views: 0.2ms)
475
+ Processing by DummyApplicationController#index as HTML
476
+ Completed 200 OK in 0ms (Views: 0.1ms)
477
+ Processing by DummyApplicationController#index as HTML
478
+ Completed 200 OK in 0ms (Views: 0.1ms)
479
+ Processing by DummyApplicationController#index as HTML
480
+ Completed 500 Internal Server Error in 0ms
481
+ Processing by DummyApplicationController#index as HTML
482
+ Completed 200 OK in 1ms (Views: 0.3ms)
483
+ Processing by DummyApplicationController#index as HTML
484
+ Completed 200 OK in 0ms (Views: 0.1ms)
485
+ Processing by DummyApplicationController#index as HTML
486
+ Completed 200 OK in 0ms (Views: 0.1ms)
487
+ Processing by DummyApplicationController#index as HTML
488
+ Completed 500 Internal Server Error in 0ms
489
+ Processing by DummyApplicationController#index as HTML
490
+ Completed 200 OK in 1ms (Views: 0.4ms)
491
+ Processing by DummyApplicationController#index as HTML
492
+ Completed 200 OK in 0ms (Views: 0.1ms)
493
+ Processing by DummyApplicationController#index as HTML
494
+ Completed 200 OK in 0ms (Views: 0.1ms)
495
+ Processing by DummyApplicationController#index as HTML
496
+ Completed 500 Internal Server Error in 0ms
497
+ Processing by DummyApplicationController#index as HTML
498
+ Completed 200 OK in 1ms (Views: 0.3ms)
499
+ Processing by DummyApplicationController#index as HTML
500
+ Completed 200 OK in 1ms (Views: 0.3ms)
501
+ Processing by DummyApplicationController#index as HTML
502
+ Completed 200 OK in 0ms (Views: 0.2ms)
503
+ Processing by DummyApplicationController#index as HTML
504
+ Completed 500 Internal Server Error in 0ms
505
+ Processing by DummyApplicationController#index as HTML
506
+ Completed 200 OK in 1ms (Views: 1.1ms)
507
+ Processing by DummyApplicationController#index as HTML
508
+ Completed 200 OK in 1ms (Views: 0.4ms)
509
+ Processing by DummyApplicationController#index as HTML
510
+ Completed 200 OK in 0ms (Views: 0.2ms)
511
+ Processing by DummyApplicationController#index as HTML
512
+ Completed 500 Internal Server Error in 0ms
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: company_scope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: '0.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Forkin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-02 00:00:00.000000000 Z
11
+ date: 2015-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -207,7 +207,7 @@ files:
207
207
  - lib/company_scope/version.rb
208
208
  - lib/custom/multi_company.rb
209
209
  - lib/generators/company_scope/install_generator.rb
210
- - lib/generators/company_scope/templates/company_scope.rb.erb
210
+ - lib/generators/company_scope/templates/migrations/create_companies.rb.erb
211
211
  - lib/subdomain_matcher.rb
212
212
  - log/test.log
213
213
  homepage: http://github.com/netflakes/company_scope
@@ -1,3 +0,0 @@
1
- CompanyScope.setup do |config|
2
-
3
- end