rolify 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -0
- data/README.rdoc +3 -3
- data/lib/generators/rolify/role/role_generator.rb +2 -2
- data/lib/generators/rolify/role/templates/initializer.rb +2 -1
- data/lib/generators/rolify/role/templates/migration.rb +7 -3
- data/lib/generators/rolify/role/templates/role.rb +1 -1
- data/lib/rolify.rb +2 -0
- data/lib/rolify/role.rb +22 -5
- data/lib/rolify/rolify_railtie.rb +9 -0
- data/lib/rolify/version.rb +1 -1
- data/spec/rolify/role_spec.rb +227 -208
- data/spec/support/data.rb +4 -1
- data/spec/support/models.rb +12 -0
- data/spec/support/schema.rb +15 -1
- metadata +3 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
= 0.6
|
2
|
+
* custom User and Role class names support
|
3
|
+
* can now use other class names for Role and User classes
|
4
|
+
* fixed generators and templates
|
5
|
+
* join table is explicitly set to avoid alphabetical order issue
|
6
|
+
* created a new railtie to load the dynamic shortcuts at startup
|
7
|
+
|
1
8
|
= 0.5.1
|
2
9
|
* fixed a nasty typo on a variable name and added a spec to make this never happen again
|
3
10
|
|
data/README.rdoc
CHANGED
@@ -11,7 +11,7 @@ This library was intended to be used with CanCan[https://github.com/ryanb/cancan
|
|
11
11
|
|
12
12
|
== Requirements
|
13
13
|
|
14
|
-
* >= Rails 3.1 (
|
14
|
+
* >= Rails 3.1 (rc3 is currently out)
|
15
15
|
* ActiveRecord ORM
|
16
16
|
|
17
17
|
== Installation
|
@@ -91,6 +91,6 @@ If you have any issue or feature request with/for rolify, please add an {issue o
|
|
91
91
|
|
92
92
|
== TODO
|
93
93
|
|
94
|
-
* put syntactic sugar:
|
95
|
-
* <tt>is_admin?</tt> and <tt>is_admin_of?(resource)</tt> like shortcuts
|
96
94
|
* write a tutorial showing how to use rolify with CanCan and devise
|
95
|
+
* complete tests coverage
|
96
|
+
* performance enhancements
|
@@ -12,11 +12,11 @@ module Rolify
|
|
12
12
|
desc "Generates a model with the given NAME and a migration file."
|
13
13
|
|
14
14
|
def generate_role
|
15
|
-
template "role.rb", "app/models
|
15
|
+
template "role.rb", "app/models/#{role_cname.downcase}.rb"
|
16
16
|
inject_into_class(model_path, user_cname.camelize) do
|
17
17
|
" include Rolify::Roles\n" +
|
18
18
|
" extend Rolify::Reloaded\n" +
|
19
|
-
" has_and_belongs_to_many :roles, :class_name => \"
|
19
|
+
" has_and_belongs_to_many :roles#{", :class_name => \"" + role_cname.camelize + "\"" if role_cname != "Role"}, :join_table => :#{user_cname.tableize + "_" + role_cname.tableize}\n"
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -1 +1,2 @@
|
|
1
|
-
<%= user_cname.camelize
|
1
|
+
Rolify.user_cname = <%= user_cname.camelize %>
|
2
|
+
Rolify.role_cname = <%= role_cname.camelize %>
|
@@ -1,15 +1,19 @@
|
|
1
|
-
class RolifyCreate<%= role_cname.camelize %> < ActiveRecord::Migration
|
1
|
+
class RolifyCreate<%= role_cname.pluralize.camelize %> < ActiveRecord::Migration
|
2
2
|
def change
|
3
|
-
create_table(
|
3
|
+
create_table(:<%= role_cname.tableize %>) do |t|
|
4
4
|
t.string :name
|
5
5
|
t.references :resource, :polymorphic => true
|
6
6
|
|
7
7
|
t.timestamps
|
8
8
|
end
|
9
9
|
|
10
|
-
create_table(
|
10
|
+
create_table(:<%= (user_cname.tableize + "_" + role_cname.tableize) %>, :id => false) do |t|
|
11
11
|
t.references :<%= user_cname.underscore.singularize %>
|
12
12
|
t.references :<%= role_cname.underscore.singularize %>
|
13
13
|
end
|
14
|
+
|
15
|
+
add_index(:<%= role_cname.tableize %>, :name)
|
16
|
+
add_index(:<%= role_cname.tableize %>, [ :name, :resource_type, :resource_id ])
|
17
|
+
add_index(:<%= "#{user_cname.tableize}_#{role_cname.tableize}" %>, [ :<%= user_cname.underscore.singularize %>_id, :<%= role_cname.underscore.singularize %>_id ])
|
14
18
|
end
|
15
19
|
end
|
@@ -1,4 +1,4 @@
|
|
1
1
|
class <%= role_cname.camelize %> < ActiveRecord::Base
|
2
|
-
has_and_belongs_to_many :<%= user_cname.tableize %>
|
2
|
+
has_and_belongs_to_many :<%= user_cname.tableize %>, :join_table => :<%= "#{user_cname.tableize}_#{role_cname.tableize}" %>
|
3
3
|
belongs_to :resource, :polymorphic => true
|
4
4
|
end
|
data/lib/rolify.rb
CHANGED
data/lib/rolify/role.rb
CHANGED
@@ -1,11 +1,28 @@
|
|
1
1
|
module Rolify
|
2
2
|
|
3
|
+
def self.role_cname
|
4
|
+
@@role_cname
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.role_cname=(role_cname)
|
8
|
+
@@role_cname = role_cname
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.user_cname
|
12
|
+
@@user_cname
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.user_cname=(user_cname)
|
16
|
+
@@user_cname = user_cname
|
17
|
+
end
|
18
|
+
|
19
|
+
|
3
20
|
module Roles
|
4
21
|
|
5
22
|
def has_role(role_name, resource = nil)
|
6
|
-
role =
|
7
|
-
|
8
|
-
|
23
|
+
role = Rolify.role_cname.find_or_create_by_name_and_resource_type_and_resource_id( :name => role_name,
|
24
|
+
:resource_type => (resource.class.name if resource),
|
25
|
+
:resource_id => (resource.id if resource))
|
9
26
|
if !roles.include?(role)
|
10
27
|
self.class.define_dynamic_method role_name, resource
|
11
28
|
self.roles << role
|
@@ -50,7 +67,7 @@ module Rolify
|
|
50
67
|
end
|
51
68
|
|
52
69
|
def has_no_role(role_name, resource = nil)
|
53
|
-
role =
|
70
|
+
role = self.roles.where( :name => role_name)
|
54
71
|
role = role.where( :resource_type => resource.class.name,
|
55
72
|
:resource_id => resource.id) if resource
|
56
73
|
self.roles.delete(role) if role
|
@@ -78,7 +95,7 @@ module Rolify
|
|
78
95
|
module Reloaded
|
79
96
|
|
80
97
|
def load_dynamic_methods
|
81
|
-
|
98
|
+
Rolify.role_cname.all.each do |r|
|
82
99
|
define_dynamic_method(r.name, r.resource)
|
83
100
|
end
|
84
101
|
end
|
data/lib/rolify/version.rb
CHANGED
data/spec/rolify/role_spec.rb
CHANGED
@@ -1,235 +1,254 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
shared_examples_for "Rolify module" do
|
4
|
+
context "in a Instance level" do
|
5
|
+
before(:all) do
|
6
|
+
Rolify.user_cname = user_cname
|
7
|
+
Rolify.role_cname = role_cname
|
8
|
+
@admin = Rolify.user_cname.first
|
9
|
+
@admin.has_role "admin"
|
10
|
+
@admin.has_role "moderator", Forum.first
|
11
|
+
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
it "should respond to has_role method" do
|
14
|
+
@admin.should respond_to(:has_role).with(1).arguments
|
15
|
+
@admin.should respond_to(:has_role).with(2).arguments
|
16
|
+
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
it "should respond to has_role? method" do
|
19
|
+
@admin.should respond_to(:has_role?).with(1).arguments
|
20
|
+
@admin.should respond_to(:has_role?).with(2).arguments
|
21
|
+
end
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
it "should respond to has_all_roles? method" do
|
24
|
+
@admin.should respond_to(:has_all_roles?)
|
25
|
+
@admin.should respond_to(:has_all_roles?)
|
26
|
+
end
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
28
|
+
it "should respond to has_any_role? method" do
|
29
|
+
@admin.should respond_to(:has_any_role?)
|
30
|
+
@admin.should respond_to(:has_any_role?)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should respond to has_no_role method" do
|
34
|
+
@admin.should respond_to(:has_no_role).with(1).arguments
|
35
|
+
@admin.should respond_to(:has_no_role).with(2).arguments
|
36
|
+
end
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
it "should respond to dynamic methods" do
|
39
|
+
@admin.should respond_to(:is_admin?).with(0).arguments
|
40
|
+
@admin.should respond_to(:is_moderator_of?).with(1).arguments
|
41
|
+
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
+
it "should not respond to any unknown methods" do
|
44
|
+
@admin.should_not respond_to(:is_god?)
|
45
|
+
end
|
43
46
|
end
|
44
|
-
end
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
48
|
+
context "with a global role" do
|
49
|
+
before(:all) do
|
50
|
+
@admin = Rolify.user_cname.first
|
51
|
+
@admin.has_role "admin"
|
52
|
+
@admin.has_role "staff"
|
53
|
+
@admin.has_role "moderator", Forum.first
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should set a global role" do
|
57
|
+
expect { @admin.has_role "superadmin" }.to change{ Rolify.role_cname.count }.by(1)
|
58
|
+
superadmin = Rolify.role_cname.last
|
59
|
+
superadmin.name.should eq("superadmin")
|
60
|
+
superadmin.resource.should be(nil)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should not create another role if already existing" do
|
64
|
+
expect { @admin.has_role "admin" }.not_to change{ Rolify.role_cname.count }
|
65
|
+
expect { @admin.has_role "admin" }.not_to change{ @admin.roles.size }
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should get a global role" do
|
69
|
+
@admin.has_role?("admin").should be(true)
|
70
|
+
end
|
69
71
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
72
|
+
it "should be able to use dynamic shortcut" do
|
73
|
+
@admin.is_admin?.should be(true)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should get any resource request" do
|
77
|
+
@admin.has_role?("admin", Forum.first).should be(true)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should not get another global role" do
|
81
|
+
Rolify.role_cname.create(:name => "global")
|
82
|
+
@admin.has_role?("global").should be(false)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should not get a scoped role" do
|
86
|
+
@admin.has_role?("moderator", Forum.last).should be(false)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should not get inexisting role" do
|
90
|
+
@admin.has_role?("dummy").should be(false)
|
91
|
+
@admin.has_role?("dumber", Forum.first).should be(false)
|
92
|
+
end
|
91
93
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
94
|
+
it "should check if user has all of a global roles set" do
|
95
|
+
@admin.has_role?("staff").should be(true)
|
96
|
+
@admin.has_all_roles?("admin", "staff").should be(true)
|
97
|
+
@admin.has_all_roles?("admin", "dummy").should be(false)
|
98
|
+
@admin.has_all_roles?("dummy", "dumber").should be(false)
|
99
|
+
end
|
98
100
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
101
|
+
it "should check if user has any of a global roles set" do
|
102
|
+
@admin.has_any_role?("admin", "staff").should be(true)
|
103
|
+
@admin.has_any_role?("admin", "moderator").should be(true)
|
104
|
+
@admin.has_any_role?("dummy", "dumber").should be(false)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should remove a global role of a user" do
|
108
|
+
expect { @admin.has_no_role("admin") }.to change{ @admin.roles.size }.by(-1)
|
109
|
+
@admin.has_role?("admin").should be(false)
|
110
|
+
@admin.has_role?("staff").should be(true)
|
111
|
+
@admin.has_role?("moderator", Forum.first).should be(true)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should remove a scoped role of a user" do
|
115
|
+
expect { @admin.has_no_role("moderator") }.to change{ @admin.roles.size }.by(-1)
|
116
|
+
@admin.has_role?("staff").should be(true)
|
117
|
+
@admin.has_role?("moderator", Forum.first).should be(false)
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should not remove a another global role" do
|
121
|
+
expect { @admin.has_no_role("global") }.not_to change{ @admin.roles.size }
|
122
|
+
end
|
120
123
|
end
|
121
|
-
end
|
122
124
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
125
|
+
context "with a scoped role" do
|
126
|
+
before(:all) do
|
127
|
+
@moderator = Rolify.user_cname.find(2)
|
128
|
+
@moderator.has_role "moderator", Forum.first
|
129
|
+
@moderator.has_role "soldier"
|
130
|
+
end
|
129
131
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
132
|
+
it "should set a scoped role" do
|
133
|
+
expect { @moderator.has_role "visitor", Forum.last }.to change{ Rolify.role_cname.count }.by(1)
|
134
|
+
supermodo = Rolify.role_cname.last
|
135
|
+
supermodo.name.should eq("visitor")
|
136
|
+
supermodo.resource.should eq(Forum.last)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should not create another role if already existing" do
|
140
|
+
expect { @moderator.has_role "moderator", Forum.first }.not_to change{ Rolify.role_cname.count }
|
141
|
+
expect { @moderator.has_role "moderator" , Forum.first }.not_to change{ @moderator.roles.size }
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should get a scoped role" do
|
145
|
+
@moderator.has_role?("moderator", Forum.first).should be(true)
|
146
|
+
end
|
145
147
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
148
|
+
it "should be able to use dynamic shortcut" do
|
149
|
+
@moderator.is_moderator?.should be(false)
|
150
|
+
@moderator.is_moderator_of?(Forum.first).should be(true)
|
151
|
+
@moderator.is_moderator_of?(Forum.last).should be(false)
|
152
|
+
end
|
151
153
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
154
|
+
it "should not get a global role" do
|
155
|
+
@moderator.has_role?("admin").should be(false)
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should not get the same role on another resource" do
|
159
|
+
Rolify.role_cname.create(:name => "moderator", :resource => Forum.last)
|
160
|
+
@moderator.has_role?("moderator", Forum.last).should be(false)
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should not get the another role on the same resource" do
|
164
|
+
Rolify.role_cname.create(:name => "tourist", :resource => Forum.first)
|
165
|
+
@moderator.has_role?("tourist", Forum.first).should be(false)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should not get inexisting role" do
|
169
|
+
@moderator.has_role?("dummy", Forum.last).should be(false)
|
170
|
+
@moderator.has_role?("dumber").should be(false)
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should check if user has all of a scoped roles set" do
|
174
|
+
@moderator.has_all_roles?({ :name => "moderator", :resource => Forum.first },
|
175
|
+
{ :name => "visitor", :resource => Forum.last }).should be(true)
|
176
|
+
@moderator.has_all_roles?({ :name => "moderator", :resource => Forum.first },
|
177
|
+
{ :name => "dummy", :resource => Forum.last }).should be(false)
|
178
|
+
@moderator.has_all_roles?({ :name => "dummy", :resource => Forum.first },
|
179
|
+
{ :name => "dumber", :resource => Forum.last }).should be(false)
|
180
|
+
end
|
179
181
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
182
|
+
it "should check if user has any of a scoped roles set" do
|
183
|
+
@moderator.has_any_role?( { :name => "moderator", :resource => Forum.first },
|
184
|
+
{ :name => "visitor", :resource => Forum.last }).should be(true)
|
185
|
+
@moderator.has_any_role?( { :name => "moderator", :resource => Forum.first },
|
186
|
+
{ :name => "dummy", :resource => Forum.last }).should be(true)
|
187
|
+
@moderator.has_any_role?( { :name => "dummy", :resource => Forum.first },
|
188
|
+
{ :name => "dumber", :resource => Forum.last }).should be(false)
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should not remove a global role of a user" do
|
192
|
+
expect { @moderator.has_no_role("soldier", Forum.first) }.not_to change{ @moderator.roles.size }
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should remove a scoped role of a user" do
|
196
|
+
expect { @moderator.has_no_role("moderator", Forum.first) }.to change{ @moderator.roles.size }.by(-1)
|
197
|
+
@moderator.has_role?("moderator", Forum.first).should be(false)
|
198
|
+
@moderator.has_role?("soldier").should be(true)
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should not remove another scoped role" do
|
202
|
+
expect { @moderator.has_no_role("visitor", Forum.first) }.not_to change{ @moderator.roles.size }
|
203
|
+
end
|
187
204
|
end
|
188
|
-
|
189
|
-
it "should not remove a global role of a user" do
|
190
|
-
expect { @moderator.has_no_role("soldier", Forum.first) }.not_to change{ @moderator.roles.size }
|
191
|
-
end
|
192
|
-
|
193
|
-
it "should remove a scoped role of a user" do
|
194
|
-
expect { @moderator.has_no_role("moderator", Forum.first) }.to change{ @moderator.roles.size }.by(-1)
|
195
|
-
@moderator.has_role?("moderator", Forum.first).should be(false)
|
196
|
-
@moderator.has_role?("soldier").should be(true)
|
197
|
-
end
|
198
|
-
|
199
|
-
it "should not remove another scoped role" do
|
200
|
-
expect { @moderator.has_no_role("visitor", Forum.first) }.not_to change{ @moderator.roles.size }
|
201
|
-
end
|
202
|
-
end
|
203
205
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
206
|
+
context "with different roles" do
|
207
|
+
before(:all) do
|
208
|
+
@user = Rolify.user_cname.last
|
209
|
+
@user.has_role "admin"
|
210
|
+
@user.has_role "moderator", Forum.first
|
211
|
+
@user.has_role "visitor", Forum.last
|
212
|
+
@user.has_role "anonymous"
|
213
|
+
end
|
214
|
+
|
215
|
+
it "should get a global role" do
|
216
|
+
@user.has_role?("admin").should be(true)
|
217
|
+
@user.has_role?("anonymous").should be(true)
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should get a scoped role" do
|
221
|
+
@user.has_role?("moderator", Forum.first).should be(true)
|
222
|
+
@user.has_role?("visitor", Forum.last).should be(true)
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should check if user has all of a mix of global and scoped roles set" do
|
226
|
+
@user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.first }).should be(true)
|
227
|
+
@user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.last }).should be(false)
|
228
|
+
@user.has_all_roles?("dummy", { :name => "dumber", :resource => Forum.last }).should be(false)
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should check if user has any of a mix of global and scoped roles set" do
|
232
|
+
@user.has_any_role?("admin", { :name => "moderator", :resource => Forum.first }).should be(true)
|
233
|
+
@user.has_any_role?("admin", { :name => "moderator", :resource => Forum.last }).should be(true)
|
234
|
+
@user.has_any_role?("dummy", { :name => "dumber", :resource => Forum.last }).should be(false)
|
235
|
+
end
|
221
236
|
end
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
describe Rolify do
|
241
|
+
context "using default Role and User class names" do
|
242
|
+
it_behaves_like "Rolify module" do
|
243
|
+
let(:user_cname) { User }
|
244
|
+
let(:role_cname) { Role }
|
227
245
|
end
|
246
|
+
end
|
228
247
|
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
248
|
+
context "using custom User and Role class names" do
|
249
|
+
it_behaves_like "Rolify module" do
|
250
|
+
let(:user_cname) { Customer }
|
251
|
+
let(:role_cname) { Privilege }
|
233
252
|
end
|
234
253
|
end
|
235
254
|
end
|
data/spec/support/data.rb
CHANGED
@@ -3,4 +3,7 @@ User.create(:login => "moderator")
|
|
3
3
|
User.create(:login => "god")
|
4
4
|
Forum.create(:name => "forum 1")
|
5
5
|
Forum.create(:name => "forum 2")
|
6
|
-
Forum.create(:name => "forum 3")
|
6
|
+
Forum.create(:name => "forum 3")
|
7
|
+
|
8
|
+
Customer.create(:login => "customer VIP")
|
9
|
+
Customer.create(:login => "customer Doe")
|
data/spec/support/models.rb
CHANGED
@@ -11,3 +11,15 @@ end
|
|
11
11
|
|
12
12
|
class Forum < ActiveRecord::Base
|
13
13
|
end
|
14
|
+
|
15
|
+
class Customer < ActiveRecord::Base
|
16
|
+
has_and_belongs_to_many :roles, :join_table => :customers_privileges, :class_name => "Privilege"
|
17
|
+
include Rolify::Roles
|
18
|
+
extend Rolify::Reloaded
|
19
|
+
end
|
20
|
+
|
21
|
+
class Privilege < ActiveRecord::Base
|
22
|
+
has_and_belongs_to_many :customers, :join_table => :customers_privileges
|
23
|
+
belongs_to :resource, :polymorphic => true
|
24
|
+
end
|
25
|
+
|
data/spec/support/schema.rb
CHANGED
@@ -20,4 +20,18 @@ ActiveRecord::Schema.define do
|
|
20
20
|
create_table(:forums) do |t|
|
21
21
|
t.string :name
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
|
+
create_table(:privileges) do |t|
|
25
|
+
t.string :name
|
26
|
+
t.references :resource, :polymorphic => true
|
27
|
+
end
|
28
|
+
|
29
|
+
create_table(:customers) do |t|
|
30
|
+
t.string :login
|
31
|
+
end
|
32
|
+
|
33
|
+
create_table(:customers_privileges, :id => false) do |t|
|
34
|
+
t.references :customer
|
35
|
+
t.references :privilege
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rolify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.6.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Florent Monbillard
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-06-
|
13
|
+
date: 2011-06-20 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: sqlite3
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- lib/generators/rolify/role/templates/role.rb
|
57
57
|
- lib/rolify.rb
|
58
58
|
- lib/rolify/role.rb
|
59
|
+
- lib/rolify/rolify_railtie.rb
|
59
60
|
- lib/rolify/version.rb
|
60
61
|
- rolify.gemspec
|
61
62
|
- spec/rolify/role_spec.rb
|