company_scope 0.1.1 → 0.1.2
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 +4 -4
- data/README.md +90 -11
- data/lib/company_scope/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 736068a8a448f00bbc6691173a4a6fa89c086d41
|
4
|
+
data.tar.gz: ed3357ed9447e8018760334dad8b5aa0febe463c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b70cb263376ee416321a517c0f2c72653cee0cdfc59f4e3ffed8bb9e41454c38b8b3638b42e4c85695c797fe494b7db3eb0e58138cfdfcf398e470aebcda3fa
|
7
|
+
data.tar.gz: ad8b3a6fe5b67ba1a8e4a114ecf7106582b1fb3cc94ca945dcff594f39b67838c49e4c1957716b89728b324d5274cb0803ce0bf2ab46ebfc696b9896bcdea4a6
|
data/README.md
CHANGED
@@ -31,30 +31,109 @@ Or install it yourself as:
|
|
31
31
|
|
32
32
|
Getting started
|
33
33
|
===============
|
34
|
-
There are
|
34
|
+
There are three main steps in adding multi-tenancy/company to your app with company_scope:
|
35
35
|
|
36
|
-
1.
|
37
|
-
2.
|
36
|
+
1. Decide on a process of determining the company/account. Such as using the sub-domain.
|
37
|
+
2. Setting the current company and controller based setup.
|
38
|
+
3. Scoping your models.
|
38
39
|
|
39
|
-
Setting the current tenant
|
40
|
-
--------------------------
|
41
|
-
There are three ways to set the current tenant:
|
42
40
|
|
43
|
-
|
44
|
-
2. by setting the current tenant in the controller, and
|
45
|
-
3. by setting the current tenant for a block.
|
41
|
+
### Decide on a process of determining the company/account ###
|
46
42
|
|
47
|
-
|
43
|
+
In the current version a helper_method called "current_company" is added to the Controller,
|
44
|
+
where you add the method "company_setup". You have therefore two choices. Either you use your
|
45
|
+
own process to set the instance of "Company" into "request.env".
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
def current_company
|
49
|
+
request.env["COMPANY_ID"]
|
50
|
+
end
|
51
|
+
```
|
52
|
+
An example of this is to use "Rack Middleware" - see the method excerpt below:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
def call(env)
|
56
|
+
request = Rack::Request.new(env)
|
57
|
+
domain = request.host.split('.').first.upcase
|
58
|
+
env["COMPANY_ID"] = your_custom_method_to_retrieve_company_from_subdomain(domain)
|
59
|
+
response = @app.call(env)
|
60
|
+
response
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
The next version of "company_scope", will have the Rack::Middleware call integrated and the
|
65
|
+
ability to opt out of using it.
|
66
|
+
|
67
|
+
Alternatively you can use your own process for determining the "current_company" and override this
|
68
|
+
method in your application controller, providing you declare this after the "company_setup" method,
|
69
|
+
which is detailed in the next step.
|
70
|
+
|
71
|
+
|
72
|
+
### Setting the current company and controller based setup ###
|
48
73
|
|
49
74
|
```ruby
|
50
75
|
class ApplicationController < ActionController::Base
|
51
|
-
|
76
|
+
|
77
|
+
company_setup
|
78
|
+
|
79
|
+
set_scoping_class :company
|
80
|
+
|
81
|
+
acts_as_company_filter
|
82
|
+
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
The above three methods need to be added to the Rails Controllers. For small systems they
|
87
|
+
will typically be added to the ApplicationController. However they can be split into
|
88
|
+
child-controllers dependent on the layout of the application.
|
89
|
+
|
90
|
+
All Controllers that inherit from the Controller that implements the "acts_as_company_filter"
|
91
|
+
will have an around filter applied that set the Company class attribute required for the scoping
|
92
|
+
process.
|
93
|
+
|
94
|
+
The "company_setup" method adds some helper methods that are available to all child controllers.
|
95
|
+
|
96
|
+
* company_setup
|
97
|
+
* set_scoping_class :company
|
98
|
+
* acts_as_company_filter
|
99
|
+
|
100
|
+
The "set_scoping_class :company" method tells CompanyScope that we have a model called Company, and
|
101
|
+
it will be the model that all others will be scoped with.
|
102
|
+
The method parameter defaults to :company but can be another model of your choosing such as Account.
|
103
|
+
Each model that is scoped by the Company needs to have the company_id column.
|
104
|
+
|
105
|
+
NB: The "CompanyScope" gem does not handle the process of adding migrations or changes to the DB.
|
106
|
+
|
107
|
+
|
108
|
+
### Scoping your models ###
|
109
|
+
|
110
|
+
* The "acts_as_guardian" method injects the behaviour required for the scoping model.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
class Company < ActiveRecord::Base
|
114
|
+
|
115
|
+
acts_as_guardian
|
116
|
+
|
117
|
+
...
|
118
|
+
|
52
119
|
end
|
53
120
|
```
|
54
121
|
|
122
|
+
* Each class to be scoped needs to have the "acts_as_company :account" method. The parameter ":account"
|
123
|
+
defaults to :company if left blank. This can be any class/name of your choosing - the parameter needs
|
124
|
+
to be a underscored version of the Class name as a symbol.
|
55
125
|
|
126
|
+
```ruby
|
127
|
+
class User < ActiveRecord::Base
|
128
|
+
|
129
|
+
acts_as_company :account # Defaults to :company if left blank!
|
56
130
|
|
131
|
+
# NB - don't add 'belongs_to :company' or validation
|
132
|
+
# of the 'company_id' since the gem does this for you.
|
57
133
|
|
134
|
+
...
|
135
|
+
end
|
136
|
+
```
|
58
137
|
|
59
138
|
|
60
139
|
## Development
|