cancan 1.3.4 → 1.4.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +19 -0
- data/README.rdoc +1 -1
- data/Rakefile +1 -1
- data/lib/cancan/ability.rb +80 -31
- data/lib/cancan/can_definition.rb +33 -11
- data/lib/cancan/controller_additions.rb +59 -6
- data/lib/cancan/controller_resource.rb +18 -6
- data/lib/cancan/exceptions.rb +5 -1
- data/spec/cancan/ability_spec.rb +128 -59
- data/spec/cancan/active_record_additions_spec.rb +1 -1
- data/spec/cancan/can_definition_spec.rb +1 -0
- data/spec/cancan/controller_additions_spec.rb +25 -26
- data/spec/cancan/controller_resource_spec.rb +92 -83
- data/spec/cancan/query_spec.rb +48 -48
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +12 -2
- metadata +5 -4
data/spec/cancan/query_spec.rb
CHANGED
@@ -7,101 +7,101 @@ describe CanCan::Query do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should have false conditions if no abilities match" do
|
10
|
-
@ability.query(:destroy,
|
10
|
+
@ability.query(:destroy, Project).conditions.should == "true=false"
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should return hash for single `can` definition" do
|
14
|
-
@ability.can :read,
|
15
|
-
@ability.query(:read,
|
14
|
+
@ability.can :read, Project, :blocked => false, :user_id => 1
|
15
|
+
@ability.query(:read, Project).conditions.should == { :blocked => false, :user_id => 1 }
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should merge multiple can definitions into single SQL string joining with OR" do
|
19
|
-
@ability.can :read,
|
20
|
-
@ability.can :read,
|
21
|
-
@ability.query(:read,
|
19
|
+
@ability.can :read, Project, :blocked => false
|
20
|
+
@ability.can :read, Project, :admin => true
|
21
|
+
@ability.query(:read, Project).conditions.should == "(admin=true) OR (blocked=false)"
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should merge multiple can definitions into single SQL string joining with OR and AND" do
|
25
|
-
@ability.can :read,
|
26
|
-
@ability.can :read,
|
27
|
-
@ability.query(:read,
|
25
|
+
@ability.can :read, Project, :blocked => false, :active => true
|
26
|
+
@ability.can :read, Project, :admin => true
|
27
|
+
@ability.query(:read, Project).conditions.should orderlessly_match("(blocked=false AND active=true) OR (admin=true)")
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should merge multiple can definitions into single SQL string joining with OR and AND" do
|
31
|
-
@ability.can :read,
|
32
|
-
@ability.can :read,
|
33
|
-
@ability.query(:read,
|
31
|
+
@ability.can :read, Project, :blocked => false, :active => true
|
32
|
+
@ability.can :read, Project, :admin => true
|
33
|
+
@ability.query(:read, Project).conditions.should orderlessly_match("(blocked=false AND active=true) OR (admin=true)")
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should return false conditions for cannot clause" do
|
37
|
-
@ability.cannot :read,
|
38
|
-
@ability.query(:read,
|
37
|
+
@ability.cannot :read, Project
|
38
|
+
@ability.query(:read, Project).conditions.should == "true=false"
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should return SQL for single `can` definition in front of default `cannot` condition" do
|
42
|
-
@ability.cannot :read,
|
43
|
-
@ability.can :read,
|
44
|
-
@ability.query(:read,
|
42
|
+
@ability.cannot :read, Project
|
43
|
+
@ability.can :read, Project, :blocked => false, :user_id => 1
|
44
|
+
@ability.query(:read, Project).conditions.should orderlessly_match("blocked=false AND user_id=1")
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should return true condition for single `can` definition in front of default `can` condition" do
|
48
|
-
@ability.can :read,
|
49
|
-
@ability.can :read,
|
50
|
-
@ability.query(:read,
|
48
|
+
@ability.can :read, Project
|
49
|
+
@ability.can :read, Project, :blocked => false, :user_id => 1
|
50
|
+
@ability.query(:read, Project).conditions.should == 'true=true'
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should return false condition for single `cannot` definition" do
|
54
|
-
@ability.cannot :read,
|
55
|
-
@ability.query(:read,
|
54
|
+
@ability.cannot :read, Project, :blocked => true, :user_id => 1
|
55
|
+
@ability.query(:read, Project).conditions.should == 'true=false'
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should return `false condition` for single `cannot` definition in front of default `cannot` condition" do
|
59
|
-
@ability.cannot :read,
|
60
|
-
@ability.cannot :read,
|
61
|
-
@ability.query(:read,
|
59
|
+
@ability.cannot :read, Project
|
60
|
+
@ability.cannot :read, Project, :blocked => true, :user_id => 1
|
61
|
+
@ability.query(:read, Project).conditions.should == 'true=false'
|
62
62
|
end
|
63
63
|
|
64
64
|
it "should return `not (sql)` for single `cannot` definition in front of default `can` condition" do
|
65
|
-
@ability.can :read,
|
66
|
-
@ability.cannot :read,
|
67
|
-
@ability.query(:read,
|
65
|
+
@ability.can :read, Project
|
66
|
+
@ability.cannot :read, Project, :blocked => true, :user_id => 1
|
67
|
+
@ability.query(:read, Project).conditions.should orderlessly_match("not (blocked=true AND user_id=1)")
|
68
68
|
end
|
69
69
|
|
70
70
|
it "should return appropriate sql conditions in complex case" do
|
71
|
-
@ability.can :read,
|
72
|
-
@ability.can :manage,
|
73
|
-
@ability.can :update,
|
74
|
-
@ability.cannot :update,
|
75
|
-
@ability.query(:update,
|
76
|
-
@ability.query(:manage,
|
77
|
-
@ability.query(:read,
|
71
|
+
@ability.can :read, Project
|
72
|
+
@ability.can :manage, Project, :id => 1
|
73
|
+
@ability.can :update, Project, :manager_id => 1
|
74
|
+
@ability.cannot :update, Project, :self_managed => true
|
75
|
+
@ability.query(:update, Project).conditions.should == 'not (self_managed=true) AND ((manager_id=1) OR (id=1))'
|
76
|
+
@ability.query(:manage, Project).conditions.should == {:id=>1}
|
77
|
+
@ability.query(:read, Project).conditions.should == 'true=true'
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should have nil joins if no can definitions" do
|
81
|
-
@ability.query(:read,
|
81
|
+
@ability.query(:read, Project).joins.should be_nil
|
82
82
|
end
|
83
83
|
|
84
84
|
it "should have nil joins if no nested hashes specified in conditions" do
|
85
|
-
@ability.can :read,
|
86
|
-
@ability.can :read,
|
87
|
-
@ability.query(:read,
|
85
|
+
@ability.can :read, Project, :blocked => false
|
86
|
+
@ability.can :read, Project, :admin => true
|
87
|
+
@ability.query(:read, Project).joins.should be_nil
|
88
88
|
end
|
89
89
|
|
90
90
|
it "should merge separate joins into a single array" do
|
91
|
-
@ability.can :read,
|
92
|
-
@ability.can :read,
|
93
|
-
@ability.query(:read,
|
91
|
+
@ability.can :read, Project, :project => { :blocked => false }
|
92
|
+
@ability.can :read, Project, :company => { :admin => true }
|
93
|
+
@ability.query(:read, Project).joins.inspect.should orderlessly_match([:company, :project].inspect)
|
94
94
|
end
|
95
95
|
|
96
96
|
it "should merge same joins into a single array" do
|
97
|
-
@ability.can :read,
|
98
|
-
@ability.can :read,
|
99
|
-
@ability.query(:read,
|
97
|
+
@ability.can :read, Project, :project => { :blocked => false }
|
98
|
+
@ability.can :read, Project, :project => { :admin => true }
|
99
|
+
@ability.query(:read, Project).joins.should == [:project]
|
100
100
|
end
|
101
101
|
|
102
102
|
it "should merge complex, nested joins" do
|
103
|
-
@ability.can :read,
|
104
|
-
@ability.can :read,
|
105
|
-
@ability.query(:read,
|
103
|
+
@ability.can :read, Project, :project => { :bar => {:test => true} }, :company => { :bar => {:test => true} }
|
104
|
+
@ability.can :read, Project, :project => { :foo => {:bar => true}, :bar => {:zip => :zap} }
|
105
|
+
@ability.query(:read, Project).joins.inspect.should orderlessly_match([{:project => [:bar, :foo]}, {:company => [:bar]}].inspect)
|
106
106
|
end
|
107
107
|
end
|
data/spec/spec.opts
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -19,8 +19,18 @@ class Ability
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
#
|
23
|
-
class
|
22
|
+
# Generic class to mimic a model
|
23
|
+
class Project
|
24
|
+
attr_accessor :name
|
25
|
+
|
26
|
+
def initialize(attributes = {})
|
27
|
+
@name = attributes[:name]
|
28
|
+
end
|
29
|
+
|
30
|
+
def attributes=(attributes)
|
31
|
+
@name = attributes[:name] if attributes[:name]
|
32
|
+
end
|
33
|
+
|
24
34
|
class << self
|
25
35
|
protected
|
26
36
|
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cancan
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
4
|
+
prerelease: true
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
- 3
|
8
7
|
- 4
|
9
|
-
|
8
|
+
- 0
|
9
|
+
- beta1
|
10
|
+
version: 1.4.0.beta1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Ryan Bates
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-09-03 00:00:00 -07:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|