company_scope 0.1.0 → 0.1.1

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: 76e4908c7c91d390e44c66f6c18891493831d1b3
4
- data.tar.gz: bac3b43ef130a3f422b56e6129ec7227dd484f7d
3
+ metadata.gz: 20a3400d84425d8ce823542bc038abb96dc43995
4
+ data.tar.gz: 45ce497b1fb10c375f22f58f42349199899720ef
5
5
  SHA512:
6
- metadata.gz: c28bc166bcdddc387ffb508f0accb04d0151f9bcac62b84d5a700e505f60a9ed6519acfd73cd135bd6ccd415b9538de466dbb09d395feaea75260910add0d51d
7
- data.tar.gz: 497ad6e99a12acee86b0858caab13273478142b7e3a260aef664ff4d7f3bc821e1e4b6a75b2ecf435551e888639c274029c8c9faae4ec539de0bcc206be0b0bb
6
+ metadata.gz: 2c50413c3b5aa0106acaf68ff4e55694866b459e9c7f020f37c8e3c490666d76344980c5794e6646d13eacddbe330367a20e56b6122eb1f5cc9f4371777309f9
7
+ data.tar.gz: 107dd39ae499575d1d6253929f00a0932e78b1e68931b99374868527f15ae5878365c5928f07d85bc7c4fc3b2f3ac5a7f83b6545253231b8f225f0a5898953ef
data/README.md CHANGED
@@ -29,8 +29,33 @@ Or install it yourself as:
29
29
 
30
30
  ## Usage
31
31
 
32
- NB: This gem is currently under development and has not been published officially!
33
- (use at your own risk..)
32
+ Getting started
33
+ ===============
34
+ There are two steps in adding multi-tenancy/company to your app with company_scope:
35
+
36
+ 1. setting the current tenant and
37
+ 2. scoping your models.
38
+
39
+ Setting the current tenant
40
+ --------------------------
41
+ There are three ways to set the current tenant:
42
+
43
+ 1. by using the subdomain to lookup the current tenant,
44
+ 2. by setting the current tenant in the controller, and
45
+ 3. by setting the current tenant for a block.
46
+
47
+ ### Use the subdomain to lookup the current tenant ###
48
+
49
+ ```ruby
50
+ class ApplicationController < ActionController::Base
51
+ set_current_tenant_by_subdomain(:account, :subdomain)
52
+ end
53
+ ```
54
+
55
+
56
+
57
+
58
+
34
59
 
35
60
  ## Development
36
61
 
@@ -9,28 +9,33 @@ module CompanyScope
9
9
  #
10
10
  module CompanyScopeClassMethods
11
11
  #
12
- def acts_as_company
12
+ def acts_as_company(tenant = :company)
13
13
 
14
- belongs_to :company
14
+ belongs_to tenant.to_s.underscore.to_sym
15
15
 
16
- validates_presence_of :company_id
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) }
18
+ #default_scope { where("#{self.table_name}.company_id = ?", Company.current_id) }
19
+ default_scope { where("#{self.table_name}.#{tenant.to_s.underscore}_id = ?",
20
+ Module.const_get("#{tenant.to_s.classify}").current_id) }
19
21
 
20
22
  # - on creation we make sure the company_id is set!
21
23
  before_validation(on: :create) do |obj|
22
- obj.company_id = Company.current_id
24
+ obj.send(eval(":#{tenant.to_s.underscore}_id="), Module.const_get("#{tenant.to_s.classify}").current_id)
23
25
  end
24
26
 
25
27
  before_save do |obj|
26
28
  # catch each time someone attempts to violate the company relationship!
27
- raise ::CompanyScope::Control::CompanyAccessViolationError unless obj.company_id == Company.current_id
29
+ raise ::CompanyScope::Control::CompanyAccessViolationError unless
30
+ obj.attributes["#{tenant.to_s.underscore}_id"] == Module.const_get("#{tenant.to_s.classify}").current_id
28
31
  true
29
32
  end
30
33
 
31
34
  before_destroy do |obj|
32
35
  # force company to be correct for current_user
33
- raise ::CompanyScope::Control::CompanyAccessViolationError unless obj.company_id == Company.current_id
36
+ #raise ::CompanyScope::Control::CompanyAccessViolationError unless obj.company_id == Company.current_id
37
+ raise ::CompanyScope::Control::CompanyAccessViolationError unless
38
+ obj.attributes["#{tenant.to_s.underscore}_id"] == Module.const_get("#{tenant.to_s.classify}").current_id
34
39
  true
35
40
  end
36
41
  end
@@ -10,24 +10,31 @@ module CompanyScope
10
10
  end
11
11
 
12
12
  module FilterMethods
13
-
13
+
14
14
  def current_company
15
15
  request.env["COMPANY_ID"] # a default that is best overridden in the application controller..
16
16
  end
17
-
17
+
18
18
  def filter_by_current_company_scope
19
- Company.current_id = current_company.id
19
+ scope_class.current_id = current_company.id
20
20
  yield
21
21
  ensure
22
- Company.current_id = nil
22
+ scope_class.current_id = nil
23
23
  end
24
24
  end
25
25
 
26
26
  module FilterClassMethods
27
+ #
28
+ def set_scoping_class(scope_model = :company)
29
+ self.class_eval do
30
+ cattr_accessor :scope_class
31
+ end
32
+ self.scope_class = scope_model.to_s.camelcase.constantize
33
+ end
27
34
  #
28
35
  def company_setup
29
36
  helper_method :current_company
30
- end
37
+ end
31
38
  #
32
39
  def acts_as_company_filter
33
40
  around_filter :filter_by_current_company_scope
@@ -1,3 +1,3 @@
1
1
  module CompanyScope
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.1.0
4
+ version: 0.1.1
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-03-18 00:00:00.000000000 Z
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler