Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
b287e354
Commit
b287e354
authored
Apr 16, 2020
by
Mark Chao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor class lookup
Avoid rechecking presence of declarative_policy_class.
parent
3455e77e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
9 deletions
+44
-9
lib/declarative_policy.rb
lib/declarative_policy.rb
+8
-9
spec/lib/declarative_policy_spec.rb
spec/lib/declarative_policy_spec.rb
+36
-0
No files found.
lib/declarative_policy.rb
View file @
b287e354
...
...
@@ -72,18 +72,17 @@ module DeclarativePolicy
end
def
compute_class_for_class
(
subject_class
)
if
subject_class
.
respond_to?
(
:declarative_policy_class
)
return
subject_class
.
declarative_policy_class
.
constantize
end
subject_class
.
ancestors
.
each
do
|
klass
|
next
unless
klass
.
name
name
=
klass
.
name
next
unless
name
begin
klass_name
=
if
subject_class
.
respond_to?
(
:declarative_policy_class
)
subject_class
.
declarative_policy_class
else
"
#{
klass
.
name
}
Policy"
end
policy_class
=
klass_name
.
constantize
policy_class
=
"
#{
name
}
Policy"
.
constantize
# NOTE: the < operator here tests whether policy_class
# inherits from Base. We can't use #is_a? because that
...
...
spec/lib/declarative_policy_spec.rb
0 → 100644
View file @
b287e354
# frozen_string_literal: true
require
'spec_helper'
describe
DeclarativePolicy
do
describe
'.class_for'
do
it
'uses declarative_policy_class if present'
do
instance
=
Gitlab
::
ErrorTracking
::
ErrorEvent
.
new
expect
(
described_class
.
class_for
(
instance
)).
to
eq
(
ErrorTracking
::
BasePolicy
)
end
it
'infers policy class from name'
do
instance
=
PersonalSnippet
.
new
expect
(
described_class
.
class_for
(
instance
)).
to
eq
(
PersonalSnippetPolicy
)
end
it
'raises error if not found'
do
instance
=
Object
.
new
expect
{
described_class
.
class_for
(
instance
)
}.
to
raise_error
(
'no policy for Object'
)
end
context
'when found policy class does not inherit base'
do
class
Foo
;
end
class
FooPolicy
;
end
it
'raises error if inferred class does not inherit Base'
do
instance
=
Foo
.
new
expect
{
described_class
.
class_for
(
instance
)
}.
to
raise_error
(
'no policy for Foo'
)
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment