strongbolt 0.3.12 → 0.3.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +56 -0
- data/.rubocop_todo.yml +91 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +18 -2
- data/Rakefile +1 -1
- data/app/controllers/strongbolt/capabilities_controller.rb +36 -45
- data/app/controllers/strongbolt/roles_controller.rb +39 -47
- data/app/controllers/strongbolt/security_controller.rb +2 -3
- data/app/controllers/strongbolt/user_groups_controller.rb +48 -54
- data/app/controllers/strongbolt/user_groups_users_controller.rb +2 -4
- data/app/controllers/strongbolt_controller.rb +1 -1
- data/circle.yml +13 -0
- data/lib/generators/strongbolt/fix_generator.rb +5 -6
- data/lib/generators/strongbolt/fix_unique_group_members_generator.rb +2 -3
- data/lib/generators/strongbolt/indexes_generator.rb +3 -4
- data/lib/generators/strongbolt/install_generator.rb +8 -9
- data/lib/generators/strongbolt/templates/fix_unique_group_members.rb +1 -1
- data/lib/generators/strongbolt/templates/indexes.rb +1 -1
- data/lib/generators/strongbolt/templates/migration.rb +11 -12
- data/lib/generators/strongbolt/templates/strongbolt.rb +1 -1
- data/lib/generators/strongbolt/views_generator.rb +4 -4
- data/lib/strongbolt.rb +51 -54
- data/lib/strongbolt/base.rb +1 -1
- data/lib/strongbolt/bolted.rb +12 -13
- data/lib/strongbolt/bolted_controller.rb +46 -57
- data/lib/strongbolt/capabilities_role.rb +5 -5
- data/lib/strongbolt/capability.rb +32 -31
- data/lib/strongbolt/configuration.rb +18 -19
- data/lib/strongbolt/controllers/url_helpers.rb +5 -5
- data/lib/strongbolt/engine.rb +9 -9
- data/lib/strongbolt/errors.rb +4 -4
- data/lib/strongbolt/generators/migration.rb +4 -6
- data/lib/strongbolt/helpers.rb +5 -7
- data/lib/strongbolt/rails/routes.rb +4 -4
- data/lib/strongbolt/role.rb +11 -12
- data/lib/strongbolt/roles_user_group.rb +5 -5
- data/lib/strongbolt/rspec.rb +2 -2
- data/lib/strongbolt/rspec/user.rb +13 -15
- data/lib/strongbolt/tenantable.rb +78 -80
- data/lib/strongbolt/user_abilities.rb +44 -54
- data/lib/strongbolt/user_group.rb +8 -10
- data/lib/strongbolt/user_groups_user.rb +6 -6
- data/lib/strongbolt/version.rb +1 -1
- data/lib/tasks/strongbolt_tasks.rake +4 -4
- data/spec/controllers/strongbolt/capabilities_controller_spec.rb +28 -45
- data/spec/controllers/strongbolt/roles_controller_spec.rb +39 -72
- data/spec/controllers/strongbolt/user_groups_controller_spec.rb +34 -65
- data/spec/controllers/strongbolt/user_groups_users_controller_spec.rb +11 -19
- data/spec/controllers/without_authorization_controller_spec.rb +5 -5
- data/spec/dummy/app/controllers/posts_controller.rb +2 -2
- data/spec/dummy/app/controllers/test_controller.rb +1 -1
- data/spec/dummy/app/controllers/without_authorization_controller.rb +1 -1
- data/spec/dummy/bin/rails +1 -1
- data/spec/dummy/config.ru +1 -1
- data/spec/dummy/config/application.rb +4 -5
- data/spec/dummy/config/initializers/cookies_serializer.rb +1 -1
- data/spec/dummy/config/initializers/strongbolt.rb +2 -2
- data/spec/dummy/config/routes.rb +1 -3
- data/spec/dummy/db/migrate/20150630212236_create_strongbolt_tables.rb +9 -10
- data/spec/dummy/db/migrate/20150630212251_create_strongbolt_tables_indexes.rb +2 -2
- data/spec/dummy/db/migrate/20160531110509_fix_unique_group_members.rb +1 -1
- data/spec/fabricators/capability_fabricator.rb +4 -4
- data/spec/fabricators/role_fabricator.rb +3 -3
- data/spec/fabricators/user_fabricator.rb +2 -2
- data/spec/fabricators/user_group_fabricator.rb +3 -3
- data/spec/fixtures/application.rb +6 -3
- data/spec/fixtures/controllers.rb +1 -1
- data/spec/spec_helper.rb +7 -8
- data/spec/strongbolt/bolted_controller_spec.rb +110 -208
- data/spec/strongbolt/bolted_spec.rb +26 -40
- data/spec/strongbolt/capability_spec.rb +72 -86
- data/spec/strongbolt/configuration_spec.rb +33 -46
- data/spec/strongbolt/controllers/url_helpers_spec.rb +7 -9
- data/spec/strongbolt/helpers_spec.rb +14 -16
- data/spec/strongbolt/role_spec.rb +32 -35
- data/spec/strongbolt/tenantable_spec.rb +88 -86
- data/spec/strongbolt/user_abilities_multiple_tenants_spec.rb +29 -34
- data/spec/strongbolt/user_abilities_spec.rb +142 -188
- data/spec/strongbolt/user_group_spec.rb +14 -14
- data/spec/strongbolt/users_tenant_spec.rb +10 -12
- data/spec/strongbolt_spec.rb +53 -73
- data/spec/support/controller_macros.rb +1 -3
- data/spec/support/db_setup.rb +31 -25
- data/spec/support/helpers.rb +12 -12
- data/spec/support/transactional_specs.rb +1 -3
- data/strongbolt.gemspec +14 -12
- metadata +20 -3
@@ -1,42 +1,35 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Strongbolt::Configuration do
|
4
|
-
|
5
|
-
|
6
4
|
#
|
7
5
|
# User class
|
8
6
|
#
|
9
|
-
describe
|
10
|
-
|
11
|
-
|
12
|
-
expect(Strongbolt::Configuration.user_class).to eq "User"
|
7
|
+
describe 'user class' do
|
8
|
+
it 'should default to User' do
|
9
|
+
expect(Strongbolt::Configuration.user_class).to eq 'User'
|
13
10
|
end
|
14
11
|
|
15
|
-
context
|
16
|
-
before { Strongbolt::Configuration.user_class =
|
17
|
-
after { Strongbolt::Configuration.user_class =
|
12
|
+
context 'when setting it' do
|
13
|
+
before { Strongbolt::Configuration.user_class = 'Account' }
|
14
|
+
after { Strongbolt::Configuration.user_class = 'User' }
|
18
15
|
|
19
|
-
it
|
20
|
-
expect(Strongbolt::Configuration.user_class).to eq
|
16
|
+
it 'should give it' do
|
17
|
+
expect(Strongbolt::Configuration.user_class).to eq 'Account'
|
21
18
|
end
|
22
19
|
end
|
23
|
-
|
24
20
|
end
|
25
21
|
|
26
|
-
|
27
|
-
|
28
22
|
#
|
29
23
|
# Setting up tenants
|
30
24
|
#
|
31
25
|
describe 'tenants=' do
|
32
|
-
|
33
26
|
before do
|
34
|
-
define_model
|
35
|
-
self.table_name =
|
27
|
+
define_model 'Model' do
|
28
|
+
self.table_name = 'models'
|
36
29
|
end
|
37
30
|
|
38
|
-
define_model
|
39
|
-
self.table_name =
|
31
|
+
define_model 'OtherModel' do
|
32
|
+
self.table_name = 'models'
|
40
33
|
end
|
41
34
|
|
42
35
|
expect(Model).to receive(:send).with :tenant
|
@@ -44,76 +37,70 @@ describe Strongbolt::Configuration do
|
|
44
37
|
end
|
45
38
|
after { Strongbolt::Configuration.tenants = [] }
|
46
39
|
|
47
|
-
it
|
48
|
-
Strongbolt::Configuration.tenants =
|
40
|
+
it 'should tenant the models' do
|
41
|
+
Strongbolt::Configuration.tenants = 'Model', OtherModel, Model
|
49
42
|
expect(Strongbolt::Configuration.tenants).to eq [Model, OtherModel]
|
50
43
|
end
|
51
|
-
|
52
44
|
end
|
53
45
|
|
54
46
|
#
|
55
47
|
# Configuring Capability Models
|
56
48
|
#
|
57
|
-
describe
|
49
|
+
describe 'models=' do
|
58
50
|
before do
|
59
|
-
Strongbolt::Configuration.models =
|
51
|
+
Strongbolt::Configuration.models = 'OtherModel', 'Model'
|
60
52
|
end
|
61
53
|
after do
|
62
54
|
Strongbolt::Capability.models = nil
|
63
55
|
end
|
64
56
|
|
65
|
-
it
|
66
|
-
expect(Strongbolt::Capability.models).to eq [
|
57
|
+
it 'should set Capability::Models' do
|
58
|
+
expect(Strongbolt::Capability.models).to eq ['Model', 'OtherModel', 'Strongbolt::Capability', 'Strongbolt::Role', 'Strongbolt::UserGroup', 'Strongbolt::UsersTenant']
|
67
59
|
end
|
68
60
|
|
69
|
-
context
|
61
|
+
context 'when adding other models' do
|
70
62
|
before do
|
71
|
-
Strongbolt::Configuration.models =
|
63
|
+
Strongbolt::Configuration.models = 'Model', 'LastModel'
|
72
64
|
end
|
73
65
|
|
74
|
-
it
|
75
|
-
expect(Strongbolt::Capability.models).to eq [
|
66
|
+
it 'should merge with current models' do
|
67
|
+
expect(Strongbolt::Capability.models).to eq ['LastModel', 'Model', 'OtherModel', 'Strongbolt::Capability', 'Strongbolt::Role', 'Strongbolt::UserGroup', 'Strongbolt::UsersTenant']
|
76
68
|
end
|
77
69
|
end
|
78
70
|
|
79
|
-
context
|
71
|
+
context 'when adding 1 model' do
|
80
72
|
before do
|
81
|
-
Strongbolt::Configuration.models =
|
73
|
+
Strongbolt::Configuration.models = 'BottomModel'
|
82
74
|
end
|
83
75
|
|
84
|
-
it
|
85
|
-
expect(Strongbolt::Capability.models).to eq [
|
76
|
+
it 'should merge with current models' do
|
77
|
+
expect(Strongbolt::Capability.models).to eq ['BottomModel', 'Model', 'OtherModel', 'Strongbolt::Capability', 'Strongbolt::Role', 'Strongbolt::UserGroup', 'Strongbolt::UsersTenant']
|
86
78
|
end
|
87
79
|
end
|
88
|
-
end
|
89
|
-
|
90
|
-
|
80
|
+
end # /models=
|
91
81
|
|
92
82
|
#
|
93
83
|
# Setting default permissions
|
94
84
|
#
|
95
|
-
describe
|
96
|
-
|
85
|
+
describe 'default_capabilities=' do
|
97
86
|
before do
|
98
87
|
Strongbolt::Configuration.default_capabilities = [
|
99
|
-
{:
|
100
|
-
{:
|
88
|
+
{ model: 'User', actions: :all },
|
89
|
+
{ model: 'Model', actions: 'find' }
|
101
90
|
]
|
102
91
|
end
|
103
92
|
after do
|
104
93
|
Strongbolt::Configuration.default_capabilities = []
|
105
94
|
end
|
106
95
|
|
107
|
-
it
|
96
|
+
it 'should return 5 Capabilities' do
|
108
97
|
expect(Strongbolt::Configuration.default_capabilities.size).to eq 5
|
109
98
|
end
|
110
99
|
|
111
|
-
it
|
100
|
+
it 'should return Capability' do
|
112
101
|
Strongbolt::Configuration.default_capabilities.each do |c|
|
113
102
|
expect(c).to be_a Strongbolt::Capability
|
114
103
|
end
|
115
104
|
end
|
116
|
-
|
117
105
|
end
|
118
|
-
|
119
106
|
end
|
@@ -1,10 +1,9 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'strongbolt/controllers/url_helpers'
|
3
3
|
|
4
4
|
module Strongbolt
|
5
5
|
module Controllers
|
6
6
|
describe UrlHelpers do
|
7
|
-
|
8
7
|
let(:helpersClass) do
|
9
8
|
Class.new do
|
10
9
|
def main_app; end
|
@@ -19,16 +18,15 @@ module Strongbolt
|
|
19
18
|
|
20
19
|
it { should respond_to :new_role_path }
|
21
20
|
|
22
|
-
describe
|
23
|
-
let(:main_app) { double(
|
24
|
-
|
25
|
-
it
|
21
|
+
describe 'edit_role_path' do
|
22
|
+
let(:main_app) { double('main_app', edit_strongbolt_role_path: true) }
|
23
|
+
|
24
|
+
it 'should call new_strongbolt_role_path on the main app' do
|
26
25
|
expect(helpers).to receive(:main_app).and_return main_app
|
27
26
|
expect(main_app).to receive(:edit_strongbolt_role_path).with 2
|
28
27
|
helpers.edit_role_path 2
|
29
28
|
end
|
30
29
|
end
|
31
|
-
|
32
30
|
end
|
33
31
|
end
|
34
|
-
end
|
32
|
+
end
|
@@ -1,12 +1,11 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'strongbolt/helpers'
|
3
3
|
|
4
4
|
describe Strongbolt::Helpers do
|
5
|
-
|
6
5
|
before do
|
7
6
|
@user = User.create!
|
8
7
|
|
9
|
-
define(
|
8
|
+
define('Helper', Object) do
|
10
9
|
include Strongbolt::Helpers
|
11
10
|
end
|
12
11
|
|
@@ -19,25 +18,24 @@ describe Strongbolt::Helpers do
|
|
19
18
|
|
20
19
|
let(:user) { @user }
|
21
20
|
let(:helper) { Helper.new }
|
22
|
-
|
23
|
-
describe
|
21
|
+
|
22
|
+
describe 'can?' do
|
24
23
|
before do
|
25
|
-
expect(user).to receive(:can?).with :find,
|
24
|
+
expect(user).to receive(:can?).with :find, 'me'
|
26
25
|
end
|
27
26
|
|
28
|
-
it
|
29
|
-
helper.can?(:find) {
|
27
|
+
it 'should call the user method' do
|
28
|
+
helper.can?(:find) { 'me' }
|
30
29
|
end
|
31
30
|
end
|
32
|
-
|
33
|
-
describe
|
31
|
+
|
32
|
+
describe 'cannot?' do
|
34
33
|
before do
|
35
|
-
expect(user).to receive(:can?).with :find,
|
34
|
+
expect(user).to receive(:can?).with :find, 'me'
|
36
35
|
end
|
37
36
|
|
38
|
-
it
|
39
|
-
helper.cannot? :find,
|
37
|
+
it 'should call the user method' do
|
38
|
+
helper.cannot? :find, 'me'
|
40
39
|
end
|
41
40
|
end
|
42
|
-
|
43
|
-
end
|
41
|
+
end
|
@@ -1,9 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
module Strongbolt
|
4
|
-
|
5
4
|
describe Role do
|
6
|
-
|
7
5
|
let(:role) { Role.new name: 'Moderator' }
|
8
6
|
|
9
7
|
subject { role }
|
@@ -12,79 +10,78 @@ module Strongbolt
|
|
12
10
|
|
13
11
|
it { is_expected.to validate_presence_of :name }
|
14
12
|
|
15
|
-
it {
|
16
|
-
.
|
13
|
+
it {
|
14
|
+
is_expected.to have_many(:roles_user_groups).class_name('Strongbolt::RolesUserGroup')
|
15
|
+
.dependent :restrict_with_exception
|
16
|
+
}
|
17
17
|
it { is_expected.to have_many(:user_groups).through :roles_user_groups }
|
18
18
|
|
19
|
-
it { is_expected.to have_many(:users).through :user_groups
|
20
|
-
|
21
|
-
it {
|
22
|
-
.
|
19
|
+
it { is_expected.to have_many(:users).through :user_groups }
|
20
|
+
|
21
|
+
it {
|
22
|
+
is_expected.to have_many(:capabilities_roles).class_name('Strongbolt::CapabilitiesRole')
|
23
|
+
.dependent :delete_all
|
24
|
+
}
|
23
25
|
it { is_expected.to have_many(:capabilities).through :capabilities_roles }
|
24
26
|
|
25
|
-
it { is_expected.to belong_to(:parent).class_name(
|
27
|
+
it { is_expected.to belong_to(:parent).class_name('Strongbolt::Role') }
|
26
28
|
|
27
|
-
describe
|
28
|
-
|
29
|
+
describe 'inherited capabilities' do
|
29
30
|
before do
|
30
31
|
# A family
|
31
|
-
grandfather = Role.create! name:
|
32
|
-
father = Role.create! name:
|
33
|
-
sibling = Role.create! name:
|
32
|
+
grandfather = Role.create! name: 'GrandFather'
|
33
|
+
father = Role.create! name: 'Father', parent: grandfather
|
34
|
+
sibling = Role.create! name: 'Sibling'
|
34
35
|
role.parent = father
|
35
36
|
role.save!
|
36
|
-
child = Role.create! name:
|
37
|
+
child = Role.create! name: 'Child', parent: role
|
37
38
|
|
38
39
|
# Some capabilities
|
39
40
|
begin
|
40
|
-
role.capabilities.create! model:
|
41
|
+
role.capabilities.create! model: 'Model', action: 'create'
|
41
42
|
rescue => e
|
42
43
|
puts e.record.capabilities_roles[0].errors.full_messages
|
43
44
|
end
|
44
|
-
child.capabilities.create! model:
|
45
|
-
@inherited1 = father.capabilities.create! model:
|
46
|
-
@inherited2 = grandfather.capabilities.create! model:
|
47
|
-
sibling.capabilities.create! model:
|
45
|
+
child.capabilities.create! model: 'Model', action: 'destroy'
|
46
|
+
@inherited1 = father.capabilities.create! model: 'Model', action: 'update'
|
47
|
+
@inherited2 = grandfather.capabilities.create! model: 'Model', action: 'find'
|
48
|
+
sibling.capabilities.create! model: 'User', action: 'find'
|
48
49
|
end
|
49
50
|
|
50
51
|
let(:inherited_capabilities) { role.inherited_capabilities }
|
51
52
|
|
52
|
-
it
|
53
|
+
it 'should have 2 inherited_capabilities' do
|
53
54
|
expect(inherited_capabilities.size).to eq 2
|
54
55
|
end
|
55
56
|
|
56
|
-
it
|
57
|
+
it 'should have the right ones' do
|
57
58
|
expect(inherited_capabilities).to include @inherited1
|
58
59
|
expect(inherited_capabilities).to include @inherited2
|
59
60
|
end
|
60
|
-
|
61
61
|
end
|
62
62
|
|
63
|
-
describe 'destroy' do |
|
63
|
+
describe 'destroy' do |_variable|
|
64
64
|
before { role.save! }
|
65
65
|
|
66
|
-
context
|
67
|
-
before { role.user_groups << UserGroup.create!(name:
|
66
|
+
context 'when have user groups' do
|
67
|
+
before { role.user_groups << UserGroup.create!(name: 'User Group') }
|
68
68
|
|
69
|
-
it
|
69
|
+
it 'should raise error when destroy' do
|
70
70
|
expect do
|
71
71
|
role.destroy
|
72
72
|
end.to raise_error ActiveRecord::DeleteRestrictionError
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
context
|
77
|
-
before { Role.create! name:
|
76
|
+
context 'when have children' do
|
77
|
+
before { Role.create! name: 'Child', parent: role }
|
78
78
|
|
79
|
-
it
|
79
|
+
it 'should raise an error when destroy' do
|
80
80
|
expect do
|
81
81
|
role.destroy
|
82
82
|
end.to raise_error ActiveRecord::DeleteRestrictionError
|
83
83
|
end
|
84
84
|
end
|
85
|
-
|
86
85
|
end
|
87
|
-
|
88
86
|
end
|
89
|
-
|
90
|
-
end
|
87
|
+
end
|
@@ -1,8 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Strongbolt::Tenantable do
|
4
|
-
|
5
|
-
it "should have been included in ActiveRecord::Base" do
|
4
|
+
it 'should have been included in ActiveRecord::Base' do
|
6
5
|
expect(ActiveRecord::Base.included_modules).to include Strongbolt::Tenantable
|
7
6
|
end
|
8
7
|
|
@@ -10,25 +9,25 @@ describe Strongbolt::Tenantable do
|
|
10
9
|
# When a class is set as tenant
|
11
10
|
#
|
12
11
|
describe 'tenant?' do
|
13
|
-
context
|
12
|
+
context 'when class is not a tenant' do
|
14
13
|
before do
|
15
|
-
define_model
|
14
|
+
define_model 'OtherModel'
|
16
15
|
end
|
17
16
|
|
18
|
-
it
|
17
|
+
it 'should return false' do
|
19
18
|
expect(OtherModel.tenant?).to eq false
|
20
19
|
end
|
21
20
|
end
|
22
21
|
|
23
|
-
context
|
22
|
+
context 'when class is a tenant' do
|
24
23
|
before do
|
25
|
-
define_model
|
24
|
+
define_model 'OtherModel' do
|
26
25
|
send :tenant
|
27
26
|
end
|
28
27
|
end
|
29
28
|
after { Strongbolt.send :tenants=, [] }
|
30
29
|
|
31
|
-
it
|
30
|
+
it 'should return true' do
|
32
31
|
expect(OtherModel.tenant?).to eq true
|
33
32
|
end
|
34
33
|
end
|
@@ -37,13 +36,11 @@ describe Strongbolt::Tenantable do
|
|
37
36
|
#
|
38
37
|
# Tenant setup
|
39
38
|
#
|
40
|
-
describe
|
41
|
-
|
39
|
+
describe 'tenant setup' do
|
42
40
|
#
|
43
41
|
# When every association is properly configured
|
44
42
|
#
|
45
|
-
context
|
46
|
-
|
43
|
+
context 'when no configuration error' do
|
47
44
|
#
|
48
45
|
# Set up a series of models to test the tenant setup
|
49
46
|
#
|
@@ -51,166 +48,181 @@ describe Strongbolt::Tenantable do
|
|
51
48
|
#
|
52
49
|
# Tenant Model
|
53
50
|
#
|
54
|
-
define_model
|
55
|
-
self.table_name =
|
56
|
-
has_many :child_models, class_name:
|
51
|
+
define_model 'TenantModel' do
|
52
|
+
self.table_name = 'models'
|
53
|
+
has_many :child_models, class_name: 'ChildModel'
|
57
54
|
|
58
|
-
belongs_to :parent, class_name:
|
59
|
-
|
55
|
+
belongs_to :parent, class_name: 'UnownedModel',
|
56
|
+
foreign_key: :parent_id
|
60
57
|
end
|
61
58
|
|
62
59
|
#
|
63
60
|
# Direct child to Tenant Model
|
64
61
|
#
|
65
|
-
define_model
|
66
|
-
self.table_name =
|
62
|
+
define_model 'ChildModel' do
|
63
|
+
self.table_name = 'child_models'
|
67
64
|
|
68
65
|
belongs_to :tenant_model, foreign_key: :model_id,
|
69
|
-
|
66
|
+
class_name: 'TenantModel'
|
70
67
|
|
71
|
-
has_many :other_child_models, class_name:
|
68
|
+
has_many :other_child_models, class_name: 'OtherChildModel'
|
69
|
+
has_one :very_polymorphic_sibling_model, as: :model, class_name: 'VeryPolymorphicSiblingModel'
|
72
70
|
end
|
73
71
|
|
74
72
|
#
|
75
73
|
# 2nd degree child of tenant model
|
76
74
|
#
|
77
|
-
define_model
|
78
|
-
self.table_name =
|
75
|
+
define_model 'OtherChildModel' do
|
76
|
+
self.table_name = 'child_models'
|
79
77
|
|
80
78
|
belongs_to :child_model, foreign_key: :model_id,
|
81
|
-
|
79
|
+
class_name: 'ChildModel'
|
82
80
|
belongs_to :uncle_model, foreign_key: :parent_id,
|
83
|
-
|
84
|
-
has_one :sibling_model, class_name:
|
85
|
-
has_one :polymorphic_sibling_model, class_name:
|
81
|
+
class_name: 'UncleModel'
|
82
|
+
has_one :sibling_model, class_name: 'SiblingModel'
|
83
|
+
has_one :polymorphic_sibling_model, class_name: 'PolymorphicSiblingModel'
|
86
84
|
|
87
85
|
has_and_belongs_to_many :bottom_models,
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
86
|
+
join_table: 'model_models',
|
87
|
+
class_name: 'BottomModel',
|
88
|
+
foreign_key: :parent_id,
|
89
|
+
association_foreign_key: :child_id
|
92
90
|
end
|
93
91
|
|
94
92
|
#
|
95
93
|
# Parent of second degree child
|
96
94
|
#
|
97
|
-
define_model
|
98
|
-
self.table_name =
|
95
|
+
define_model 'UncleModel' do
|
96
|
+
self.table_name = 'models'
|
99
97
|
|
100
|
-
has_many :other_child_models, class_name:
|
101
|
-
belongs_to :parent, class_name:
|
102
|
-
|
98
|
+
has_many :other_child_models, class_name: 'OtherChildModel'
|
99
|
+
belongs_to :parent, class_name: 'UnownedModel',
|
100
|
+
foreign_key: :parent_id
|
103
101
|
end
|
104
102
|
|
105
103
|
#
|
106
104
|
# Cousin of second degree child
|
107
105
|
#
|
108
|
-
define_model
|
109
|
-
self.table_name =
|
106
|
+
define_model 'SiblingModel' do
|
107
|
+
self.table_name = 'child_models'
|
110
108
|
|
111
109
|
belongs_to :other_child_model, foreign_key: :model_id
|
110
|
+
has_one :very_polymorphic_sibling_model, as: :other_model, class_name: 'VeryPolymorphicSiblingModel'
|
112
111
|
end
|
113
112
|
|
114
113
|
#
|
115
114
|
# Cousin of second degree child
|
116
115
|
#
|
117
|
-
define_model
|
118
|
-
self.table_name =
|
116
|
+
define_model 'PolymorphicSiblingModel' do
|
117
|
+
self.table_name = 'child_models'
|
119
118
|
|
120
119
|
belongs_to :model, polymorphic: true
|
121
120
|
has_one :unowned_model, foreign_key: :model_id
|
122
121
|
end
|
123
122
|
|
123
|
+
#
|
124
|
+
# Cousin of second degree child
|
125
|
+
#
|
126
|
+
define_model 'VeryPolymorphicSiblingModel' do
|
127
|
+
self.table_name = 'other_child_models'
|
128
|
+
|
129
|
+
belongs_to :model, polymorphic: true
|
130
|
+
belongs_to :other_model, polymorphic: true
|
131
|
+
end
|
132
|
+
|
124
133
|
#
|
125
134
|
# Top level model, parent of Tenant Model
|
126
135
|
#
|
127
|
-
define_model
|
136
|
+
define_model 'UnownedModel' do
|
128
137
|
has_many :tenant_models, foreign_key: :parent_id,
|
129
|
-
|
138
|
+
class_name: 'TenantModel'
|
130
139
|
# has_many :uncle_models, foreign_key: :parent_id
|
131
140
|
end
|
132
141
|
|
133
142
|
#
|
134
143
|
# Bottom level model, has and belons to many 2nd degree child
|
135
144
|
#
|
136
|
-
define_model
|
137
|
-
self.table_name =
|
145
|
+
define_model 'BottomModel' do
|
146
|
+
self.table_name = 'models'
|
138
147
|
|
139
148
|
has_and_belongs_to_many :other_child_models,
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
149
|
+
class_name: 'OtherChildModel',
|
150
|
+
join_table: 'model_models',
|
151
|
+
foreign_key: :child_id,
|
152
|
+
association_foreign_key: :parent_id
|
144
153
|
end
|
145
154
|
|
146
155
|
TenantModel.send :tenant
|
147
156
|
end
|
148
157
|
|
149
|
-
it
|
158
|
+
it 'should have added has_one :tenant_model to other child model' do
|
150
159
|
expect(OtherChildModel.new).to have_one(:tenant_model).through(:child_model)
|
151
160
|
end
|
152
161
|
|
153
|
-
it
|
162
|
+
it 'should have added has_many :tenant_models to BottomModel' do
|
154
163
|
expect(BottomModel.new).to have_many(:tenant_models).through :other_child_models
|
155
164
|
end
|
156
165
|
|
157
|
-
it
|
166
|
+
it 'should have created a has_one :tenant_model to SiblingModel' do
|
158
167
|
expect(SiblingModel.new).to have_one(:tenant_model).through :other_child_model
|
159
168
|
end
|
160
169
|
|
161
|
-
it
|
170
|
+
it 'should not have set a :tenant_model on polymorphic association' do
|
162
171
|
expect(PolymorphicSiblingModel.new).not_to have_one(:tenant_model)
|
163
172
|
end
|
164
173
|
|
165
|
-
it
|
174
|
+
it 'should have added has_many :tenant_models to UncleModel' do
|
166
175
|
expect(UncleModel.new).not_to have_many(:tenant_models).through :other_child_models
|
167
176
|
end
|
168
177
|
|
169
|
-
%w
|
178
|
+
%w[ChildModel OtherChildModel BottomModel SiblingModel].each do |model|
|
170
179
|
it "should have added a scope with_tenants to #{model}" do
|
171
180
|
expect(model.constantize).to respond_to :with_tenant_models
|
172
181
|
end
|
173
182
|
end
|
174
183
|
|
175
|
-
%w
|
184
|
+
%w[ChildModel OtherChildModel BottomModel SiblingModel].each do |model|
|
176
185
|
it "should have added a scope where_tenants to #{model}" do
|
177
186
|
expect(model.constantize).to respond_to :where_tenant_models_among
|
178
187
|
end
|
179
188
|
end
|
180
189
|
|
181
|
-
it
|
190
|
+
it 'creates a has_many users_tenant_models' do
|
182
191
|
expect(Strongbolt::Configuration.user_class.constantize.new).to have_many(:users_tenant_models)
|
183
192
|
.dependent(:delete_all)
|
184
193
|
end
|
185
194
|
|
186
|
-
it
|
195
|
+
it 'creates a has_many relationship on the User defined' do
|
187
196
|
expect(Strongbolt::Configuration.user_class.constantize.new).to have_many(:tenant_models).through :users_tenant_models
|
188
197
|
end
|
189
198
|
|
190
|
-
it
|
199
|
+
it 'creates a relationship on the user to get accessible tenants' do
|
191
200
|
expect(Strongbolt::Configuration.user_class.constantize.new).to respond_to :accessible_tenant_models
|
192
201
|
end
|
193
202
|
|
194
|
-
it
|
203
|
+
it 'should have added models to Capability::Models' do
|
195
204
|
expect(Strongbolt::Capability.models).to be_present
|
196
205
|
expect(Strongbolt::Capability.models.size).to be > 0
|
197
206
|
end
|
198
207
|
|
208
|
+
it 'should not raise an error' do
|
209
|
+
expect do
|
210
|
+
TenantModel.send :tenant
|
211
|
+
end.not_to raise_error
|
212
|
+
end
|
199
213
|
end
|
200
214
|
|
201
|
-
|
202
215
|
#
|
203
216
|
# When an association lacks an inverse (none configured and none found)
|
204
217
|
#
|
205
218
|
|
206
|
-
context
|
207
|
-
|
219
|
+
context 'when an association lacks an inverse' do
|
208
220
|
before(:all) do
|
209
221
|
#
|
210
222
|
# Tenant Model
|
211
223
|
#
|
212
|
-
define_model
|
213
|
-
self.table_name =
|
224
|
+
define_model 'TenantModel' do
|
225
|
+
self.table_name = 'models'
|
214
226
|
|
215
227
|
has_many :child_models
|
216
228
|
end
|
@@ -218,39 +230,37 @@ describe Strongbolt::Tenantable do
|
|
218
230
|
#
|
219
231
|
# Direct child to Tenant Model
|
220
232
|
#
|
221
|
-
define_model
|
222
|
-
self.table_name =
|
233
|
+
define_model 'ChildModel' do
|
234
|
+
self.table_name = 'child_models'
|
223
235
|
|
224
236
|
belongs_to :tenant_model, foreign_key: :model_id
|
225
237
|
|
226
238
|
has_many :other_child_models
|
227
239
|
end
|
228
240
|
|
229
|
-
define_model
|
230
|
-
self.table_name =
|
241
|
+
define_model 'OtherChildModel' do
|
242
|
+
self.table_name = 'child_models'
|
231
243
|
end
|
232
244
|
end
|
233
245
|
|
234
|
-
it
|
246
|
+
it 'should raise an error' do
|
235
247
|
expect do
|
236
248
|
TenantModel.send :tenant
|
237
249
|
end.to raise_error Strongbolt::InverseAssociationNotConfigured
|
238
250
|
end
|
239
|
-
|
240
251
|
end
|
241
252
|
|
242
253
|
#
|
243
254
|
# When a direct association lacks a reference to the tenant
|
244
255
|
#
|
245
256
|
|
246
|
-
context
|
247
|
-
|
257
|
+
context 'when an association lacks an inverse' do
|
248
258
|
before(:all) do
|
249
259
|
#
|
250
260
|
# Tenant Model
|
251
261
|
#
|
252
|
-
define_model
|
253
|
-
self.table_name =
|
262
|
+
define_model 'TenantModel' do
|
263
|
+
self.table_name = 'models'
|
254
264
|
|
255
265
|
has_many :child_models
|
256
266
|
end
|
@@ -258,24 +268,16 @@ describe Strongbolt::Tenantable do
|
|
258
268
|
#
|
259
269
|
# Direct child to Tenant Model
|
260
270
|
#
|
261
|
-
define_model
|
262
|
-
self.table_name =
|
271
|
+
define_model 'ChildModel' do
|
272
|
+
self.table_name = 'child_models'
|
263
273
|
end
|
264
274
|
end
|
265
275
|
|
266
|
-
it
|
276
|
+
it 'should raise an error' do
|
267
277
|
expect do
|
268
278
|
TenantModel.send :tenant
|
269
279
|
end.to raise_error Strongbolt::DirectAssociationNotConfigured
|
270
280
|
end
|
271
|
-
|
272
281
|
end
|
273
|
-
|
274
|
-
|
275
|
-
|
276
282
|
end
|
277
|
-
|
278
283
|
end
|
279
|
-
|
280
|
-
|
281
|
-
|