roles 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +19 -0
- data/lib/generators/roles/role/templates/role-active_record.rb +1 -0
- data/lib/roles.rb +1 -1
- data/lib/roles/resource.rb +4 -4
- data/lib/roles/role.rb +5 -5
- data/lib/roles/version.rb +1 -1
- data/roles.gemspec +1 -0
- data/spec/generators/roles/role/role_generator_spec.rb +88 -0
- data/spec/roles/resource_spec.rb +59 -0
- data/spec/roles/role_spec.rb +173 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/support/active_record.rb +32 -0
- data/spec/support/schema.rb +33 -0
- metadata +32 -4
- data/CHANGELOG.rdoc +0 -14
data/CHANGELOG.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
## v0.1.0 (Sep 2, 2012)
|
2
|
+
- add spec test set
|
3
|
+
- fix duplicate records in `resources#users_with_role`, `users#resources_with_role` and `users#with_role`
|
4
|
+
- fix roles records not destroyed when user destroyed
|
5
|
+
|
6
|
+
## v0.0.5 (Aug 23, 2012)
|
7
|
+
- fix user_cname nil problem in User class
|
8
|
+
|
9
|
+
## v0.0.4 (Aug 23, 2012)
|
10
|
+
- fix method name error(roles -> rolify)
|
11
|
+
|
12
|
+
## v0.0.3 (Aug 23, 2012)
|
13
|
+
- fix generator error
|
14
|
+
|
15
|
+
## v0.0.2 (Aug 19, 2012)
|
16
|
+
- rename `user.resources` to `user.resources_with_role`
|
17
|
+
|
18
|
+
## v0.0.1 (Aug 17, 2012)
|
19
|
+
- first release
|
@@ -2,4 +2,5 @@ class <%= role_cname.camelize %> < ActiveRecord::Base
|
|
2
2
|
belongs_to :<%= user_cname.tableize.singularize %>
|
3
3
|
belongs_to :resource, :polymorphic => true
|
4
4
|
|
5
|
+
validates_uniqueness_of :name, :scope => [:<%= user_cname.underscore.singularize %>_id, :resource_type, :resource_id]
|
5
6
|
end
|
data/lib/roles.rb
CHANGED
@@ -12,7 +12,7 @@ module Roles
|
|
12
12
|
options.reverse_merge!({:role_cname => 'Role'})
|
13
13
|
options.reverse_merge!({:user_cname => 'User'})
|
14
14
|
|
15
|
-
roles_options = { :class_name => options[:role_cname].camelize }
|
15
|
+
roles_options = { :class_name => options[:role_cname].camelize, :dependent => :destroy }
|
16
16
|
roles_options.merge!(options.select{ |k,v| [:before_add, :after_add, :before_remove, :after_remove].include? k.to_sym })
|
17
17
|
|
18
18
|
has_many :roles, roles_options
|
data/lib/roles/resource.rb
CHANGED
@@ -7,18 +7,18 @@ module Roles
|
|
7
7
|
module ClassMethods
|
8
8
|
def users_with_role(role_name = nil)
|
9
9
|
if role_name.nil?
|
10
|
-
self.user_class.
|
10
|
+
self.user_class.includes(:roles).where("roles.resource_type LIKE '%s'", self.to_s).where("roles.resource_id IS NULL")
|
11
11
|
else
|
12
|
-
self.user_class.
|
12
|
+
self.user_class.includes(:roles).where("roles.resource_type LIKE '%s'", self.to_s).where("roles.resource_id IS NULL").where("roles.name LIKE '%s'", role_name.to_s)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
def users_with_role(role_name = nil)
|
18
18
|
if role_name.nil?
|
19
|
-
self.class.user_class.
|
19
|
+
self.class.user_class.includes(:roles).where("roles.resource_type LIKE '%s'", self.class.to_s).where("roles.resource_id = %s", self.id)
|
20
20
|
else
|
21
|
-
self.class.user_class.
|
21
|
+
self.class.user_class.includes(:roles).where("roles.resource_type LIKE '%s'", self.class.to_s).where("roles.resource_id = %s", self.id).where("roles.name LIKE '%s'", role_name.to_s)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/lib/roles/role.rb
CHANGED
@@ -7,11 +7,11 @@ module Roles
|
|
7
7
|
module ClassMethods
|
8
8
|
def with_role(role_name, resource = nil)
|
9
9
|
if resource.nil?
|
10
|
-
self.
|
10
|
+
self.includes(:roles).where("roles.name LIKE '%s'", role_name.to_s).where("roles.resource_type IS NULL").where("roles.resource_id IS NULL")
|
11
11
|
elsif resource.is_a? Class
|
12
|
-
self.
|
12
|
+
self.includes(:roles).where("roles.name LIKE '%s'", role_name.to_s).where("roles.resource_type LIKE '%s'", resource.to_s).where("roles.resource_id IS NULL")
|
13
13
|
else
|
14
|
-
self.
|
14
|
+
self.includes(:roles).where("roles.name LIKE '%s'", role_name.to_s).where("roles.resource_type LIKE '%s'", resource.class.to_s).where("roles.resource_id = %s", resource.id)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -65,9 +65,9 @@ module Roles
|
|
65
65
|
|
66
66
|
def resources_with_role(resource_class, role_name = nil)
|
67
67
|
if role_name.nil?
|
68
|
-
resource_class.
|
68
|
+
resource_class.includes(:roles).where("roles.#{self.class.user_cname.underscore.singularize}_id = %s", self.id).where("roles.resource_type LIKE '%s'", resource_class.to_s)
|
69
69
|
else
|
70
|
-
resource_class.
|
70
|
+
resource_class.includes(:roles).where("roles.#{self.class.user_cname.underscore.singularize}_id = %s", self.id).where("roles.resource_type LIKE '%s'", resource_class.to_s).where("roles.name LIKE '%s'", role_name.to_s)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
data/lib/roles/version.rb
CHANGED
data/roles.gemspec
CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
s.add_development_dependency "sqlite3"
|
26
26
|
end
|
27
27
|
s.add_development_dependency "activerecord", ">= 3.1.0"
|
28
|
+
s.add_development_dependency "ammeter"
|
28
29
|
s.add_development_dependency "rake"
|
29
30
|
s.add_development_dependency "rspec", ">= 2.0"
|
30
31
|
s.add_development_dependency "rspec-rails", ">= 2.0"
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Generators are not automatically loaded by Rails
|
4
|
+
require 'generators/roles/role/role_generator'
|
5
|
+
|
6
|
+
describe Roles::Generators::RoleGenerator do
|
7
|
+
# Tell the generator where to put its output (what it thinks of as Rails.root)
|
8
|
+
destination File.expand_path("../../../../../tmp", __FILE__)
|
9
|
+
teardown :cleanup_destination_root
|
10
|
+
|
11
|
+
before {
|
12
|
+
prepare_destination
|
13
|
+
}
|
14
|
+
|
15
|
+
def cleanup_destination_root
|
16
|
+
FileUtils.rm_rf destination_root
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'no arguments' do
|
20
|
+
before(:all) { arguments [] }
|
21
|
+
|
22
|
+
before {
|
23
|
+
capture(:stdout) {
|
24
|
+
generator.create_file "app/models/user.rb" do
|
25
|
+
"class User < ActiveRecord::Base\nend"
|
26
|
+
end
|
27
|
+
}
|
28
|
+
run_generator
|
29
|
+
}
|
30
|
+
|
31
|
+
describe 'app/models/role.rb' do
|
32
|
+
subject { file('app/models/role.rb') }
|
33
|
+
it { should exist }
|
34
|
+
it { should contain "class Role < ActiveRecord::Base" }
|
35
|
+
it { should contain "belongs_to :user" }
|
36
|
+
it { should contain "belongs_to :resource, :polymorphic => true" }
|
37
|
+
it { should contain "validates_uniqueness_of :name, :scope => [:user_id, :resource_type, :resource_id]" }
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'app/models/user.rb' do
|
41
|
+
subject { file('app/models/user.rb') }
|
42
|
+
it { should contain /class User < ActiveRecord::Base\n rolify\n/ }
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'migration file' do
|
46
|
+
subject { migration_file('db/migrate/roles_create_roles.rb') }
|
47
|
+
|
48
|
+
it { should be_a_migration }
|
49
|
+
it { should contain "create_table(:roles) do" }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'specifying user and role names' do
|
54
|
+
before(:all) { arguments %w(AdminRole AdminUser) }
|
55
|
+
|
56
|
+
before {
|
57
|
+
capture(:stdout) {
|
58
|
+
generator.create_file "app/models/admin_user.rb" do
|
59
|
+
"class AdminUser < ActiveRecord::Base\nend"
|
60
|
+
end
|
61
|
+
}
|
62
|
+
run_generator
|
63
|
+
}
|
64
|
+
|
65
|
+
describe 'app/models/rank.rb' do
|
66
|
+
subject { file('app/models/admin_role.rb') }
|
67
|
+
|
68
|
+
it { should exist }
|
69
|
+
it { should contain "class AdminRole < ActiveRecord::Base" }
|
70
|
+
it { should contain "belongs_to :admin_user" }
|
71
|
+
it { should contain "belongs_to :resource, :polymorphic => true" }
|
72
|
+
it { should contain "validates_uniqueness_of :name, :scope => [:admin_user_id, :resource_type, :resource_id]" }
|
73
|
+
end
|
74
|
+
|
75
|
+
describe 'app/models/admin_user.rb' do
|
76
|
+
subject { file('app/models/admin_user.rb') }
|
77
|
+
|
78
|
+
it { should contain /class AdminUser < ActiveRecord::Base\n rolify :role_cname => 'AdminRole'\n/ }
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'migration file' do
|
82
|
+
subject { migration_file('db/migrate/roles_create_admin_roles.rb') }
|
83
|
+
|
84
|
+
it { should be_a_migration }
|
85
|
+
it { should contain "create_table(:admin_roles)" }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Roles::Resource do
|
4
|
+
before do
|
5
|
+
User.rolify :role_cname => "Role"
|
6
|
+
Forum.resourcify :role_cname => "Role"
|
7
|
+
Group.resourcify :role_cname => "Role"
|
8
|
+
reset_data
|
9
|
+
end
|
10
|
+
|
11
|
+
# Users
|
12
|
+
let(:admin) { User.first }
|
13
|
+
let(:tourist) { User.last }
|
14
|
+
|
15
|
+
describe "#users_with_role" do
|
16
|
+
before do
|
17
|
+
admin.add_role(:moderator, Forum.first)
|
18
|
+
admin.add_role(:admin, Forum.first)
|
19
|
+
admin.add_role(:admin, Forum)
|
20
|
+
tourist.add_role(:moderator, Forum.first)
|
21
|
+
end
|
22
|
+
|
23
|
+
context "on a Forum instance" do
|
24
|
+
subject { Forum.first }
|
25
|
+
it { should respond_to :users_with_role }
|
26
|
+
specify { subject.users_with_role.should == [admin, tourist] }
|
27
|
+
specify { subject.users_with_role(:moderator).should == [admin, tourist] }
|
28
|
+
specify { subject.users_with_role(:admin).should == [admin] }
|
29
|
+
specify { subject.users_with_role(:teacher).should == [] }
|
30
|
+
end
|
31
|
+
|
32
|
+
context "on Forum class" do
|
33
|
+
specify { Forum.should respond_to :users_with_role }
|
34
|
+
specify { Forum.users_with_role.should == [admin] }
|
35
|
+
specify { Forum.users_with_role(:moderator).should == [] }
|
36
|
+
specify { Forum.users_with_role(:admin).should == [admin] }
|
37
|
+
end
|
38
|
+
|
39
|
+
context "on a Group instance" do
|
40
|
+
subject { Group.last }
|
41
|
+
|
42
|
+
context "when deleting a Group instance" do
|
43
|
+
subject do
|
44
|
+
Group.create(:name => "to delete")
|
45
|
+
end
|
46
|
+
|
47
|
+
before do
|
48
|
+
subject.roles.create :name => "group_role1", :user => admin
|
49
|
+
subject.roles.create :name => "group_role2", :user => tourist
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should remove the roles binded to this instance" do
|
53
|
+
expect { subject.destroy }.to change { Role.count }.by(-2)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Roles::Role do
|
4
|
+
before do
|
5
|
+
User.rolify :role_cname => "Role"
|
6
|
+
Forum.resourcify :role_cname => "Role"
|
7
|
+
Group.resourcify :role_cname => "Role"
|
8
|
+
reset_data
|
9
|
+
|
10
|
+
@admin = User.first
|
11
|
+
@tourist = User.last
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#with_role" do
|
15
|
+
before do
|
16
|
+
@admin.add_role(:admin)
|
17
|
+
@admin.add_role(:moderator, Forum.first)
|
18
|
+
@admin.add_role(:godfather, Forum)
|
19
|
+
@tourist.add_role(:moderator, Forum.first)
|
20
|
+
end
|
21
|
+
|
22
|
+
specify { User.should respond_to :with_role }
|
23
|
+
specify { User.with_role(:admin).should == [@admin] }
|
24
|
+
specify { User.with_role(:moderator, Forum.first).should == [@admin, @tourist] }
|
25
|
+
specify { User.with_role(:moderator, Forum).should == [] }
|
26
|
+
specify { User.with_role(:godfather, Forum).should == [@admin] }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".add_role" do
|
30
|
+
it "should be able to add global role" do
|
31
|
+
@admin.add_role :moderator
|
32
|
+
@admin.has_role?(:moderator).should be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be able to add role on Forum class" do
|
36
|
+
@admin.add_role :moderator, Forum
|
37
|
+
@admin.has_role?(:moderator, Forum).should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be able to add role on a Forum instance" do
|
41
|
+
@admin.add_role :moderator, Forum.first
|
42
|
+
@admin.has_role?(:moderator, Forum.first).should be_true
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not add duplicate roles" do
|
46
|
+
@admin.add_role :moderator
|
47
|
+
@admin.add_role :moderator
|
48
|
+
@admin.role_names.should == ["moderator"]
|
49
|
+
|
50
|
+
@admin.add_role :moderator, Forum
|
51
|
+
@admin.add_role :moderator, Forum
|
52
|
+
@admin.role_names(Forum).should == ["moderator"]
|
53
|
+
|
54
|
+
@admin.add_role :moderator, Forum.first
|
55
|
+
@admin.add_role :moderator, Forum.first
|
56
|
+
@admin.role_names(Forum.first).should == ["moderator"]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should be use grant instead of add_role" do
|
60
|
+
@tourist.grant :admin
|
61
|
+
@tourist.has_role?(:admin).should be_true
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe ".remove_role" do
|
66
|
+
it "should be able to remove global role" do
|
67
|
+
@admin.add_role :moderator
|
68
|
+
@admin.has_role?(:moderator).should be_true
|
69
|
+
@admin.remove_role :moderator
|
70
|
+
@admin.has_role?(:moderator).should be_false
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should be able to remove role on Forum class" do
|
74
|
+
@admin.add_role :moderator, Forum
|
75
|
+
@admin.has_role?(:moderator, Forum).should be_true
|
76
|
+
@admin.remove_role :moderator, Forum
|
77
|
+
@admin.has_role?(:moderator, Forum).should be_false
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should be able to remove role on a Forum instance" do
|
81
|
+
@admin.add_role :moderator, Forum.first
|
82
|
+
@admin.has_role?(:moderator, Forum.first).should be_true
|
83
|
+
@admin.remove_role :moderator, Forum.first
|
84
|
+
@admin.has_role?(:moderator, Forum.first).should be_false
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should be use revoke instead of remove_role" do
|
88
|
+
@tourist.grant :admin
|
89
|
+
@tourist.has_role?(:admin).should be_true
|
90
|
+
@tourist.revoke :admin
|
91
|
+
@tourist.has_role?(:admin).should be_false
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe ".has_role?" do
|
96
|
+
it "should be able to has_role? global role" do
|
97
|
+
@admin.add_role :moderator
|
98
|
+
@admin.add_role :teacher
|
99
|
+
@admin.has_role?(:moderator).should be_true
|
100
|
+
@admin.has_role?(:teacher).should be_true
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should be able to has_role? on Forum class" do
|
104
|
+
@admin.add_role :moderator, Forum
|
105
|
+
@admin.add_role :teacher, Forum
|
106
|
+
@admin.has_role?(:moderator, Forum).should be_true
|
107
|
+
@admin.has_role?(:teacher, Forum).should be_true
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should be able to has_role? on a Forum instance" do
|
111
|
+
@admin.add_role :moderator, Forum.first
|
112
|
+
@admin.add_role :teacher, Forum.first
|
113
|
+
@admin.has_role?(:moderator, Forum.first).should be_true
|
114
|
+
@admin.has_role?(:teacher, Forum.first).should be_true
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should be use is_xxx instead of has_role?" do
|
118
|
+
@tourist.grant :admin
|
119
|
+
@tourist.has_role?(:admin).should be_true
|
120
|
+
@tourist.is_admin?.should be_true
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe ".role_names" do
|
125
|
+
it "should be able to list global role names" do
|
126
|
+
@admin.add_role :moderator
|
127
|
+
@admin.add_role :teacher
|
128
|
+
@admin.role_names.should == ["moderator", "teacher"]
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should be able to list role names on Forum class" do
|
132
|
+
@admin.add_role :moderator, Forum
|
133
|
+
@admin.add_role :teacher, Forum
|
134
|
+
@admin.role_names(Forum).should == ["moderator", "teacher"]
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should be able to list role names on a Forum instance" do
|
138
|
+
@admin.add_role :moderator, Forum.first
|
139
|
+
@admin.add_role :teacher, Forum.first
|
140
|
+
@admin.role_names(Forum.first).should == ["moderator", "teacher"]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe ".resources_with_role" do
|
145
|
+
before do
|
146
|
+
@admin.add_role(:moderator, Forum.first)
|
147
|
+
@admin.add_role(:moderator, Forum.last)
|
148
|
+
@admin.add_role(:teacher, Forum.last)
|
149
|
+
@tourist.add_role(:moderator, Forum.first)
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should be able to find all resources of which user has any role" do
|
153
|
+
@admin.resources_with_role(Forum).should == [Forum.first, Forum.last]
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should be able to find all resources of which user has specific role" do
|
157
|
+
@admin.resources_with_role(Forum, :moderator).should == [Forum.first, Forum.last]
|
158
|
+
@admin.resources_with_role(Forum, :teacher).should == [Forum.last]
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "roles get destroyed when user destroyed" do
|
163
|
+
before do
|
164
|
+
@admin.roles.create :name => "teacher"
|
165
|
+
@admin.roles.create :name => "moderator", :resource_type => "Forum"
|
166
|
+
@admin.roles.create :name => "admin", :resource => Forum.first
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should remove the roles binded to this instance" do
|
170
|
+
expect { @admin.destroy }.to change { Role.count }.by(-3)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
require 'roles'
|
5
|
+
require 'ammeter/init'
|
6
|
+
|
7
|
+
load File.dirname(__FILE__) + "/support/active_record.rb"
|
8
|
+
|
9
|
+
def reset_data
|
10
|
+
User.destroy_all
|
11
|
+
Role.destroy_all
|
12
|
+
Forum.destroy_all
|
13
|
+
Group.destroy_all
|
14
|
+
Privilege.destroy_all
|
15
|
+
Customer.destroy_all
|
16
|
+
|
17
|
+
# Users
|
18
|
+
User.create(:login => "admin")
|
19
|
+
User.create(:login => "moderator")
|
20
|
+
User.create(:login => "god")
|
21
|
+
User.create(:login => "zombie")
|
22
|
+
|
23
|
+
Customer.create(:login => "admin")
|
24
|
+
Customer.create(:login => "moderator")
|
25
|
+
Customer.create(:login => "god")
|
26
|
+
Customer.create(:login => "zombie")
|
27
|
+
|
28
|
+
# Resources
|
29
|
+
Forum.create(:name => "forum 1")
|
30
|
+
Forum.create(:name => "forum 2")
|
31
|
+
Forum.create(:name => "forum 3")
|
32
|
+
|
33
|
+
Group.create(:name => "group 1")
|
34
|
+
Group.create(:name => "group 2")
|
35
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
RSpec::Matchers::OperatorMatcher.register(ActiveRecord::Relation, '=~', RSpec::Matchers::BuiltIn::MatchArray)
|
4
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
5
|
+
ActiveRecord::Base.extend Roles
|
6
|
+
|
7
|
+
load File.dirname(__FILE__) + '/schema.rb'
|
8
|
+
|
9
|
+
# ActiveRecord models
|
10
|
+
class User < ActiveRecord::Base
|
11
|
+
end
|
12
|
+
|
13
|
+
class Role < ActiveRecord::Base
|
14
|
+
belongs_to :user
|
15
|
+
belongs_to :resource, :polymorphic => true
|
16
|
+
end
|
17
|
+
|
18
|
+
class Forum < ActiveRecord::Base
|
19
|
+
#resourcify done during specs setup to be able to use custom user classes
|
20
|
+
end
|
21
|
+
|
22
|
+
class Group < ActiveRecord::Base
|
23
|
+
#resourcify done during specs setup to be able to use custom user classes
|
24
|
+
end
|
25
|
+
|
26
|
+
class Customer < ActiveRecord::Base
|
27
|
+
end
|
28
|
+
|
29
|
+
class Privilege < ActiveRecord::Base
|
30
|
+
belongs_to :customer
|
31
|
+
belongs_to :resource, :polymorphic => true
|
32
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
self.verbose = false
|
3
|
+
|
4
|
+
create_table(:roles) do |t|
|
5
|
+
t.string :name
|
6
|
+
t.references :resource, :polymorphic => true
|
7
|
+
t.references :user
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table(:users) do |t|
|
13
|
+
t.string :login
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table(:forums) do |t|
|
17
|
+
t.string :name
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table(:groups) do |t|
|
21
|
+
t.string :name
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table(:privileges) do |t|
|
25
|
+
t.string :name
|
26
|
+
t.references :resource, :polymorphic => true
|
27
|
+
t.references :customer
|
28
|
+
end
|
29
|
+
|
30
|
+
create_table(:customers) do |t|
|
31
|
+
t.string :login
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sqlite3
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: 3.1.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: ammeter
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
- !ruby/object:Gem::Dependency
|
47
63
|
name: rake
|
48
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,7 +132,7 @@ extra_rdoc_files: []
|
|
116
132
|
files:
|
117
133
|
- .gitignore
|
118
134
|
- .travis.yml
|
119
|
-
- CHANGELOG.
|
135
|
+
- CHANGELOG.md
|
120
136
|
- Gemfile
|
121
137
|
- LICENSE
|
122
138
|
- README.md
|
@@ -133,6 +149,12 @@ files:
|
|
133
149
|
- lib/roles/role.rb
|
134
150
|
- lib/roles/version.rb
|
135
151
|
- roles.gemspec
|
152
|
+
- spec/generators/roles/role/role_generator_spec.rb
|
153
|
+
- spec/roles/resource_spec.rb
|
154
|
+
- spec/roles/role_spec.rb
|
155
|
+
- spec/spec_helper.rb
|
156
|
+
- spec/support/active_record.rb
|
157
|
+
- spec/support/schema.rb
|
136
158
|
homepage: http://github.com/liufengyun/roles
|
137
159
|
licenses: []
|
138
160
|
post_install_message:
|
@@ -157,4 +179,10 @@ rubygems_version: 1.8.24
|
|
157
179
|
signing_key:
|
158
180
|
specification_version: 3
|
159
181
|
summary: Roles library with resource scoping
|
160
|
-
test_files:
|
182
|
+
test_files:
|
183
|
+
- spec/generators/roles/role/role_generator_spec.rb
|
184
|
+
- spec/roles/resource_spec.rb
|
185
|
+
- spec/roles/role_spec.rb
|
186
|
+
- spec/spec_helper.rb
|
187
|
+
- spec/support/active_record.rb
|
188
|
+
- spec/support/schema.rb
|
data/CHANGELOG.rdoc
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
= v0.0.5 (Aug 23, 2012)
|
2
|
-
* fix user_cname nil problem in User class
|
3
|
-
|
4
|
-
= v0.0.4 (Aug 23, 2012)
|
5
|
-
* fix method name error(roles -> rolify)
|
6
|
-
|
7
|
-
= v0.0.3 (Aug 23, 2012)
|
8
|
-
* fix generator error
|
9
|
-
|
10
|
-
= v0.0.2 (Aug 19, 2012)
|
11
|
-
* rename `user.resources` to `user.resources_with_role`
|
12
|
-
|
13
|
-
= v0.0.1 (Aug 17, 2012)
|
14
|
-
* first release
|