monarchy 1.1.5 → 2.0.0.pre.beta.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 +4 -4
- data/.codeclimate.yml +26 -0
- data/.gitignore +6 -6
- data/.rubocop.yml +3 -3
- data/CHANGELOG.md +11 -0
- data/Gemfile +4 -3
- data/Gemfile.lock +54 -53
- data/README.md +1 -0
- data/app/models/monarchy/hierarchy.rb +0 -1
- data/docs/acts_as_hierarchy.md +45 -0
- data/docs/acts_as_resource.md +103 -0
- data/docs/acts_as_user.md +52 -0
- data/docs/configuration.md +54 -0
- data/docs/installation.md +30 -0
- data/docs/usage.md +22 -0
- data/dummy/.rspec +2 -0
- data/dummy/README.rdoc +28 -0
- data/dummy/Rakefile +7 -0
- data/dummy/app/assets/images/.keep +0 -0
- data/dummy/app/assets/javascripts/application.js +13 -0
- data/dummy/app/assets/stylesheets/application.css +15 -0
- data/dummy/app/controllers/application_controller.rb +6 -0
- data/dummy/app/controllers/concerns/.keep +0 -0
- data/dummy/app/helpers/application_helper.rb +3 -0
- data/dummy/app/mailers/.keep +0 -0
- data/dummy/app/models/.keep +0 -0
- data/dummy/app/models/concerns/.keep +0 -0
- data/dummy/app/models/member.rb +4 -0
- data/dummy/app/models/memo.rb +5 -0
- data/dummy/app/models/project.rb +12 -0
- data/dummy/app/models/resource.rb +4 -0
- data/dummy/app/models/role.rb +4 -0
- data/dummy/app/models/status.rb +5 -0
- data/dummy/app/models/task.rb +5 -0
- data/dummy/app/models/user.rb +4 -0
- data/dummy/app/views/layouts/application.html.erb +14 -0
- data/dummy/bin/bundle +4 -0
- data/dummy/bin/rails +5 -0
- data/dummy/bin/rake +5 -0
- data/dummy/bin/setup +30 -0
- data/dummy/config/application.rb +25 -0
- data/dummy/config/boot.rb +6 -0
- data/dummy/config/database.yml +10 -0
- data/dummy/config/environment.rb +6 -0
- data/dummy/config/environments/development.rb +42 -0
- data/dummy/config/environments/production.rb +80 -0
- data/dummy/config/environments/test.rb +43 -0
- data/dummy/config/initializers/assets.rb +12 -0
- data/dummy/config/initializers/backtrace_silencers.rb +8 -0
- data/dummy/config/initializers/cookies_serializer.rb +4 -0
- data/dummy/config/initializers/filter_parameter_logging.rb +5 -0
- data/dummy/config/initializers/inflections.rb +17 -0
- data/dummy/config/initializers/mime_types.rb +5 -0
- data/dummy/config/initializers/monarchy.rb +46 -0
- data/dummy/config/initializers/session_store.rb +4 -0
- data/dummy/config/initializers/wrap_parameters.rb +15 -0
- data/dummy/config/locales/en.yml +23 -0
- data/dummy/config/routes.rb +57 -0
- data/dummy/config/secrets.yml +22 -0
- data/dummy/config.ru +5 -0
- data/dummy/db/migrate/20160220215254_monarchy_create_users.rb +7 -0
- data/dummy/db/migrate/20160221132205_monarchy_create_hierarchies.rb +22 -0
- data/dummy/db/migrate/20160221133215_monarchy_create_memberships.rb +38 -0
- data/dummy/db/migrate/20160221142305_create_memo_resource.rb +8 -0
- data/dummy/db/migrate/20160221142309_create_project_resource.rb +8 -0
- data/dummy/db/migrate/20160221154113_create_resource_resource.rb +8 -0
- data/dummy/db/migrate/20160306124820_add_memo_to_resource.rb +6 -0
- data/dummy/db/migrate/20160502212213_parent_as_setup.rb +7 -0
- data/dummy/db/migrate/20160815163909_task_model.rb +10 -0
- data/dummy/db/migrate/20160818205534_status.rb +9 -0
- data/dummy/db/schema.rb +111 -0
- data/dummy/lib/assets/.keep +0 -0
- data/dummy/public/404.html +67 -0
- data/dummy/public/422.html +67 -0
- data/dummy/public/500.html +66 -0
- data/dummy/public/favicon.ico +0 -0
- data/dummy/spec +1 -0
- data/lib/generators/templates/config.rb +44 -7
- data/lib/monarchy/acts_as_hierarchy.rb +12 -11
- data/lib/monarchy/acts_as_member.rb +8 -17
- data/lib/monarchy/acts_as_resource.rb +35 -22
- data/lib/monarchy/acts_as_user.rb +51 -39
- data/lib/monarchy/exceptions.rb +135 -0
- data/lib/monarchy/tasks.rb +1 -1
- data/lib/monarchy/validators.rb +83 -0
- data/lib/monarchy/version.rb +1 -1
- data/lib/monarchy.rb +21 -8
- data/monarchy.gemspec +4 -4
- metadata +85 -13
@@ -13,10 +13,7 @@ module Monarchy
|
|
13
13
|
after_update :assign_parent
|
14
14
|
after_create :ensure_hierarchy, :assign_parent
|
15
15
|
|
16
|
-
|
17
|
-
has_many :users, through: :members, class_name: "::#{Monarchy.user_class}"
|
18
|
-
has_one :hierarchy, as: :resource, dependent: :destroy, class_name: 'Monarchy::Hierarchy'
|
19
|
-
|
16
|
+
include_relationships
|
20
17
|
include_scopes
|
21
18
|
|
22
19
|
include Monarchy::ActsAsResource::InstanceMethods
|
@@ -24,7 +21,16 @@ module Monarchy
|
|
24
21
|
end
|
25
22
|
|
26
23
|
module SupportMethods
|
27
|
-
attr_accessor :
|
24
|
+
attr_accessor :automatic_hierarchy
|
25
|
+
attr_reader :acting_as_resource, :parentize_name
|
26
|
+
|
27
|
+
def default_role_name
|
28
|
+
Monarchy.configuration.inherited_default_role
|
29
|
+
end
|
30
|
+
|
31
|
+
def default_role
|
32
|
+
@default_role ||= Monarchy.role_class.find_by(name: default_role_name)
|
33
|
+
end
|
28
34
|
|
29
35
|
private
|
30
36
|
|
@@ -35,18 +41,26 @@ module Monarchy
|
|
35
41
|
end
|
36
42
|
|
37
43
|
def parent_as(name)
|
38
|
-
|
44
|
+
@parentize_name = name
|
39
45
|
end
|
40
46
|
|
41
47
|
def include_scopes
|
42
|
-
scope :in, (lambda do |resource|
|
43
|
-
|
48
|
+
scope :in, (lambda do |resource, descendants = true|
|
49
|
+
Monarchy::Validators.resource(resource)
|
50
|
+
hierarchies = Monarchy.hierarchy_class.in(resource.hierarchy, descendants)
|
51
|
+
joins(:hierarchy).where(monarchy_hierarchies: { id: hierarchies })
|
44
52
|
end)
|
45
53
|
|
46
54
|
scope :accessible_for, (lambda do |user|
|
47
|
-
joins(:hierarchy).where(monarchy_hierarchies: { id: Monarchy
|
55
|
+
joins(:hierarchy).where(monarchy_hierarchies: { id: Monarchy.hierarchy_class.accessible_for(user) })
|
48
56
|
end)
|
49
57
|
end
|
58
|
+
|
59
|
+
def include_relationships
|
60
|
+
has_many :members, through: :hierarchy, class_name: "::#{Monarchy.member_class}"
|
61
|
+
has_many :users, through: :members, class_name: "::#{Monarchy.user_class}"
|
62
|
+
has_one :hierarchy, as: :resource, dependent: :destroy, class_name: "::#{Monarchy.hierarchy_class}"
|
63
|
+
end
|
50
64
|
end
|
51
65
|
|
52
66
|
module InstanceMethods
|
@@ -55,11 +69,9 @@ module Monarchy
|
|
55
69
|
end
|
56
70
|
|
57
71
|
def parent=(resource)
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
@parent = resource
|
62
|
-
end
|
72
|
+
Monarchy::Validators.resource(resource, true)
|
73
|
+
hierarchy&.update(parent: resource.try(:ensure_hierarchy))
|
74
|
+
@parent = resource
|
63
75
|
end
|
64
76
|
|
65
77
|
def children
|
@@ -67,12 +79,12 @@ module Monarchy
|
|
67
79
|
end
|
68
80
|
|
69
81
|
def children=(array)
|
70
|
-
hierarchy
|
82
|
+
hierarchy&.update(children: hierarchies_for(array))
|
71
83
|
@children = array
|
72
84
|
end
|
73
85
|
|
74
86
|
def ensure_hierarchy(force = false)
|
75
|
-
self.hierarchy ||= Monarchy
|
87
|
+
self.hierarchy ||= Monarchy.hierarchy_class.create(
|
76
88
|
resource: self,
|
77
89
|
parent: parent.try(:hierarchy),
|
78
90
|
children: hierarchies_for(children)
|
@@ -82,12 +94,12 @@ module Monarchy
|
|
82
94
|
private
|
83
95
|
|
84
96
|
def assign_parent(force = false)
|
85
|
-
parentize = self.class.
|
97
|
+
parentize = self.class.parentize_name
|
98
|
+
return unless parentize
|
86
99
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
end
|
100
|
+
was_changed = changes["#{parentize}_id"] || changes["#{parentize}_type"]
|
101
|
+
Monarchy::Validators.resource(send(parentize), true, false)
|
102
|
+
self.parent = send(parentize) if was_changed || force
|
91
103
|
end
|
92
104
|
|
93
105
|
def children_resources
|
@@ -97,7 +109,8 @@ module Monarchy
|
|
97
109
|
end
|
98
110
|
|
99
111
|
def hierarchies_for(array)
|
100
|
-
|
112
|
+
array&.compact!
|
113
|
+
Array(array).map { |resource| Monarchy::Validators.resource(resource).hierarchy }
|
101
114
|
end
|
102
115
|
end
|
103
116
|
end
|
@@ -6,10 +6,10 @@ module Monarchy
|
|
6
6
|
module ClassMethods
|
7
7
|
def acts_as_user
|
8
8
|
has_many :members, class_name: "::#{Monarchy.member_class}", dependent: :destroy
|
9
|
-
has_many :hierarchies, through: :members, class_name:
|
9
|
+
has_many :hierarchies, through: :members, class_name: "::#{Monarchy.hierarchy_class}"
|
10
10
|
|
11
11
|
scope :accessible_for, (lambda do |user|
|
12
|
-
where(id: Monarchy
|
12
|
+
where(id: Monarchy.hierarchy_class.accessible_for(user)
|
13
13
|
.joins(members: [:user]).select(:user_id)).union(where(id: user.id))
|
14
14
|
end)
|
15
15
|
|
@@ -18,24 +18,26 @@ module Monarchy
|
|
18
18
|
end
|
19
19
|
|
20
20
|
module InstanceMethods
|
21
|
-
def roles_for(resource,
|
22
|
-
|
23
|
-
accessible_roles_for(resource,
|
21
|
+
def roles_for(resource, inheritance = true)
|
22
|
+
Monarchy::Validators.resource(resource, false, false)
|
23
|
+
accessible_roles_for(resource, inheritance)
|
24
24
|
end
|
25
25
|
|
26
26
|
def member_for(resource)
|
27
|
+
Monarchy::Validators.resource(resource)
|
27
28
|
resource.hierarchy.members.where(monarchy_members: { user_id: id }).first
|
28
29
|
end
|
29
30
|
|
30
|
-
def grant(
|
31
|
+
def grant(*role_names, resource)
|
31
32
|
ActiveRecord::Base.transaction do
|
32
|
-
grant_or_create_member(
|
33
|
+
grant_or_create_member(role_names, resource)
|
33
34
|
end
|
34
35
|
end
|
35
36
|
|
36
|
-
def revoke_access(resource,
|
37
|
-
|
38
|
-
|
37
|
+
def revoke_access(resource, hierarchies = nil)
|
38
|
+
Monarchy::Validators.resource(resource)
|
39
|
+
hierarchies ||= resource.hierarchy.self_and_descendants
|
40
|
+
members_for(hierarchies).delete_all
|
39
41
|
end
|
40
42
|
|
41
43
|
def revoke_role(role_name, resource)
|
@@ -43,14 +45,15 @@ module Monarchy
|
|
43
45
|
end
|
44
46
|
|
45
47
|
def revoke_role!(role_name, resource)
|
46
|
-
revoking_role(role_name, resource,
|
48
|
+
revoking_role(role_name, resource, Monarchy.configuration.revoke_strategy)
|
47
49
|
end
|
48
50
|
|
49
51
|
private
|
50
52
|
|
51
53
|
def accessible_roles_for(resource, inheritnce)
|
54
|
+
return Monarchy.role_class.none unless resource.hierarchy
|
52
55
|
accessible_roles = if inheritnce
|
53
|
-
|
56
|
+
resource_and_inheritance_roles(resource)
|
54
57
|
else
|
55
58
|
resource_roles(resource).order('level desc')
|
56
59
|
end
|
@@ -58,7 +61,7 @@ module Monarchy
|
|
58
61
|
accessible_roles.present? ? accessible_roles : descendant_role(resource)
|
59
62
|
end
|
60
63
|
|
61
|
-
def
|
64
|
+
def resource_and_inheritance_roles(resource)
|
62
65
|
hierarchy_ids = resource.hierarchy.ancestors.select(:id)
|
63
66
|
Monarchy.role_class.where(id:
|
64
67
|
Monarchy.role_class.joins(:members).where('monarchy_members.user_id': id)
|
@@ -71,52 +74,61 @@ module Monarchy
|
|
71
74
|
def resource_roles(resource)
|
72
75
|
Monarchy.role_class.joins(:members)
|
73
76
|
.where('monarchy_members.hierarchy_id': resource.hierarchy.id, 'monarchy_members.user_id': id)
|
74
|
-
.distinct
|
75
77
|
end
|
76
78
|
|
77
79
|
def descendant_role(resource)
|
78
|
-
|
79
|
-
children_access = members_for(
|
80
|
-
children_access ? Monarchy.role_class.where(id:
|
80
|
+
descendants = resource.hierarchy.descendants
|
81
|
+
children_access = members_for(descendants).present?
|
82
|
+
children_access ? Monarchy.role_class.where(id: inherited_default_role) : Monarchy.role_class.none
|
81
83
|
end
|
82
84
|
|
83
|
-
def revoking_role(role_name, resource,
|
84
|
-
|
85
|
+
def revoking_role(role_name, resource, strategy = nil)
|
86
|
+
Monarchy::Validators.resource(resource)
|
87
|
+
role = Monarchy::Validators.role_name(role_name)
|
88
|
+
|
89
|
+
member = member_for(resource)
|
90
|
+
member_roles = member.try(:members_roles)
|
85
91
|
return 0 if member_roles.nil?
|
86
92
|
|
87
|
-
|
88
|
-
member_roles.
|
93
|
+
revoking_last_role(role, resource, strategy) if Monarchy::Validators.last_role?(member, role)
|
94
|
+
member_roles.where(role: role).delete_all
|
89
95
|
end
|
90
96
|
|
91
|
-
def
|
92
|
-
|
93
|
-
|
97
|
+
def revoking_last_role(role, resource, strategy)
|
98
|
+
case strategy
|
99
|
+
when :revoke_access
|
100
|
+
return revoke_access(resource)
|
101
|
+
when :revoke_member
|
102
|
+
return member_for(resource).delete
|
103
|
+
else
|
104
|
+
default_role = Monarchy::Validators.default_role?(resource, role)
|
105
|
+
raise Monarchy::Exceptions::RoleNotRevokable if default_role
|
106
|
+
|
107
|
+
grant(resource.class.default_role_name, resource)
|
108
|
+
end
|
109
|
+
end
|
94
110
|
|
111
|
+
def grant_or_create_member(role_names, resource)
|
112
|
+
Monarchy::Validators.resource(resource)
|
113
|
+
roles = Monarchy::Validators.role_names(role_names)
|
95
114
|
member = member_for(resource)
|
115
|
+
|
96
116
|
if member
|
97
|
-
|
117
|
+
member_roles = roles.map { |role| { member: member, role: role } }
|
118
|
+
Monarchy::MembersRole.create(member_roles)
|
98
119
|
else
|
99
|
-
member = Monarchy.member_class.create(user: self, hierarchy: resource.hierarchy, roles:
|
120
|
+
member = Monarchy.member_class.create(user: self, hierarchy: resource.hierarchy, roles: roles)
|
100
121
|
end
|
101
122
|
|
102
123
|
member
|
103
124
|
end
|
104
125
|
|
105
|
-
def members_for(
|
106
|
-
Monarchy.member_class.where(
|
107
|
-
end
|
108
|
-
|
109
|
-
def default_role
|
110
|
-
@default_role ||= Monarchy.role_class.find_by(name: Monarchy.configuration.default_role.name)
|
111
|
-
end
|
112
|
-
|
113
|
-
def last_role?(member_roles, role_name = nil)
|
114
|
-
role_name ||= default_role.name
|
115
|
-
member_roles.count == 1 && member_roles.first.role.name == role_name.to_s
|
126
|
+
def members_for(hierarchies)
|
127
|
+
Monarchy.member_class.where(hierarchy: hierarchies, user_id: id)
|
116
128
|
end
|
117
129
|
|
118
|
-
def
|
119
|
-
|
130
|
+
def inherited_default_role
|
131
|
+
@inherited_default_role ||= Monarchy.role_class.find_by(name: Monarchy.configuration.inherited_default_role)
|
120
132
|
end
|
121
133
|
end
|
122
134
|
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Monarchy
|
3
|
+
module Exceptions
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
class ClassNotDefined < Error
|
7
|
+
def initialize(class_name)
|
8
|
+
@class_name = class_name
|
9
|
+
end
|
10
|
+
|
11
|
+
def message
|
12
|
+
"#{@class_name} class has to be initialized or exist!"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ConfigNotDefined < Error
|
17
|
+
def initialize(property)
|
18
|
+
@property = property
|
19
|
+
end
|
20
|
+
|
21
|
+
def message
|
22
|
+
"Monarchy requires a #{@property} to be configured!"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class RoleNotExist < Error
|
27
|
+
def initialize(role_name)
|
28
|
+
@role_name = role_name
|
29
|
+
end
|
30
|
+
|
31
|
+
def message
|
32
|
+
"Role '#{@role_name}' does not exist"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class ModelNotResource < Error
|
37
|
+
def initialize(resource)
|
38
|
+
@resource = resource
|
39
|
+
end
|
40
|
+
|
41
|
+
def message
|
42
|
+
"Model '#{@resource.class}' is not acting as resource!"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class ModelNotHierarchy < Error
|
47
|
+
def initialize(resource)
|
48
|
+
@resource = resource
|
49
|
+
end
|
50
|
+
|
51
|
+
def message
|
52
|
+
"Model '#{@resource.class}' is not a hierarchy!"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class ModelNotMember < Error
|
57
|
+
def initialize(member)
|
58
|
+
@member = member
|
59
|
+
end
|
60
|
+
|
61
|
+
def message
|
62
|
+
"Model '#{@member.class}' is not acting as member!"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class ModelNotRole < Error
|
67
|
+
def initialize(role)
|
68
|
+
@role = role
|
69
|
+
end
|
70
|
+
|
71
|
+
def message
|
72
|
+
"Model '#{@role.class}' is not acting as role!"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class ModelNotUser < Error
|
77
|
+
def initialize(user)
|
78
|
+
@user = user
|
79
|
+
end
|
80
|
+
|
81
|
+
def message
|
82
|
+
"Model '#{@user.class}' is not acting as user!"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class RoleNotRevokable < Error
|
87
|
+
def message
|
88
|
+
"Can not revoke default role when no more roles exists! \
|
89
|
+
(Use 'revoke_role!' if you know what you are doing )"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class ResourceIsNil < Error
|
94
|
+
def message
|
95
|
+
'Resource can NOT be nil!'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class HierarchyIsNil < Error
|
100
|
+
def message
|
101
|
+
'Hierarchy can NOT be nil!'
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
class UserIsNil < Error
|
106
|
+
def message
|
107
|
+
'User can NOT be nil!'
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
class MemberIsNil < Error
|
112
|
+
def message
|
113
|
+
'Member can NOT be nil!'
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class RoleIsNil < Error
|
118
|
+
def message
|
119
|
+
'Role can NOT be nil!'
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class ResourceNotPersist < Error
|
124
|
+
def message
|
125
|
+
'Resource has to persisted!'
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
class HierarchyNotPersist < Error
|
130
|
+
def message
|
131
|
+
'Resource has to persisted!'
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
data/lib/monarchy/tasks.rb
CHANGED
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Monarchy
|
3
|
+
module Validators
|
4
|
+
class << self
|
5
|
+
def last_role?(member, role)
|
6
|
+
member(member)
|
7
|
+
role(role)
|
8
|
+
(member.roles - [role]).empty?
|
9
|
+
end
|
10
|
+
|
11
|
+
def default_role?(resource, role)
|
12
|
+
resource(resource)
|
13
|
+
role(role)
|
14
|
+
|
15
|
+
role == resource.class.default_role
|
16
|
+
end
|
17
|
+
|
18
|
+
def role_name(role_name)
|
19
|
+
role = Monarchy.role_class.find_by(name: role_name)
|
20
|
+
role || raise(Monarchy::Exceptions::RoleNotExist, role_name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def role_names(*role_names)
|
24
|
+
role_names.flatten!
|
25
|
+
roles = Monarchy.role_class.where(name: role_names)
|
26
|
+
wrong_names = role_names.map(&:to_s) - roles.map(&:name)
|
27
|
+
wrong_names.each { |name| raise(Monarchy::Exceptions::RoleNotExist, name) }
|
28
|
+
roles
|
29
|
+
end
|
30
|
+
|
31
|
+
def resource(resource, allow_nil = false, persistance = true)
|
32
|
+
raise Monarchy::Exceptions::ResourceIsNil if !resource && !allow_nil
|
33
|
+
|
34
|
+
model = check_model_class(resource, 'ModelNotResource') do
|
35
|
+
resource.class.try(:acting_as_resource)
|
36
|
+
end
|
37
|
+
|
38
|
+
persistance && resource && !resource.persisted? ? raise(Monarchy::Exceptions::ResourceNotPersist) : model
|
39
|
+
end
|
40
|
+
|
41
|
+
def hierarchy(hierarchy, allow_nil = false, persistance = true)
|
42
|
+
raise Monarchy::Exceptions::HierarchyIsNil if !hierarchy && !allow_nil
|
43
|
+
|
44
|
+
model = check_model_class(hierarchy, 'ModelNotHierarchy') do
|
45
|
+
hierarchy.is_a?(Monarchy.hierarchy_class)
|
46
|
+
end
|
47
|
+
|
48
|
+
persistance && hierarchy && !hierarchy.persisted? ? raise(Monarchy::Exceptions::HierarchyNotPersist) : model
|
49
|
+
end
|
50
|
+
|
51
|
+
def user(user, allow_nil = false)
|
52
|
+
raise Monarchy::Exceptions::UserIsNil if !user && !allow_nil
|
53
|
+
model_is_class(user, Monarchy.user_class, 'ModelNotUser')
|
54
|
+
end
|
55
|
+
|
56
|
+
def member(member, allow_nil = false)
|
57
|
+
raise Monarchy::Exceptions::MemberIsNil if !member && !allow_nil
|
58
|
+
model_is_class(member, Monarchy.member_class, 'ModelNotMember')
|
59
|
+
end
|
60
|
+
|
61
|
+
def role(role, allow_nil = false)
|
62
|
+
raise Monarchy::Exceptions::RoleIsNil if !role && !allow_nil
|
63
|
+
model_is_class(role, Monarchy.role_class, 'ModelNotRole')
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def check_model_class(model, exception_class)
|
69
|
+
if yield
|
70
|
+
model
|
71
|
+
elsif model
|
72
|
+
raise "Monarchy::Exceptions::#{exception_class}".constantize, model
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def model_is_class(model, klass, exception_class)
|
77
|
+
check_model_class(model, exception_class) do
|
78
|
+
model.is_a?(klass)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
data/lib/monarchy/version.rb
CHANGED
data/lib/monarchy.rb
CHANGED
@@ -3,12 +3,14 @@ require 'closure_tree'
|
|
3
3
|
require 'configurations'
|
4
4
|
require 'active_record_union'
|
5
5
|
|
6
|
+
require 'monarchy/exceptions'
|
7
|
+
require 'monarchy/validators'
|
6
8
|
require 'monarchy/tasks'
|
7
9
|
require 'monarchy/engine'
|
8
10
|
|
9
|
-
require 'monarchy/acts_as_user'
|
10
11
|
require 'monarchy/acts_as_role'
|
11
12
|
require 'monarchy/acts_as_member'
|
13
|
+
require 'monarchy/acts_as_user'
|
12
14
|
require 'monarchy/acts_as_resource'
|
13
15
|
require 'monarchy/acts_as_hierarchy'
|
14
16
|
|
@@ -24,23 +26,34 @@ module Monarchy
|
|
24
26
|
configuration_defaults do |config|
|
25
27
|
config.member_class_name = 'Monarchy::Member'
|
26
28
|
config.role_class_name = 'Monarchy::Role'
|
27
|
-
config.
|
29
|
+
config.hierarchy_class_name = 'Monarchy::Hierarchy'
|
30
|
+
config.members_access_revoke = false
|
31
|
+
config.revoke_strategy = :revoke_member
|
28
32
|
end
|
29
33
|
|
30
|
-
not_configured do |
|
31
|
-
raise
|
34
|
+
not_configured do |property|
|
35
|
+
raise Monarchy::Exceptions::ConfigNotDefined, property
|
32
36
|
end
|
33
37
|
|
34
38
|
def self.member_class
|
35
|
-
Monarchy.configuration.member_class_name.safe_constantize
|
39
|
+
Monarchy.configuration.member_class_name.safe_constantize || class_not_defined('Member')
|
36
40
|
end
|
37
41
|
|
38
42
|
def self.role_class
|
39
|
-
Monarchy.configuration.role_class_name.safe_constantize
|
43
|
+
Monarchy.configuration.role_class_name.safe_constantize || class_not_defined('Role')
|
40
44
|
end
|
41
45
|
|
42
46
|
def self.user_class
|
43
|
-
|
44
|
-
|
47
|
+
Monarchy.configuration.user_class_name.safe_constantize || class_not_defined('User')
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.hierarchy_class
|
51
|
+
Monarchy.configuration.hierarchy_class_name.safe_constantize || class_not_defined('Hierarchy')
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def class_not_defined(class_name)
|
57
|
+
raise Monarchy::Exceptions::ClassNotDefined, class_name
|
45
58
|
end
|
46
59
|
end
|
data/monarchy.gemspec
CHANGED
@@ -25,13 +25,13 @@ Gem::Specification.new do |s|
|
|
25
25
|
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
26
|
s.require_paths = ['lib']
|
27
27
|
|
28
|
-
s.add_runtime_dependency 'rails', '~> 4.2', '>= 4.2.
|
29
|
-
s.add_dependency 'closure_tree', '6.
|
30
|
-
s.add_dependency 'configurations', '2.2.
|
28
|
+
s.add_runtime_dependency 'rails', '~> 4.2', '>= 4.2.7.1'
|
29
|
+
s.add_dependency 'closure_tree', '6.2.0'
|
30
|
+
s.add_dependency 'configurations', '2.2.1'
|
31
31
|
s.add_dependency 'active_record_union', '1.2.0'
|
32
32
|
s.add_dependency 'tqdm', '0.3.0'
|
33
33
|
|
34
34
|
s.add_development_dependency 'bundler', '~> 1.12'
|
35
|
-
s.add_development_dependency 'rake', '~> 11.
|
35
|
+
s.add_development_dependency 'rake', '~> 11.3'
|
36
36
|
s.add_development_dependency 'rspec', '3.5.0'
|
37
37
|
end
|