cancan 1.4.1 → 1.5.0

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.
Files changed (36) hide show
  1. data/CHANGELOG.rdoc +21 -0
  2. data/Gemfile +17 -0
  3. data/LICENSE +1 -1
  4. data/README.rdoc +20 -90
  5. data/Rakefile +8 -0
  6. data/lib/cancan/ability.rb +24 -26
  7. data/lib/cancan/controller_additions.rb +50 -0
  8. data/lib/cancan/controller_resource.rb +33 -15
  9. data/lib/cancan/exceptions.rb +3 -0
  10. data/lib/cancan/model_adapters/abstract_adapter.rb +40 -0
  11. data/lib/cancan/model_adapters/active_record_adapter.rb +119 -0
  12. data/lib/cancan/model_adapters/data_mapper_adapter.rb +33 -0
  13. data/lib/cancan/model_adapters/default_adapter.rb +7 -0
  14. data/lib/cancan/model_adapters/mongoid_adapter.rb +41 -0
  15. data/lib/cancan/{active_record_additions.rb → model_additions.rb} +5 -16
  16. data/lib/cancan/{can_definition.rb → rule.rb} +29 -25
  17. data/lib/cancan.rb +8 -3
  18. data/lib/generators/cancan/ability/USAGE +4 -0
  19. data/lib/generators/cancan/ability/ability_generator.rb +11 -0
  20. data/lib/generators/cancan/ability/templates/ability.rb +28 -0
  21. data/spec/README.rdoc +28 -0
  22. data/spec/cancan/ability_spec.rb +11 -3
  23. data/spec/cancan/controller_additions_spec.rb +30 -0
  24. data/spec/cancan/controller_resource_spec.rb +68 -2
  25. data/spec/cancan/inherited_resource_spec.rb +3 -1
  26. data/spec/cancan/model_adapters/active_record_adapter_spec.rb +185 -0
  27. data/spec/cancan/model_adapters/data_mapper_adapter_spec.rb +115 -0
  28. data/spec/cancan/model_adapters/default_adapter_spec.rb +7 -0
  29. data/spec/cancan/model_adapters/mongoid_adapter_spec.rb +168 -0
  30. data/spec/cancan/rule_spec.rb +39 -0
  31. data/spec/spec_helper.rb +2 -24
  32. metadata +24 -16
  33. data/lib/cancan/query.rb +0 -97
  34. data/spec/cancan/active_record_additions_spec.rb +0 -75
  35. data/spec/cancan/can_definition_spec.rb +0 -57
  36. data/spec/cancan/query_spec.rb +0 -107
@@ -1,107 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe CanCan::Query do
4
- before(:each) do
5
- @ability = Object.new
6
- @ability.extend(CanCan::Ability)
7
- end
8
-
9
- it "should have false conditions if no abilities match" do
10
- @ability.query(:destroy, Project).conditions.should == "true=false"
11
- end
12
-
13
- it "should return hash for single `can` definition" do
14
- @ability.can :read, Project, :blocked => false, :user_id => 1
15
- @ability.query(:read, Project).conditions.should == { :blocked => false, :user_id => 1 }
16
- end
17
-
18
- it "should merge multiple can definitions into single SQL string joining with OR" do
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
- end
23
-
24
- it "should merge multiple can definitions into single SQL string joining with OR and AND" do
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
- end
29
-
30
- it "should merge multiple can definitions into single SQL string joining with OR and AND" do
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
- end
35
-
36
- it "should return false conditions for cannot clause" do
37
- @ability.cannot :read, Project
38
- @ability.query(:read, Project).conditions.should == "true=false"
39
- end
40
-
41
- it "should return SQL for single `can` definition in front of default `cannot` condition" do
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
- end
46
-
47
- it "should return true condition for single `can` definition in front of default `can` condition" do
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
- end
52
-
53
- it "should return false condition for single `cannot` definition" do
54
- @ability.cannot :read, Project, :blocked => true, :user_id => 1
55
- @ability.query(:read, Project).conditions.should == 'true=false'
56
- end
57
-
58
- it "should return `false condition` for single `cannot` definition in front of default `cannot` condition" do
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
- end
63
-
64
- it "should return `not (sql)` for single `cannot` definition in front of default `can` condition" do
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
- end
69
-
70
- it "should return appropriate sql conditions in complex case" do
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
- end
79
-
80
- it "should have nil joins if no can definitions" do
81
- @ability.query(:read, Project).joins.should be_nil
82
- end
83
-
84
- it "should have nil joins if no nested hashes specified in conditions" do
85
- @ability.can :read, Project, :blocked => false
86
- @ability.can :read, Project, :admin => true
87
- @ability.query(:read, Project).joins.should be_nil
88
- end
89
-
90
- it "should merge separate joins into a single array" do
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
- end
95
-
96
- it "should merge same joins into a single array" do
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
- end
101
-
102
- it "should merge complex, nested joins" do
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
- end
107
- end