rails_multitenant 0.4.0 → 0.5.0

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: ff528a38b03db9f83d9665fc802a577fa10cc0cb
4
- data.tar.gz: d75fb03a0907977a6f18cf1fdc8a32a183179e88
3
+ metadata.gz: b26ac97762bed39380b26b1f08db3198f3d7d91f
4
+ data.tar.gz: ef72bdd22908dfc9e150c168655db4f42ae26738
5
5
  SHA512:
6
- metadata.gz: a3fd2430828d14280bd19414b20079814babd2ae01d67eb783b2130e2ddb029bde5521ee3dd6cec73ed2e7150637be99ab83c4d635db0d450f072ec28a3a8688
7
- data.tar.gz: dd556073c42ef36665285a03018e2042375b815195a879687ddbe8c65d79f643a612c359425d88d6f006e0f16813b34c3e95e4d519af82cf79409b8b4c5ce43a
6
+ metadata.gz: 046aaf25a46bd08c344718ec700344b441f3b8131667f4295e0b7c6cdd7a5d2de17320356f6a94b43a8ae68907b4071a59352b6bbf0f04edcf9e693093d25ad6
7
+ data.tar.gz: 2d1d346080098b3989eae5a089aa4a1d426081a1ce7413c718d81e60c2425076c9c2fc75387f7067414d873d55cbd29922ed94eb28206b871e241bc52b05d163
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ### 0.5.0
4
+ * Add `required` option to `multitenant_on` and `multitenant_on_model`.
5
+
3
6
  ### 0.4.0
4
7
  * Fix be_multitenant_on matcher to handle models that don't include the `RailsMultitenant::MultitenantModel` module.
5
8
  * Fix `context_entity_id_field` to work with inheritance.
data/README.md CHANGED
@@ -58,6 +58,9 @@ RailsMultitenant::GlobalContextRegistry.with_isolated_registry(organization_id:
58
58
  end
59
59
  ```
60
60
 
61
+ By default this adds an ActiveRecord validation to ensure the multi-tenant attribute is present but this can be disabled
62
+ by passing `required: false` to `multitenant_on`.
63
+
61
64
  ### Multi-tenancy Based on Associated Models
62
65
 
63
66
  The following model is multi-tenant based on an `Organization` model:
@@ -89,6 +92,9 @@ Organization.as_current_id(1) do
89
92
  end
90
93
  ```
91
94
 
95
+ By default this adds an ActiveRecord validation to ensure the tenant model is present but this can be disabled
96
+ by passing `required: false` to `multitenant_on_model`.
97
+
92
98
  ## Development
93
99
 
94
100
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -8,9 +8,9 @@ module RailsMultitenant
8
8
 
9
9
  module ClassMethods
10
10
 
11
- def multitenant_on(context_entity_id_field)
11
+ def multitenant_on(context_entity_id_field, required: true)
12
12
  self.context_entity_id_field = context_entity_id_field
13
- validates_presence_of context_entity_id_field
13
+ validates_presence_of(context_entity_id_field) if required
14
14
 
15
15
  context_entity = context_entity_id_field.to_s.gsub(/_id$/, '')
16
16
  scope_sym = "from_current_#{context_entity}".to_sym
@@ -26,9 +26,17 @@ module RailsMultitenant
26
26
  end
27
27
  end
28
28
 
29
- def multitenant_on_model(context_entity)
30
- multitenant_on("#{context_entity}_id".to_sym)
31
- belongs_to context_entity
29
+ def multitenant_on_model(context_entity, required: true)
30
+ multitenant_on("#{context_entity}_id".to_sym, required: required)
31
+
32
+ if ActiveRecord::VERSION::MAJOR < 5
33
+ belongs_to(context_entity)
34
+ else
35
+ # Rails 5 added required validation to belongs_to associations and
36
+ # an `optional` setting to disable it. We already do validation on
37
+ # the foreign key so we always disable the native Rails validation.
38
+ belongs_to(context_entity, optional: true)
39
+ end
32
40
  end
33
41
 
34
42
  def validates_multitenant_uniqueness_of(*attr_names)
@@ -1,3 +1,3 @@
1
1
  module RailsMultitenant
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
data/spec/db/schema.rb CHANGED
@@ -9,9 +9,18 @@ ActiveRecord::Schema.define(version: 0) do
9
9
  t.string :type
10
10
  end
11
11
 
12
+ create_table(:item_with_optional_orgs, force: true) do |t|
13
+ t.integer :organization_id
14
+ t.string :type
15
+ end
16
+
12
17
  create_table(:external_items, force: true) do |t|
13
18
  t.integer :external_organization_id
14
19
  end
20
+
21
+ create_table(:external_item_with_optional_orgs, force: true) do |t|
22
+ t.integer :external_organization_id
23
+ end
15
24
  end
16
25
 
17
26
  class Organization < ActiveRecord::Base
@@ -23,6 +32,11 @@ class Item < ActiveRecord::Base
23
32
  multitenant_on_model :organization
24
33
  end
25
34
 
35
+ class ItemWithOptionalOrg < ActiveRecord::Base
36
+ include RailsMultitenant::MultitenantModel
37
+ multitenant_on_model :organization, required: false
38
+ end
39
+
26
40
  class ItemSubtype < Item
27
41
 
28
42
  end
@@ -32,3 +46,8 @@ class ExternalItem < ActiveRecord::Base
32
46
  multitenant_on :external_organization_id
33
47
  end
34
48
 
49
+ class ExternalItemWithOptionalOrg < ActiveRecord::Base
50
+ include RailsMultitenant::MultitenantModel
51
+ multitenant_on :external_organization_id, required: false
52
+ end
53
+
@@ -0,0 +1,25 @@
1
+ include RailsMultitenant
2
+
3
+ describe ExternalItemWithOptionalOrg do
4
+
5
+ let!(:external_item_without_org) { as_external_org(nil) { described_class.create! } }
6
+
7
+ let!(:external_item_with_org) { as_external_org(1) { described_class.create! } }
8
+ let!(:external_item_with_other_org) { as_external_org(2) { described_class.create! } }
9
+
10
+ specify 'the nil org has the correct external items' do
11
+ as_external_org(nil) do
12
+ expect(described_class.all).to eq([external_item_without_org])
13
+ end
14
+ end
15
+
16
+ specify 'org1 has the correct external items' do
17
+ as_external_org(1) do
18
+ expect(described_class.all).to eq([external_item_with_org])
19
+ end
20
+ end
21
+
22
+ def as_external_org(id, &block)
23
+ GlobalContextRegistry.with_isolated_registry(external_organization_id: id, &block)
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ describe ItemWithOptionalOrg do
2
+
3
+ let!(:item_without_org) { without_org { described_class.create! } }
4
+
5
+ let!(:org) { Organization.create! }
6
+ let!(:item_with_org) { org.as_current { described_class.create! } }
7
+
8
+ let!(:other_org) { Organization.create! }
9
+ let!(:item_with_other_org) { other_org.as_current { described_class.create! } }
10
+
11
+ it "returns the correct items when no org is set" do
12
+ without_org do
13
+ expect(described_class.all).to eq([item_without_org])
14
+ end
15
+ end
16
+
17
+ it "returns the correct items when an org is set" do
18
+ org.as_current do
19
+ expect(described_class.all).to eq([item_with_org])
20
+ end
21
+ end
22
+
23
+ def without_org(&block)
24
+ GlobalContextRegistry.with_isolated_registry(&block)
25
+ end
26
+ end
data/spec/spec_helper.rb CHANGED
@@ -11,6 +11,11 @@ SimpleCov.start do
11
11
  add_filter 'spec'
12
12
  end
13
13
 
14
+ require 'active_record'
15
+ if ActiveRecord::VERSION::MAJOR >= 5
16
+ ActiveRecord::Base.belongs_to_required_by_default = true
17
+ end
18
+
14
19
  require 'logger'
15
20
  require 'database_cleaner'
16
21
  require 'rails_multitenant'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_multitenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Breault
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-28 00:00:00.000000000 Z
11
+ date: 2016-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,42 +28,30 @@ dependencies:
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '4.1'
34
- - - "<"
31
+ - - '='
35
32
  - !ruby/object:Gem::Version
36
- version: '5.1'
33
+ version: '4.2'
37
34
  type: :runtime
38
35
  prerelease: false
39
36
  version_requirements: !ruby/object:Gem::Requirement
40
37
  requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- version: '4.1'
44
- - - "<"
38
+ - - '='
45
39
  - !ruby/object:Gem::Version
46
- version: '5.1'
40
+ version: '4.2'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: activesupport
49
43
  requirement: !ruby/object:Gem::Requirement
50
44
  requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: '4.1'
54
- - - "<"
45
+ - - '='
55
46
  - !ruby/object:Gem::Version
56
- version: '5.1'
47
+ version: '4.2'
57
48
  type: :runtime
58
49
  prerelease: false
59
50
  version_requirements: !ruby/object:Gem::Requirement
60
51
  requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- version: '4.1'
64
- - - "<"
52
+ - - '='
65
53
  - !ruby/object:Gem::Version
66
- version: '5.1'
54
+ version: '4.2'
67
55
  - !ruby/object:Gem::Dependency
68
56
  name: coveralls
69
57
  requirement: !ruby/object:Gem::Requirement
@@ -176,9 +164,11 @@ files:
176
164
  - spec/db/database.yml
177
165
  - spec/db/schema.rb
178
166
  - spec/external_item_spec.rb
167
+ - spec/external_item_with_optional_org_spec.rb
179
168
  - spec/global_context_registry_spec.rb
180
169
  - spec/item_spec.rb
181
170
  - spec/item_subtype_spec.rb
171
+ - spec/item_with_optional_org_spec.rb
182
172
  - spec/spec_helper.rb
183
173
  homepage: https://github.com/salsify/rails_multitenant
184
174
  licenses:
@@ -209,7 +199,9 @@ test_files:
209
199
  - spec/db/database.yml
210
200
  - spec/db/schema.rb
211
201
  - spec/external_item_spec.rb
202
+ - spec/external_item_with_optional_org_spec.rb
212
203
  - spec/global_context_registry_spec.rb
213
204
  - spec/item_spec.rb
214
205
  - spec/item_subtype_spec.rb
206
+ - spec/item_with_optional_org_spec.rb
215
207
  - spec/spec_helper.rb