can_opener 0.0.2 → 0.0.3

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.
@@ -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)
@@ -27,9 +27,9 @@ module CanOpener
27
27
  end
28
28
  end
29
29
 
30
- def initialize(user)
30
+ def initialize(*args)
31
31
  self.class.ability_classes.each do |ability_class|
32
- ability_class.new(self, user)
32
+ ability_class.new(self, *args)
33
33
  end
34
34
  end
35
35
  end
@@ -6,11 +6,32 @@ module CanOpener
6
6
 
7
7
  attr_accessor :user
8
8
  protected :user=
9
-
10
- def initialize(base, user)
9
+
10
+ def initialize(base, *args)
11
11
  @base = base
12
- self.user = user
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
@@ -1,3 +1,3 @@
1
1
  module CanOpener
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brendon Murphy