third-prestige-rolify 3.3.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +17 -0
  4. data/CHANGELOG.rdoc +157 -0
  5. data/Gemfile +21 -0
  6. data/LICENSE +20 -0
  7. data/README.md +220 -0
  8. data/Rakefile +34 -0
  9. data/UPGRADE.rdoc +44 -0
  10. data/gemfiles/Gemfile.rails-3.2 +21 -0
  11. data/gemfiles/Gemfile.rails-4.0 +27 -0
  12. data/lib/generators/active_record/rolify_generator.rb +50 -0
  13. data/lib/generators/active_record/templates/README +8 -0
  14. data/lib/generators/active_record/templates/migration.rb +19 -0
  15. data/lib/generators/mongoid/rolify_generator.rb +51 -0
  16. data/lib/generators/mongoid/templates/README-mongoid +4 -0
  17. data/lib/generators/rolify/rolify_generator.rb +35 -0
  18. data/lib/generators/rolify/templates/README +13 -0
  19. data/lib/generators/rolify/templates/initializer.rb +8 -0
  20. data/lib/generators/rolify/templates/role-active_record.rb +11 -0
  21. data/lib/generators/rolify/templates/role-mongoid.rb +17 -0
  22. data/lib/generators/rolify/user_generator.rb +39 -0
  23. data/lib/rolify.rb +57 -0
  24. data/lib/rolify/adapters/active_record/resource_adapter.rb +26 -0
  25. data/lib/rolify/adapters/active_record/role_adapter.rb +86 -0
  26. data/lib/rolify/adapters/active_record/scopes.rb +27 -0
  27. data/lib/rolify/adapters/base.rb +60 -0
  28. data/lib/rolify/adapters/mongoid/resource_adapter.rb +27 -0
  29. data/lib/rolify/adapters/mongoid/role_adapter.rb +89 -0
  30. data/lib/rolify/adapters/mongoid/scopes.rb +27 -0
  31. data/lib/rolify/configure.rb +56 -0
  32. data/lib/rolify/dynamic.rb +21 -0
  33. data/lib/rolify/finders.rb +40 -0
  34. data/lib/rolify/matchers.rb +13 -0
  35. data/lib/rolify/railtie.rb +20 -0
  36. data/lib/rolify/resource.rb +31 -0
  37. data/lib/rolify/role.rb +85 -0
  38. data/lib/rolify/utils.rb +10 -0
  39. data/lib/rolify/version.rb +3 -0
  40. data/rolify.gemspec +30 -0
  41. data/spec/README.rdoc +24 -0
  42. data/spec/generators/rolify/rolify_activerecord_generator_spec.rb +163 -0
  43. data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
  44. data/spec/generators_helper.rb +21 -0
  45. data/spec/rolify/config_spec.rb +191 -0
  46. data/spec/rolify/custom_spec.rb +20 -0
  47. data/spec/rolify/matchers_spec.rb +24 -0
  48. data/spec/rolify/namespace_spec.rb +24 -0
  49. data/spec/rolify/resource_spec.rb +389 -0
  50. data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
  51. data/spec/rolify/role_spec.rb +20 -0
  52. data/spec/rolify/shared_contexts.rb +92 -0
  53. data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +92 -0
  54. data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +65 -0
  55. data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +151 -0
  56. data/spec/rolify/shared_examples/shared_examples_for_finders.rb +77 -0
  57. data/spec/rolify/shared_examples/shared_examples_for_has_all_roles.rb +71 -0
  58. data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +71 -0
  59. data/spec/rolify/shared_examples/shared_examples_for_has_role.rb +135 -0
  60. data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +174 -0
  61. data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +121 -0
  62. data/spec/rolify/shared_examples/shared_examples_for_roles.rb +102 -0
  63. data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +38 -0
  64. data/spec/spec_helper.rb +30 -0
  65. data/spec/support/adapters/active_record.rb +76 -0
  66. data/spec/support/adapters/mongoid.rb +143 -0
  67. data/spec/support/adapters/mongoid.yml +6 -0
  68. data/spec/support/data.rb +25 -0
  69. data/spec/support/schema.rb +52 -0
  70. metadata +254 -0
@@ -0,0 +1,121 @@
1
+ shared_examples_for "#remove_role_examples" do |param_name, param_method|
2
+ context "using #{param_name} as parameter" do
3
+ context "removing a global role", :scope => :global do
4
+ context "being a global role of the user" do
5
+ it { expect { subject.remove_role("admin".send(param_method)) }.to change { subject.roles.size }.by(-1) }
6
+
7
+ it { should_not have_role("admin".send(param_method)) }
8
+ it { should have_role("staff".send(param_method)) }
9
+ it { should have_role("manager".send(param_method), Group) }
10
+ it { should have_role("moderator".send(param_method), Forum.last) }
11
+ it { should have_role("moderator".send(param_method), Group.last) }
12
+ it { should have_role("anonymous".send(param_method), Forum.first) }
13
+ end
14
+
15
+ context "being a class scoped role to the user" do
16
+ it { expect { subject.remove_role("manager".send(param_method)) }.to change { subject.roles.size }.by(-1) }
17
+
18
+ it { should_not have_role("admin".send(param_method)) }
19
+ it { should have_role("staff".send(param_method)) }
20
+ it { should_not have_role("manager".send(param_method), Group) }
21
+ it { should have_role("moderator".send(param_method), Forum.last) }
22
+ it { should have_role("moderator".send(param_method), Group.last) }
23
+ it { should have_role("anonymous".send(param_method), Forum.first) }
24
+ end
25
+
26
+ context "being instance scoped roles to the user" do
27
+ it { expect { subject.remove_role("moderator".send(param_method)) }.to change { subject.roles.size }.by(-2) }
28
+
29
+ it { should_not have_role("admin".send(param_method)) }
30
+ it { should have_role("staff".send(param_method)) }
31
+ it { should_not have_role("manager".send(param_method), Group) }
32
+ it { should_not have_role("moderator".send(param_method), Forum.last) }
33
+ it { should_not have_role("moderator".send(param_method), Group.last) }
34
+ it { should have_role("anonymous".send(param_method), Forum.first) }
35
+ end
36
+
37
+ context "not being a role of the user" do
38
+ it { expect { subject.remove_role("superhero".send(param_method)) }.not_to change { subject.roles.size } }
39
+ end
40
+
41
+ context "used by another user" do
42
+ before do
43
+ user = user_class.last
44
+ user.add_role "staff".send(param_method)
45
+ end
46
+
47
+ it { expect { subject.remove_role("staff".send(param_method)) }.not_to change { role_class.count } }
48
+
49
+ it { should_not have_role("admin".send(param_method)) }
50
+ it { should_not have_role("staff".send(param_method)) }
51
+ it { should_not have_role("manager".send(param_method), Group) }
52
+ it { should_not have_role("moderator".send(param_method), Forum.last) }
53
+ it { should_not have_role("moderator".send(param_method), Group.last) }
54
+ it { should have_role("anonymous".send(param_method), Forum.first) }
55
+ end
56
+
57
+ context "not used by anyone else" do
58
+ before do
59
+ subject.add_role "nobody".send(param_method)
60
+ end
61
+
62
+ it { expect { subject.remove_role("nobody".send(param_method)) }.to change { role_class.count }.by(-1) }
63
+ end
64
+ end
65
+
66
+ context "removing a class scoped role", :scope => :class do
67
+ context "being a global role of the user" do
68
+ it { expect { subject.remove_role("warrior".send(param_method), Forum) }.not_to change{ subject.roles.size } }
69
+ end
70
+
71
+ context "being a class scoped role to the user" do
72
+ it { expect { subject.remove_role("manager".send(param_method), Forum) }.to change{ subject.roles.size }.by(-1) }
73
+
74
+ it { should have_role("warrior") }
75
+ it { should_not have_role("manager", Forum) }
76
+ it { should have_role("player", Forum) }
77
+ it { should have_role("moderator".send(param_method), Forum.last) }
78
+ it { should have_role("moderator".send(param_method), Group.last) }
79
+ it { should have_role("anonymous".send(param_method), Forum.first) }
80
+ end
81
+
82
+ context "being instance scoped role to the user" do
83
+ it { expect { subject.remove_role("moderator".send(param_method), Forum) }.to change { subject.roles.size }.by(-1) }
84
+
85
+ it { should have_role("warrior") }
86
+ it { should_not have_role("manager", Forum) }
87
+ it { should have_role("player", Forum) }
88
+ it { should_not have_role("moderator".send(param_method), Forum.last) }
89
+ it { should have_role("moderator".send(param_method), Group.last) }
90
+ it { should have_role("anonymous".send(param_method), Forum.first) }
91
+ end
92
+
93
+ context "not being a role of the user" do
94
+ it { expect { subject.remove_role("manager".send(param_method), Group) }.not_to change { subject.roles.size } }
95
+ end
96
+ end
97
+
98
+ context "removing a instance scoped role", :scope => :instance do
99
+ context "being a global role of the user" do
100
+ it { expect { subject.remove_role("soldier".send(param_method), Group.first) }.not_to change { subject.roles.size } }
101
+ end
102
+
103
+ context "being a class scoped role to the user" do
104
+ it { expect { subject.remove_role("visitor".send(param_method), Forum.first) }.not_to change { subject.roles.size } }
105
+ end
106
+
107
+ context "being instance scoped role to the user" do
108
+ it { expect { subject.remove_role("moderator".send(param_method), Forum.first) }.to change { subject.roles.size }.by(-1) }
109
+
110
+ it { should have_role("soldier") }
111
+ it { should have_role("visitor", Forum) }
112
+ it { should_not have_role("moderator", Forum.first) }
113
+ it { should have_role("anonymous", Forum.last) }
114
+ end
115
+
116
+ context "not being a role of the user" do
117
+ it { expect { subject.remove_role("anonymous".send(param_method), Forum.first) }.not_to change { subject.roles.size } }
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,102 @@
1
+ require "rolify/shared_contexts"
2
+ require "rolify/shared_examples/shared_examples_for_add_role"
3
+ require "rolify/shared_examples/shared_examples_for_has_role"
4
+ require "rolify/shared_examples/shared_examples_for_only_has_role"
5
+ require "rolify/shared_examples/shared_examples_for_has_all_roles"
6
+ require "rolify/shared_examples/shared_examples_for_has_any_role"
7
+ require "rolify/shared_examples/shared_examples_for_remove_role"
8
+ require "rolify/shared_examples/shared_examples_for_finders"
9
+
10
+ shared_examples_for Rolify::Role do
11
+ before(:all) do
12
+ reset_defaults
13
+ Rolify.dynamic_shortcuts = false
14
+ rolify_options = { :role_cname => role_class.to_s }
15
+ rolify_options[:role_join_table_name] = join_table if defined? join_table
16
+ user_class.rolify rolify_options
17
+ role_class.destroy_all
18
+ Forum.resourcify :roles, :role_cname => role_class.to_s
19
+ Group.resourcify :roles, :role_cname => role_class.to_s
20
+ end
21
+
22
+ context "in the Instance level" do
23
+ before(:all) do
24
+ admin = user_class.first
25
+ admin.add_role :admin
26
+ admin.add_role :moderator, Forum.first
27
+ end
28
+
29
+ subject { user_class.first }
30
+
31
+ [ :has_role, :grant, :add_role ].each do |method_alias|
32
+ it { should respond_to(method_alias.to_sym).with(1).arguments }
33
+ it { should respond_to(method_alias.to_sym).with(2).arguments }
34
+ end
35
+
36
+ it { should respond_to(:has_role?).with(1).arguments }
37
+ it { should respond_to(:has_role?).with(2).arguments }
38
+
39
+ it { should respond_to(:has_all_roles?) }
40
+ it { should respond_to(:has_all_roles?) }
41
+
42
+ it { should respond_to(:has_any_role?) }
43
+ it { should respond_to(:has_any_role?) }
44
+
45
+ [ :has_no_role, :revoke, :remove_role ].each do |method_alias|
46
+ it { should respond_to(method_alias.to_sym).with(1).arguments }
47
+ it { should respond_to(method_alias.to_sym).with(2).arguments }
48
+ end
49
+
50
+ it { should_not respond_to(:is_admin?) }
51
+ it { should_not respond_to(:is_moderator_of?) }
52
+
53
+ describe "#has_role" do
54
+ it_should_behave_like "#add_role_examples", "String", :to_s
55
+ it_should_behave_like "#add_role_examples", "Symbol", :to_sym
56
+ end
57
+
58
+ describe "#has_role?" do
59
+ it_should_behave_like "#has_role?_examples", "String", :to_s
60
+ it_should_behave_like "#has_role?_examples", "Symbol", :to_sym
61
+ end
62
+
63
+ describe "#only_has_role?" do
64
+ it_should_behave_like "#only_has_role?_examples", "String", :to_s
65
+ it_should_behave_like "#only_has_role?_examples", "Symbol", :to_sym
66
+ end
67
+
68
+ describe "#has_all_roles?" do
69
+ it_should_behave_like "#has_all_roles?_examples", "String", :to_s
70
+ it_should_behave_like "#has_all_roles?_examples", "Symbol", :to_sym
71
+ end
72
+
73
+ describe "#has_any_role?" do
74
+ it_should_behave_like "#has_any_role?_examples", "String", :to_s
75
+ it_should_behave_like "#has_any_role?_examples", "Symbol", :to_sym
76
+ end
77
+
78
+ describe "#has_no_role" do
79
+ it_should_behave_like "#remove_role_examples", "String", :to_s
80
+ it_should_behave_like "#remove_role_examples", "Symbol", :to_sym
81
+ end
82
+ end
83
+
84
+ context "with a new instance" do
85
+ let(:user) { user_class.new }
86
+
87
+ before do
88
+ user.add_role :admin
89
+ user.add_role :moderator, Forum.first
90
+ end
91
+
92
+ subject { user }
93
+
94
+ it { should have_role :admin }
95
+ it { should have_role :moderator, Forum.first }
96
+ end
97
+
98
+ context "on the Class level ", :scope => :mixed do
99
+ it_should_behave_like :finders, "String", :to_s
100
+ it_should_behave_like :finders, "Symbol", :to_sym
101
+ end
102
+ end
@@ -0,0 +1,38 @@
1
+ require "rolify/shared_contexts"
2
+
3
+ shared_examples_for "Role.scopes" do
4
+ before do
5
+ role_class.destroy_all
6
+ end
7
+
8
+ subject { user_class.first }
9
+
10
+ describe ".global" do
11
+ let!(:admin_role) { subject.add_role :admin }
12
+ let!(:staff_role) { subject.add_role :staff }
13
+
14
+ it { subject.roles.global.should == [ admin_role, staff_role ] }
15
+ end
16
+
17
+ describe ".class_scoped" do
18
+ let!(:manager_role) { subject.add_role :manager, Group }
19
+ let!(:moderator_role) { subject.add_role :moderator, Forum }
20
+
21
+ it { subject.roles.class_scoped.should =~ [ manager_role, moderator_role ] }
22
+ it { subject.roles.class_scoped(Group).should =~ [ manager_role ] }
23
+ it { subject.roles.class_scoped(Forum).should =~ [ moderator_role ] }
24
+ end
25
+
26
+ describe ".instance_scoped" do
27
+ let!(:visitor_role) { subject.add_role :visitor, Forum.first }
28
+ let!(:zombie_role) { subject.add_role :visitor, Forum.last }
29
+ let!(:anonymous_role) { subject.add_role :anonymous, Group.last }
30
+
31
+ it { subject.roles.instance_scoped.to_a.entries.should =~ [ visitor_role, zombie_role, anonymous_role ] }
32
+ it { subject.roles.instance_scoped(Forum).should =~ [ visitor_role, zombie_role ] }
33
+ it { subject.roles.instance_scoped(Forum.first).should =~ [ visitor_role ] }
34
+ it { subject.roles.instance_scoped(Forum.last).should =~ [ zombie_role ] }
35
+ it { subject.roles.instance_scoped(Group.last).should =~ [ anonymous_role ] }
36
+ it { subject.roles.instance_scoped(Group.first).should be_empty }
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require "bundler/setup"
3
+
4
+ require 'rolify'
5
+ require 'rolify/matchers'
6
+ require 'rails/all'
7
+
8
+ require 'coveralls'
9
+ Coveralls.wear_merged!
10
+
11
+ ENV['ADAPTER'] ||= 'active_record'
12
+
13
+ load File.dirname(__FILE__) + "/support/adapters/#{ENV['ADAPTER']}.rb"
14
+ load File.dirname(__FILE__) + '/support/data.rb'
15
+
16
+ def reset_defaults
17
+ Rolify.use_defaults
18
+ Rolify.use_mongoid if ENV['ADAPTER'] == "mongoid"
19
+ end
20
+
21
+ def provision_user(user, roles)
22
+ roles.each do |role|
23
+ if role.is_a? Array
24
+ user.add_role *role
25
+ else
26
+ user.add_role role
27
+ end
28
+ end
29
+ user
30
+ end
@@ -0,0 +1,76 @@
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 Rolify
6
+
7
+ load File.dirname(__FILE__) + '/../schema.rb'
8
+
9
+ # Standard user and role classes
10
+ class User < ActiveRecord::Base
11
+ rolify
12
+ end
13
+
14
+ class Role < ActiveRecord::Base
15
+ has_and_belongs_to_many :users, :join_table => :users_roles
16
+ belongs_to :resource, :polymorphic => true
17
+
18
+ extend Rolify::Adapter::Scopes
19
+ end
20
+
21
+ # Resourcifed and rolifed at the same time
22
+ class HumanResource < ActiveRecord::Base
23
+ resourcify :resources
24
+ rolify
25
+ end
26
+
27
+ # Custom role and class names
28
+ class Customer < ActiveRecord::Base
29
+ rolify :role_cname => "Privilege"
30
+ end
31
+
32
+ class Privilege < ActiveRecord::Base
33
+ has_and_belongs_to_many :customers, :join_table => :customers_privileges
34
+ belongs_to :resource, :polymorphic => true
35
+
36
+ extend Rolify::Adapter::Scopes
37
+ end
38
+
39
+ # Namespaced models
40
+ module Admin
41
+ def self.table_name_prefix
42
+ 'admin_'
43
+ end
44
+
45
+ class Moderator < ActiveRecord::Base
46
+ rolify :role_cname => "Admin::Right", :role_join_table_name => "moderators_rights"
47
+ end
48
+
49
+ class Right < ActiveRecord::Base
50
+ has_and_belongs_to_many :moderators, :class_name => "Admin::Moderator", :join_table => "moderators_rights"
51
+ belongs_to :resource, :polymorphic => true
52
+
53
+ extend Rolify::Adapter::Scopes
54
+ end
55
+ end
56
+
57
+
58
+ # Resources classes
59
+ class Forum < ActiveRecord::Base
60
+ #resourcify done during specs setup to be able to use custom user classes
61
+ end
62
+
63
+ class Group < ActiveRecord::Base
64
+ #resourcify done during specs setup to be able to use custom user classes
65
+
66
+ def subgroups
67
+ Group.where(:parent_id => id)
68
+ end
69
+ end
70
+
71
+ class Team < ActiveRecord::Base
72
+ #resourcify done during specs setup to be able to use custom user classes
73
+ self.primary_key = "team_code"
74
+
75
+ default_scope { order(:team_code) }
76
+ end
@@ -0,0 +1,143 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.load!("spec/support/adapters/mongoid.yml", :test)
4
+
5
+ ::Mongoid::Document.module_eval do
6
+ def self.included(base)
7
+ base.extend Rolify
8
+ end
9
+ end
10
+
11
+ Rolify.use_mongoid
12
+
13
+ # Standard user and role classes
14
+ class User
15
+ include Mongoid::Document
16
+ rolify
17
+
18
+ field :login, :type => String
19
+ end
20
+
21
+ class Role
22
+ include Mongoid::Document
23
+ has_and_belongs_to_many :users
24
+ belongs_to :resource, :polymorphic => true
25
+
26
+ field :name, :type => String
27
+ index(
28
+ {
29
+ :name => 1,
30
+ :resource_type => 1,
31
+ :resource_id => 1
32
+ },
33
+ { :unique => true }
34
+ )
35
+
36
+ scopify
37
+ end
38
+
39
+ # Resourcifed and rolifed at the same time
40
+ class HumanResource
41
+ include Mongoid::Document
42
+ resourcify :resources
43
+ rolify
44
+
45
+ field :login, :type => String
46
+ end
47
+
48
+ #class Power
49
+ # include Mongoid::Document
50
+ # has_and_belongs_to_many :human_resources
51
+ # belongs_to :resource, :polymorphic => true
52
+ # scopify
53
+ #
54
+ # field :name, :type => String
55
+ # index(
56
+ # {
57
+ # :name => 1,
58
+ # :resource_type => 1,
59
+ # :resource_id => 1
60
+ # },
61
+ # { :unique => true }
62
+ # )
63
+ #end
64
+
65
+ # Custom role and class names
66
+ class Customer
67
+ include Mongoid::Document
68
+ rolify :role_cname => "Privilege"
69
+
70
+ field :login, :type => String
71
+ end
72
+
73
+ class Privilege
74
+ include Mongoid::Document
75
+ has_and_belongs_to_many :customers
76
+ belongs_to :resource, :polymorphic => true
77
+ scopify
78
+
79
+ field :name, :type => String
80
+ index(
81
+ {
82
+ :name => 1,
83
+ :resource_type => 1,
84
+ :resource_id => 1
85
+ },
86
+ { :unique => true }
87
+ )
88
+ end
89
+
90
+ # Namespaced models
91
+ module Admin
92
+ class Moderator
93
+ include Mongoid::Document
94
+ rolify :role_cname => "Admin::Right"
95
+
96
+ field :login, :type => String
97
+ end
98
+
99
+ class Right
100
+ include Mongoid::Document
101
+ has_and_belongs_to_many :moderators, :class_name => 'Admin::Moderator'
102
+ belongs_to :resource, :polymorphic => true
103
+ scopify
104
+
105
+ field :name, :type => String
106
+ index(
107
+ {
108
+ :name => 1,
109
+ :resource_type => 1,
110
+ :resource_id => 1
111
+ },
112
+ { :unique => true }
113
+ )
114
+ end
115
+ end
116
+
117
+ # Resources classes
118
+ class Forum
119
+ include Mongoid::Document
120
+ #resourcify done during specs setup to be able to use custom user classes
121
+
122
+ field :name, :type => String
123
+ end
124
+
125
+ class Group
126
+ include Mongoid::Document
127
+ #resourcify done during specs setup to be able to use custom user classes
128
+
129
+ field :name, :type => String
130
+ field :parent_id, :type => Integer
131
+
132
+ def subgroups
133
+ Group.in(:parent_id => _id)
134
+ end
135
+ end
136
+
137
+ class Team
138
+ include Mongoid::Document
139
+ #resourcify done during specs setup to be able to use custom user classes
140
+
141
+ field :team_code, :type => Integer
142
+ field :name, :type => String
143
+ end