canard 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +26 -10
- data/TODO +2 -2
- data/lib/ability.rb +4 -4
- data/lib/canard/version.rb +1 -1
- data/lib/generators/rspec/ability/templates/abilities_spec.rb.erb +20 -7
- metadata +14 -14
data/README.rdoc
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
= Canard
|
2
|
-
|
2
|
+
== Overview
|
3
3
|
Canard brings CanCan and RoleModel together to make role based authorization in Rails easy. Your ability
|
4
4
|
definitions gain their own folder and a little structure. The easiest way to get started is with the
|
5
5
|
Canard generator. Canard progressively enhances the abilities of the model by applying role abilities on
|
@@ -9,11 +9,21 @@ A User model with :admin and :manger roles would be defined:
|
|
9
9
|
|
10
10
|
class User < ActiveRecord::Base
|
11
11
|
|
12
|
-
acts_as_user :roles =>
|
12
|
+
acts_as_user :roles => :manager, :admin
|
13
13
|
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
If a User has both the :manager and :admin roles Canard will apply the abilities in the following order.
|
17
|
+
First it will look for a users abilities, then it will look for the roles in the order they are defined e.g.
|
18
|
+
|
19
|
+
app/abilities/users.rb
|
20
|
+
app/abilities/manager.rb
|
21
|
+
app/abilities/admin.rb
|
22
|
+
|
23
|
+
Therefore each the later abilities only need to build on their predecessors.
|
24
|
+
|
25
|
+
== Usage
|
26
|
+
To generate some abilities for the User.
|
17
27
|
|
18
28
|
$ rails g canard:ability user can:[read,create]:[account,statement] cannot:destroy:account
|
19
29
|
create app/abilities/users.rb
|
@@ -29,7 +39,7 @@ Generates an ability folder in Rails root and an associated spec;
|
|
29
39
|
|
30
40
|
The resulting app/abilities/users.rb will look something like this;
|
31
41
|
|
32
|
-
|
42
|
+
Canard::Abilities.for(:user) do
|
33
43
|
|
34
44
|
can [:read, :create], Account
|
35
45
|
cannot [:destroy], Account
|
@@ -82,18 +92,19 @@ Now lets generate some abilities for the manager and admin.
|
|
82
92
|
$ rails g canard:ability admin can:manage:[account,statement]
|
83
93
|
$ rails g canard:ability manager can:edit:statement
|
84
94
|
|
85
|
-
|
95
|
+
Gives us two new sets of abilities in the abilities folder. Canard will apply these abilities by first
|
86
96
|
loading the ability for the User model and then apply the abilities for each role the current user has.
|
87
97
|
|
88
|
-
|
98
|
+
|
99
|
+
If there is no user (i.e. logged out) Canard creates a guest and looks for a guest ability to apply so:
|
89
100
|
|
90
101
|
$ rails g canard:ability guest can:create:user
|
91
102
|
|
92
103
|
Would generate an ability for a not logged in user to signup.
|
93
104
|
|
94
|
-
Obviously the generators are just a starting point and should
|
95
|
-
suggest that every new model you create you add to the abilities as the specs are
|
96
|
-
definitions are
|
105
|
+
Obviously the generators are just a starting point and should be used only to get you going. I strongly
|
106
|
+
suggest that every new model you create you add to the abilities as the specs are easy to write and CanCan
|
107
|
+
definitions are very clear and simple.
|
97
108
|
|
98
109
|
== Scopes
|
99
110
|
|
@@ -103,7 +114,7 @@ above it will define the following scopes;
|
|
103
114
|
User.admins:: return all the users with the admin role
|
104
115
|
User.non_admins:: return all the users without the admin role
|
105
116
|
User.managers:: return all the users with the manager role
|
106
|
-
User.
|
117
|
+
User.non_managers:: return all the users without the manager role
|
107
118
|
|
108
119
|
In addition to the role specific scopes it also adds some general scopes;
|
109
120
|
|
@@ -134,6 +145,11 @@ to accept pull requests for tested Rails 2.x updates if anybody is game.
|
|
134
145
|
bump version in a commit by itself I can ignore it when I pull)
|
135
146
|
* Send me a pull request. Bonus points for topic branches.
|
136
147
|
|
148
|
+
== Contributors
|
149
|
+
|
150
|
+
* James McCarthy
|
151
|
+
* Joey Geiger
|
152
|
+
|
137
153
|
== Credits
|
138
154
|
|
139
155
|
Thanks to Ryan Bates for creating the awesome CanCan (http://wiki.github.com/ryanb/cancan/role-based-authorization)
|
data/TODO
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
* create ability generator for a new model e.g. rails g canard:abilitiy user:manage
|
2
1
|
* Test the ability class.
|
3
2
|
* Test the generators.
|
4
3
|
* Test the railtie
|
5
4
|
* Expand the tests to produce all the standard abilities: index,show,read,new,create,edit,update,destroy.
|
6
|
-
*
|
5
|
+
* Add test unit generator.
|
6
|
+
* Add install generator to allow overriding of the default tests.
|
7
7
|
* Add some rdocs.
|
data/lib/ability.rb
CHANGED
@@ -9,17 +9,17 @@ class Ability
|
|
9
9
|
|
10
10
|
if @user
|
11
11
|
# Add the base user abilities.
|
12
|
-
|
12
|
+
append_abilities @user.class.name.underscore.to_sym
|
13
13
|
else
|
14
14
|
# If user not set then lets create a guest
|
15
15
|
@user = Object.new
|
16
|
-
|
16
|
+
append_abilities :guest
|
17
17
|
end
|
18
18
|
|
19
19
|
# If user has roles get those abilities
|
20
20
|
if @user.respond_to?(:roles)
|
21
21
|
# Add roles on top of the base user abilities
|
22
|
-
@user.roles.each { |role|
|
22
|
+
@user.roles.each { |role| append_abilities(role) }
|
23
23
|
end
|
24
24
|
|
25
25
|
end
|
@@ -34,7 +34,7 @@ class Ability
|
|
34
34
|
Canard::Abilities.definitions
|
35
35
|
end
|
36
36
|
|
37
|
-
def
|
37
|
+
def append_abilities(role)
|
38
38
|
instance_eval(&ability_definitions[role]) if ability_definitions.has_key?(role)
|
39
39
|
end
|
40
40
|
|
data/lib/canard/version.rb
CHANGED
@@ -2,13 +2,19 @@ require_relative '../spec_helper'
|
|
2
2
|
|
3
3
|
require "cancan/matchers"
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe Canard::Abilities, "for <%= plural_name %>" do
|
6
6
|
|
7
7
|
before do
|
8
|
-
|
8
|
+
<% if Rails.configuration.generators.options[:rails][:fixture_replacement] == :factory_girl -%>
|
9
|
+
@user = Factory.create(:<%= name %>_user)
|
10
|
+
<% elsif Rails.configuration.generators.options[:rails][:fixture_replacement] == :machinist -%>
|
11
|
+
@user = User.make!(:<%= name %>)
|
12
|
+
<% else -%>
|
13
|
+
@user = User.create(:roles => %w(<%= name -%>))
|
14
|
+
<% end -%>
|
9
15
|
end
|
10
16
|
|
11
|
-
subject { Ability.new(
|
17
|
+
subject { Ability.new(@user) }
|
12
18
|
|
13
19
|
<% if ability_definitions.empty? -%>
|
14
20
|
# Define your ability tests thus;
|
@@ -32,22 +38,29 @@ describe Ability, "for :<%= name %>" do
|
|
32
38
|
# # on Activity
|
33
39
|
<% else -%>
|
34
40
|
<% definitions do |model, definition| -%>
|
41
|
+
<% model_name = model.camelize -%>
|
35
42
|
|
36
|
-
describe 'on <%=
|
43
|
+
describe 'on <%= model_name -%>' do
|
37
44
|
|
38
45
|
before do
|
46
|
+
<% if Rails.configuration.generators.options[:rails][:fixture_replacement] == :factory_girl -%>
|
39
47
|
@<%= model -%> = Factory.create(:<%= model -%>)
|
48
|
+
<% elsif Rails.configuration.generators.options[:rails][:fixture_replacement] == :machinist -%>
|
49
|
+
@<%= model -%> = <%= model_name -%>.make!
|
50
|
+
<% else -%>
|
51
|
+
@<%= model -%> = <%= model_name -%>.create
|
52
|
+
<% end -%>
|
40
53
|
end
|
41
54
|
|
42
55
|
<% definition.cans.each do |can| -%>
|
43
|
-
it { should be_able_to( <%= ":#{can},".ljust(12, ' ') +
|
56
|
+
it { should be_able_to( <%= ":#{can},".ljust(12, ' ') + "@#{model}" -%> ) }
|
44
57
|
<% end -%>
|
45
58
|
<%- definition.cannots.each do |cannot| -%>
|
46
|
-
it { should_not be_able_to( <%= ":#{cannot},".ljust(12, ' ') +
|
59
|
+
it { should_not be_able_to( <%= ":#{cannot},".ljust(12, ' ') + "@#{model}" -%> ) }
|
47
60
|
<% end -%>
|
48
61
|
|
49
62
|
end
|
50
|
-
# on <%=
|
63
|
+
# on <%= model_name %>
|
51
64
|
<% end -%>
|
52
65
|
|
53
66
|
<% end -%>
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-26 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
16
|
-
requirement: &
|
16
|
+
requirement: &2156040520 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2156040520
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sqlite3
|
27
|
-
requirement: &
|
27
|
+
requirement: &2156040060 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2156040060
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activerecord
|
38
|
-
requirement: &
|
38
|
+
requirement: &2156039580 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2156039580
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: activesupport
|
49
|
-
requirement: &
|
49
|
+
requirement: &2156038960 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2156038960
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: cancan
|
60
|
-
requirement: &
|
60
|
+
requirement: &2156038380 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2156038380
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: role_model
|
71
|
-
requirement: &
|
71
|
+
requirement: &2156037880 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :runtime
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *2156037880
|
80
80
|
description: Wraps CanCan and RoleModel up to make role based authorisation really
|
81
81
|
easy in Rails 3.x.
|
82
82
|
email:
|