company_scope 0.1.3 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -8
- data/lib/company_scope/base.rb +0 -2
- data/lib/company_scope/guardian.rb +2 -2
- 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: 78d74779542e73e301d7e76aa127dd7fd66cf662
|
4
|
+
data.tar.gz: ef46ca27b38a368b41dd64d168194904a6a3b925
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bc0411012a43a13235521568587a663986d0952dce1899b8d80ee0231db6bcb3c613ba292e310eba024b93196cb4f9db2dccfdbdce8277b724b4f60b4b5db21
|
7
|
+
data.tar.gz: da8def416750d32902413b3a7dea14a1619efbe035129f34db0eb532f2e90823f93b76a4624bd787761dd57aa618d7118b5d681c37e4095866dbc656f1a0edab
|
data/README.md
CHANGED
@@ -68,15 +68,18 @@ The domain 'lvh.me' points to 127.0.0.1 and is therefore an ideal candidate for
|
|
68
68
|
|
69
69
|
NB: The middleware currently uses a regex to ensure the domain name can only obtain the following:
|
70
70
|
|
71
|
-
* A-Z
|
72
|
-
* a-z
|
73
|
-
* 0-9
|
71
|
+
* A-Z, a-z, 0-9
|
74
72
|
|
75
|
-
The middleware then uses the column called "
|
73
|
+
The middleware then uses the column called "company_name" to retrieve the company by the subdomain.
|
76
74
|
|
77
|
-
|
78
|
-
|
79
|
-
|
75
|
+
The name is also upcased - which needs to be handled by the model you use for scoping! This just keeps the name clean and simple. We would strongly recommend you to have a proper index on the company_name field.
|
76
|
+
|
77
|
+
NB: If you are using a different model and don't like the idea of using the company_name field you can simply
|
78
|
+
use the ruby alias method.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
alias_method :company_name, :your_account_name_method
|
82
|
+
```
|
80
83
|
|
81
84
|
The method below is included in the Controller stack (see notes further down), and retrieves
|
82
85
|
the company object the request object. The Rack Middleware "Rack::MultiCompany" injects this
|
@@ -143,13 +146,17 @@ NB: The "CompanyScope" gem does not handle the process of adding migrations or c
|
|
143
146
|
|
144
147
|
### Scoping your models ###
|
145
148
|
|
146
|
-
* The "acts_as_guardian" method injects the behaviour required for the scoping model.
|
149
|
+
* The "acts_as_guardian" method injects the behaviour required for the scoping model. The model
|
150
|
+
needs to have a string column that is called by the "model"_name i.e. 'company_name'. The gem
|
151
|
+
adds a uniqueness validator. NB to ensure this will not cause race conditions at the DB level
|
152
|
+
you really need to add an index for this column.
|
147
153
|
|
148
154
|
```ruby
|
149
155
|
class Company < ActiveRecord::Base
|
150
156
|
|
151
157
|
acts_as_guardian
|
152
158
|
|
159
|
+
# NB - the gem adds a uniqueness validator for the company_name field
|
153
160
|
...
|
154
161
|
|
155
162
|
end
|
@@ -174,6 +181,11 @@ class User < ActiveRecord::Base
|
|
174
181
|
end
|
175
182
|
```
|
176
183
|
|
184
|
+
### The Gem is currently being used in Rails 4 and Rails-API apps and is tested against Postgres,
|
185
|
+
using UUID based ID/primary keys ###
|
186
|
+
|
187
|
+
### It should work with other databases such as MySQL without any issues ###
|
188
|
+
|
177
189
|
|
178
190
|
## Development
|
179
191
|
|
data/lib/company_scope/base.rb
CHANGED
@@ -15,7 +15,6 @@ module CompanyScope
|
|
15
15
|
|
16
16
|
validates_presence_of "#{tenant.to_s}_id".to_sym #:company_id
|
17
17
|
|
18
|
-
#default_scope { where("#{self.table_name}.company_id = ?", Company.current_id) }
|
19
18
|
default_scope { where("#{self.table_name}.#{tenant.to_s.underscore}_id = ?",
|
20
19
|
Module.const_get("#{tenant.to_s.classify}").current_id) }
|
21
20
|
|
@@ -33,7 +32,6 @@ module CompanyScope
|
|
33
32
|
|
34
33
|
before_destroy do |obj|
|
35
34
|
# force company to be correct for current_user
|
36
|
-
#raise ::CompanyScope::Control::CompanyAccessViolationError unless obj.company_id == Company.current_id
|
37
35
|
raise ::CompanyScope::Control::CompanyAccessViolationError unless
|
38
36
|
obj.attributes["#{tenant.to_s.underscore}_id"] == Module.const_get("#{tenant.to_s.classify}").current_id
|
39
37
|
true
|
@@ -11,8 +11,8 @@ module CompanyScope
|
|
11
11
|
#
|
12
12
|
def acts_as_guardian
|
13
13
|
#
|
14
|
-
#
|
15
|
-
|
14
|
+
# ensure the column that carries the company unique id/subdomain is unique
|
15
|
+
validates_uniqueness_of :company_name
|
16
16
|
#
|
17
17
|
def current_id=(id)
|
18
18
|
RequestStore.store[:default_scope_company_id] = id
|