Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cancan
Manage
Activity
Members
Plan
External wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Github Mirror
coopdevs
cancan
Commits
f1ba76b6
Commit
f1ba76b6
authored
14 years ago
by
Ryan Bates
Browse files
Options
Downloads
Patches
Plain Diff
supporting arrays, ranges, and nested hashes in ability conditions
parent
283f58ee
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.rdoc
+2
-0
2 additions, 0 deletions
CHANGELOG.rdoc
lib/cancan/ability.rb
+14
-3
14 additions, 3 deletions
lib/cancan/ability.rb
spec/cancan/ability_spec.rb
+20
-0
20 additions, 0 deletions
spec/cancan/ability_spec.rb
with
36 additions
and
3 deletions
CHANGELOG.rdoc
+
2
−
0
View file @
f1ba76b6
1.1.0 (not released)
* Supporting arrays, ranges, and nested hashes in ability conditions
* Removing "unauthorized!" method in favor of "authorize!" in controllers
* Adding action, subject and default_message abilities to AccessDenied exception - see issue #40
...
...
This diff is collapsed.
Click to expand it.
lib/cancan/ability.rb
+
14
−
3
View file @
f1ba76b6
...
...
@@ -244,15 +244,26 @@ module CanCan
if
subject
.
class
==
Class
true
else
defined_conditions
.
all?
do
|
name
,
value
|
subject
.
send
(
name
)
==
value
end
matches_conditions?
subject
,
defined_conditions
end
else
true
end
end
def
matches_conditions?
(
subject
,
defined_conditions
)
defined_conditions
.
all?
do
|
name
,
value
|
attribute
=
subject
.
send
(
name
)
if
value
.
kind_of?
(
Hash
)
matches_conditions?
attribute
,
value
elsif
value
.
kind_of?
(
Array
)
||
value
.
kind_of?
(
Range
)
value
.
include?
attribute
else
attribute
==
value
end
end
end
def
includes_action?
(
actions
,
action
)
actions
.
include?
(
:manage
)
||
actions
.
include?
(
action
)
end
...
...
This diff is collapsed.
Click to expand it.
spec/cancan/ability_spec.rb
+
20
−
0
View file @
f1ba76b6
...
...
@@ -148,6 +148,26 @@ describe CanCan::Ability do
@ability
.
can?
(
:read
,
Array
).
should
be_true
end
it
"should allow an array of options in conditions hash"
do
@ability
.
can
:read
,
Array
,
:first
=>
[
1
,
3
,
5
]
@ability
.
can?
(
:read
,
[
1
,
2
,
3
]).
should
be_true
@ability
.
can?
(
:read
,
[
2
,
3
]).
should
be_false
@ability
.
can?
(
:read
,
[
3
,
4
]).
should
be_true
end
it
"should allow a range of options in conditions hash"
do
@ability
.
can
:read
,
Array
,
:first
=>
1
..
3
@ability
.
can?
(
:read
,
[
1
,
2
,
3
]).
should
be_true
@ability
.
can?
(
:read
,
[
3
,
4
]).
should
be_true
@ability
.
can?
(
:read
,
[
4
,
5
]).
should
be_false
end
it
"should allow nested hashes in conditions hash"
do
@ability
.
can
:read
,
Array
,
:first
=>
{
:length
=>
5
}
@ability
.
can?
(
:read
,
[
"foo"
,
"bar"
]).
should
be_false
@ability
.
can?
(
:read
,
[
"test1"
,
"foo"
]).
should
be_true
end
it
"should return conditions for a given ability"
do
@ability
.
can
:read
,
Array
,
:first
=>
1
,
:last
=>
3
@ability
.
conditions
(
:show
,
Array
).
should
==
{
:first
=>
1
,
:last
=>
3
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment