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 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 (rc1 is currently out)
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/role.rb"
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 => \"#{role_cname.camelize}\"\n"
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 %>.load_dynamic_methods
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(<%= role_cname.tableize.to_sym %>) do |t|
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(<%= (user_cname.tableize + "_" + role_cname.tableize).to_sym %>, :id => false) do |t|
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
@@ -1,3 +1,5 @@
1
1
  require 'active_record'
2
2
 
3
3
  require 'rolify/role'
4
+ require 'rolify/rolify_railtie.rb' if defined?(Rails)
5
+
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 = Role.find_or_create_by_name_and_resource_type_and_resource_id( :name => role_name,
7
- :resource_type => (resource.class.name if resource),
8
- :resource_id => (resource.id if resource))
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 = Role.where( :name => role_name)
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
- Role.all.each do |r|
98
+ Rolify.role_cname.all.each do |r|
82
99
  define_dynamic_method(r.name, r.resource)
83
100
  end
84
101
  end
@@ -0,0 +1,9 @@
1
+ module Rolify
2
+ class RoleRailtie < ::Rails::Railtie
3
+ initializer "instantiate roles methods" do
4
+ ActiveSupport.on_load :active_record do
5
+ Rolify.user_cname.load_dynamic_methods if defined? Rails.server
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Rolify
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -1,235 +1,254 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Rolify do
4
- context "in a Instance level" do
5
- before(:all) do
6
- @admin = User.first
7
- @admin.has_role "admin"
8
- @admin.has_role "moderator", Forum.first
9
- end
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
- it "should respond to has_role method" do
12
- @admin.should respond_to(:has_role).with(1).arguments
13
- @admin.should respond_to(:has_role).with(2).arguments
14
- end
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
- it "should respond to has_role? method" do
17
- @admin.should respond_to(:has_role?).with(1).arguments
18
- @admin.should respond_to(:has_role?).with(2).arguments
19
- end
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
- it "should respond to has_all_roles? method" do
22
- @admin.should respond_to(:has_all_roles?)
23
- @admin.should respond_to(:has_all_roles?)
24
- end
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
- it "should respond to has_any_role? method" do
27
- @admin.should respond_to(:has_any_role?)
28
- @admin.should respond_to(:has_any_role?)
29
- end
30
-
31
- it "should respond to has_no_role method" do
32
- @admin.should respond_to(:has_no_role).with(1).arguments
33
- @admin.should respond_to(:has_no_role).with(2).arguments
34
- end
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
- it "should respond to dynamic methods" do
37
- @admin.should respond_to(:is_admin?).with(0).arguments
38
- @admin.should respond_to(:is_moderator_of?).with(1).arguments
39
- end
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
- it "should not respond to any unknown methods" do
42
- @admin.should_not respond_to(:is_god?)
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
- context "with a global role" do
47
- before(:all) do
48
- @admin = User.first
49
- @admin.has_role "admin"
50
- @admin.has_role "staff"
51
- @admin.has_role "moderator", Forum.first
52
- end
53
-
54
- it "should set a global role" do
55
- expect { @admin.has_role "superadmin" }.to change{ Role.count }.by(1)
56
- superadmin = Role.last
57
- superadmin.name.should eq("superadmin")
58
- superadmin.resource.should be(nil)
59
- end
60
-
61
- it "should not create another role if already existing" do
62
- expect { @admin.has_role "admin" }.not_to change{ Role.count }
63
- expect { @admin.has_role "admin" }.not_to change{ @admin.roles.size }
64
- end
65
-
66
- it "should get a global role" do
67
- @admin.has_role?("admin").should be(true)
68
- end
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
- it "should be able to use dynamic shortcut" do
71
- @admin.is_admin?.should be(true)
72
- end
73
-
74
- it "should get any resource request" do
75
- @admin.has_role?("admin", Forum.first).should be(true)
76
- end
77
-
78
- it "should not get another global role" do
79
- Role.create(:name => "global")
80
- @admin.has_role?("global").should be(false)
81
- end
82
-
83
- it "should not get a scoped role" do
84
- @admin.has_role?("moderator", Forum.last).should be(false)
85
- end
86
-
87
- it "should not get inexisting role" do
88
- @admin.has_role?("dummy").should be(false)
89
- @admin.has_role?("dumber", Forum.first).should be(false)
90
- end
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
- it "should check if user has all of a global roles set" do
93
- @admin.has_role?("staff").should be(true)
94
- @admin.has_all_roles?("admin", "staff").should be(true)
95
- @admin.has_all_roles?("admin", "dummy").should be(false)
96
- @admin.has_all_roles?("dummy", "dumber").should be(false)
97
- end
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
- it "should check if user has any of a global roles set" do
100
- @admin.has_any_role?("admin", "staff").should be(true)
101
- @admin.has_any_role?("admin", "moderator").should be(true)
102
- @admin.has_any_role?("dummy", "dumber").should be(false)
103
- end
104
-
105
- it "should remove a global role of a user" do
106
- expect { @admin.has_no_role("admin") }.to change{ @admin.roles.size }.by(-1)
107
- @admin.has_role?("admin").should be(false)
108
- @admin.has_role?("staff").should be(true)
109
- @admin.has_role?("moderator", Forum.first).should be(true)
110
- end
111
-
112
- it "should remove a scoped role of a user" do
113
- expect { @admin.has_no_role("moderator") }.to change{ @admin.roles.size }.by(-1)
114
- @admin.has_role?("staff").should be(true)
115
- @admin.has_role?("moderator", Forum.first).should be(false)
116
- end
117
-
118
- it "should not remove a another global role" do
119
- expect { @admin.has_no_role("global") }.not_to change{ @admin.roles.size }
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
- context "with a scoped role" do
124
- before(:all) do
125
- @moderator = User.find(2)
126
- @moderator.has_role "moderator", Forum.first
127
- @moderator.has_role "soldier"
128
- end
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
- it "should set a scoped role" do
131
- expect { @moderator.has_role "visitor", Forum.last }.to change{ Role.count }.by(1)
132
- supermodo = Role.last
133
- supermodo.name.should eq("visitor")
134
- supermodo.resource.should eq(Forum.last)
135
- end
136
-
137
- it "should not create another role if already existing" do
138
- expect { @moderator.has_role "moderator", Forum.first }.not_to change{ Role.count }
139
- expect { @moderator.has_role "moderator" , Forum.first }.not_to change{ @moderator.roles.size }
140
- end
141
-
142
- it "should get a scoped role" do
143
- @moderator.has_role?("moderator", Forum.first).should be(true)
144
- end
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
- it "should be able to use dynamic shortcut" do
147
- @moderator.is_moderator?.should be(false)
148
- @moderator.is_moderator_of?(Forum.first).should be(true)
149
- @moderator.is_moderator_of?(Forum.last).should be(false)
150
- end
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
- it "should not get a global role" do
153
- @moderator.has_role?("admin").should be(false)
154
- end
155
-
156
- it "should not get the same role on another resource" do
157
- Role.create(:name => "moderator", :resource => Forum.last)
158
- @moderator.has_role?("moderator", Forum.last).should be(false)
159
- end
160
-
161
- it "should not get the another role on the same resource" do
162
- Role.create(:name => "tourist", :resource => Forum.first)
163
- @moderator.has_role?("tourist", Forum.first).should be(false)
164
- end
165
-
166
- it "should not get inexisting role" do
167
- @moderator.has_role?("dummy", Forum.last).should be(false)
168
- @moderator.has_role?("dumber").should be(false)
169
- end
170
-
171
- it "should check if user has all of a scoped roles set" do
172
- @moderator.has_all_roles?({ :name => "moderator", :resource => Forum.first },
173
- { :name => "visitor", :resource => Forum.last }).should be(true)
174
- @moderator.has_all_roles?({ :name => "moderator", :resource => Forum.first },
175
- { :name => "dummy", :resource => Forum.last }).should be(false)
176
- @moderator.has_all_roles?({ :name => "dummy", :resource => Forum.first },
177
- { :name => "dumber", :resource => Forum.last }).should be(false)
178
- end
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
- it "should check if user has any of a scoped roles set" do
181
- @moderator.has_any_role?( { :name => "moderator", :resource => Forum.first },
182
- { :name => "visitor", :resource => Forum.last }).should be(true)
183
- @moderator.has_any_role?( { :name => "moderator", :resource => Forum.first },
184
- { :name => "dummy", :resource => Forum.last }).should be(true)
185
- @moderator.has_any_role?( { :name => "dummy", :resource => Forum.first },
186
- { :name => "dumber", :resource => Forum.last }).should be(false)
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
- context "with different roles" do
205
- before(:all) do
206
- @user = User.last
207
- @user.has_role "admin"
208
- @user.has_role "moderator", Forum.first
209
- @user.has_role "visitor", Forum.last
210
- @user.has_role "anonymous"
211
- end
212
-
213
- it "should get a global role" do
214
- @user.has_role?("admin").should be(true)
215
- @user.has_role?("anonymous").should be(true)
216
- end
217
-
218
- it "should get a scoped role" do
219
- @user.has_role?("moderator", Forum.first).should be(true)
220
- @user.has_role?("visitor", Forum.last).should be(true)
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
- it "should check if user has all of a mix of global and scoped roles set" do
224
- @user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.first }).should be(true)
225
- @user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.last }).should be(false)
226
- @user.has_all_roles?("dummy", { :name => "dumber", :resource => Forum.last }).should be(false)
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
- it "should check if user has any of a mix of global and scoped roles set" do
230
- @user.has_any_role?("admin", { :name => "moderator", :resource => Forum.first }).should be(true)
231
- @user.has_any_role?("admin", { :name => "moderator", :resource => Forum.last }).should be(true)
232
- @user.has_any_role?("dummy", { :name => "dumber", :resource => Forum.last }).should be(false)
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")
@@ -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
+
@@ -20,4 +20,18 @@ ActiveRecord::Schema.define do
20
20
  create_table(:forums) do |t|
21
21
  t.string :name
22
22
  end
23
- end
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.1
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-07 00:00:00 Z
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