can_opener 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +31 -1
- data/lib/can_opener.rb +2 -2
- data/lib/can_opener/ability.rb +24 -3
- data/lib/can_opener/version.rb +1 -1
- data/spec/can_opener_spec.rb +37 -0
- metadata +3 -3
data/README.markdown
CHANGED
@@ -52,7 +52,38 @@ Then declare your Ability class, but include `CanOpener` rather than `CanCan::Ab
|
|
52
52
|
end
|
53
53
|
|
54
54
|
Remember that CanCan processes abilities in a top down fashion, so add your general abilities up top, and then things you want to override everything (like banning a user) at the bottom. You can add line by line or multiple on one line if you wish.
|
55
|
+
|
56
|
+
### But I want to check the IP Address, Project, etc, in the ability!
|
57
|
+
|
58
|
+
Inherit `CanOpener::Ability` , setup your accessors, use the `aditional_ability_arguments` class method. Consider the following contrived example:
|
59
|
+
|
60
|
+
class TakesIpAddress < CanOpener::Ability
|
61
|
+
attr_reader :ip_address
|
62
|
+
aditional_ability_arguments :ip_address
|
63
|
+
end
|
64
|
+
|
65
|
+
class SuperAdmin < TakesIpAddress
|
66
|
+
def abilities
|
67
|
+
# Wide open, just for testing
|
68
|
+
can :manage, :foo
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class IPBouncer < TakesIpAddress
|
73
|
+
def abilities
|
74
|
+
cannot :manage, :foo unless ip_address =~ /^192\.168\./
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class TwoParamAbility
|
79
|
+
include CanOpener
|
55
80
|
|
81
|
+
configure_abilities do |c|
|
82
|
+
c.add SuperAdmin
|
83
|
+
c.add IPBouncer
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
56
87
|
Why?
|
57
88
|
----
|
58
89
|
|
@@ -62,4 +93,3 @@ Todo
|
|
62
93
|
----
|
63
94
|
|
64
95
|
* Perhaps a generator for CanOpener::Ability subclasses.
|
65
|
-
* Allow easy initializer overriding to allow additional values passed to the ability (Project, IP Address, etc)
|
data/lib/can_opener.rb
CHANGED
data/lib/can_opener/ability.rb
CHANGED
@@ -6,11 +6,32 @@ module CanOpener
|
|
6
6
|
|
7
7
|
attr_accessor :user
|
8
8
|
protected :user=
|
9
|
-
|
10
|
-
def initialize(base,
|
9
|
+
|
10
|
+
def initialize(base, *args)
|
11
11
|
@base = base
|
12
|
-
|
12
|
+
setup_vars(*args)
|
13
13
|
abilities
|
14
14
|
end
|
15
|
+
|
16
|
+
def self.aditional_ability_arguments(*args)
|
17
|
+
@@additional_arguments = [:user]
|
18
|
+
args.each_with_index do |arg, idx|
|
19
|
+
raise ArgumentError, "user cannot be an additional argument as it is required as the first arg" if arg.to_sym == :user
|
20
|
+
@@additional_arguments << arg.to_sym
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def setup_vars(*args)
|
27
|
+
if @@additional_arguments && @@additional_arguments.length > 0
|
28
|
+
@@additional_arguments.each_with_index do |arg, idx|
|
29
|
+
self.class.send(:attr_accessor, arg)
|
30
|
+
self.send("#{arg}=", args[idx])
|
31
|
+
end
|
32
|
+
else
|
33
|
+
@user = args[0]
|
34
|
+
end
|
35
|
+
end
|
15
36
|
end
|
16
37
|
end
|
data/lib/can_opener/version.rb
CHANGED
data/spec/can_opener_spec.rb
CHANGED
@@ -138,4 +138,41 @@ describe CanOpener do
|
|
138
138
|
ability.should be_able_to(:read, :foo)
|
139
139
|
end
|
140
140
|
end
|
141
|
+
|
142
|
+
describe "passing objects to the ability" do
|
143
|
+
class TakesIpAddress < CanOpener::Ability
|
144
|
+
attr_reader :ip_address
|
145
|
+
aditional_ability_arguments :ip_address
|
146
|
+
end
|
147
|
+
|
148
|
+
class SuperAdmin < TakesIpAddress
|
149
|
+
def abilities
|
150
|
+
# Wide open, just for testing
|
151
|
+
can :manage, :foo
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
class IPBouncer < TakesIpAddress
|
156
|
+
def abilities
|
157
|
+
cannot :manage, :foo unless ip_address =~ /^192\.168\./
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
class TwoParamAbility
|
162
|
+
include CanOpener
|
163
|
+
|
164
|
+
configure_abilities do |c|
|
165
|
+
c.add SuperAdmin
|
166
|
+
c.add IPBouncer
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should allow an object in addition to the user" do
|
171
|
+
ability = TwoParamAbility.new(admin_user, "1.2.3.4")
|
172
|
+
ability.should_not be_able_to(:manage, :foo)
|
173
|
+
|
174
|
+
ability = TwoParamAbility.new(admin_user, "192.168.1.1")
|
175
|
+
ability.should be_able_to(:manage, :foo)
|
176
|
+
end
|
177
|
+
end
|
141
178
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_opener
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Brendon Murphy
|