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,24 @@
1
+ require "spec_helper"
2
+
3
+ describe "Resourcify and rolify on the same model" do
4
+
5
+ before(:all) do
6
+ reset_defaults
7
+ Role.delete_all
8
+ HumanResource.delete_all
9
+ end
10
+
11
+ let!(:user) do
12
+ user = HumanResource.new login: 'Samer'
13
+ user.save
14
+ user
15
+ end
16
+
17
+ it "should add the role to the user" do
18
+ expect { user.add_role :admin }.to change { user.roles.count }.by(1)
19
+ end
20
+
21
+ it "should create a role to the roles collection" do
22
+ expect { user.add_role :moderator }.to change { Role.count }.by(1)
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+ require "rolify/shared_examples/shared_examples_for_roles"
3
+ require "rolify/shared_examples/shared_examples_for_dynamic"
4
+ require "rolify/shared_examples/shared_examples_for_scopes"
5
+ require "rolify/shared_examples/shared_examples_for_callbacks"
6
+
7
+ describe Rolify do
8
+ def user_class
9
+ User
10
+ end
11
+
12
+ def role_class
13
+ Role
14
+ end
15
+
16
+ it_behaves_like Rolify::Role
17
+ it_behaves_like "Role.scopes"
18
+ it_behaves_like Rolify::Dynamic
19
+ it_behaves_like "Rolify.callbacks"
20
+ end
@@ -0,0 +1,92 @@
1
+ shared_context "global role", :scope => :global do
2
+ subject { admin }
3
+
4
+ def admin
5
+ user_class.first
6
+ end
7
+
8
+ before(:all) do
9
+ load_roles
10
+ create_other_roles
11
+ end
12
+
13
+ def load_roles
14
+ role_class.destroy_all
15
+ admin.roles = []
16
+ admin.add_role :admin
17
+ admin.add_role :staff
18
+ admin.add_role :manager, Group
19
+ admin.add_role :player, Forum
20
+ admin.add_role :moderator, Forum.last
21
+ admin.add_role :moderator, Group.last
22
+ admin.add_role :anonymous, Forum.first
23
+ end
24
+ end
25
+
26
+ shared_context "class scoped role", :scope => :class do
27
+ subject { manager }
28
+
29
+ before(:all) do
30
+ load_roles
31
+ create_other_roles
32
+ end
33
+
34
+ def manager
35
+ user_class.where(:login => "moderator").first
36
+ end
37
+
38
+ def load_roles
39
+ role_class.destroy_all
40
+ manager.roles = []
41
+ manager.add_role :manager, Forum
42
+ manager.add_role :player, Forum
43
+ manager.add_role :warrior
44
+ manager.add_role :moderator, Forum.last
45
+ manager.add_role :moderator, Group.last
46
+ manager.add_role :anonymous, Forum.first
47
+ end
48
+ end
49
+
50
+ shared_context "instance scoped role", :scope => :instance do
51
+ subject { moderator }
52
+
53
+ before(:all) do
54
+ load_roles
55
+ create_other_roles
56
+ end
57
+
58
+ def moderator
59
+ user_class.where(:login => "god").first
60
+ end
61
+
62
+ def load_roles
63
+ role_class.destroy_all
64
+ moderator.roles = []
65
+ moderator.add_role :moderator, Forum.first
66
+ moderator.add_role :anonymous, Forum.last
67
+ moderator.add_role :visitor, Forum
68
+ moderator.add_role :soldier
69
+ end
70
+ end
71
+
72
+ shared_context "mixed scoped roles", :scope => :mixed do
73
+ subject { user_class }
74
+
75
+ before(:all) do
76
+ role_class.destroy_all
77
+ end
78
+
79
+ let!(:root) { provision_user(user_class.first, [ :admin, :staff, [ :moderator, Group ], [ :visitor, Forum.last ] ]) }
80
+ let!(:modo) { provision_user(user_class.where(:login => "moderator").first, [[ :moderator, Forum ], [ :manager, Group ], [ :visitor, Group.first ]])}
81
+ let!(:visitor) { provision_user(user_class.last, [[ :visitor, Forum.last ]]) }
82
+ end
83
+
84
+ def create_other_roles
85
+ role_class.create :name => "superhero"
86
+ role_class.create :name => "admin", :resource_type => "Group"
87
+ role_class.create :name => "admin", :resource => Forum.first
88
+ role_class.create :name => "VIP", :resource_type => "Forum"
89
+ role_class.create :name => "manager", :resource => Forum.last
90
+ role_class.create :name => "roomate", :resource => Forum.first
91
+ role_class.create :name => "moderator", :resource => Group.first
92
+ end
@@ -0,0 +1,92 @@
1
+ shared_examples_for "#add_role_examples" do |param_name, param_method|
2
+ context "using #{param_name} as parameter" do
3
+ context "with a global role", :scope => :global do
4
+ it "should add the role to the user" do
5
+ expect { subject.add_role "root".send(param_method) }.to change { subject.roles.count }.by(1)
6
+ end
7
+
8
+ it "should create a role to the roles table" do
9
+ expect { subject.add_role "moderator".send(param_method) }.to change { role_class.count }.by(1)
10
+ end
11
+
12
+ context "considering a new global role" do
13
+ subject { role_class.last }
14
+
15
+ its(:name) { should eq("moderator") }
16
+ its(:resource_type) { should be(nil) }
17
+ its(:resource_id) { should be(nil) }
18
+ end
19
+
20
+ context "should not create another role" do
21
+ it "if the role was already assigned to the user" do
22
+ subject.add_role "manager".send(param_method)
23
+ expect { subject.add_role "manager".send(param_method) }.not_to change { subject.roles.size }
24
+ end
25
+
26
+ it "if the role already exists in the db" do
27
+ role_class.create :name => "god"
28
+ expect { subject.add_role "god".send(param_method) }.not_to change { role_class.count }
29
+ end
30
+ end
31
+ end
32
+
33
+ context "with a class scoped role", :scope => :class do
34
+ it "should add the role to the user" do
35
+ expect { subject.add_role "supervisor".send(param_method), Forum }.to change { subject.roles.count }.by(1)
36
+ end
37
+
38
+ it "should create a role in the roles table" do
39
+ expect { subject.add_role "moderator".send(param_method), Forum }.to change { role_class.count }.by(1)
40
+ end
41
+
42
+ context "considering a new class scoped role" do
43
+ subject { role_class.last }
44
+
45
+ its(:name) { should eq("moderator") }
46
+ its(:resource_type) { should eq(Forum.to_s) }
47
+ its(:resource_id) { should be(nil) }
48
+ end
49
+
50
+ context "should not create another role" do
51
+ it "if the role was already assigned to the user" do
52
+ subject.add_role "warrior".send(param_method), Forum
53
+ expect { subject.add_role "warrior".send(param_method), Forum }.not_to change { subject.roles.count }
54
+ end
55
+
56
+ it "if already existing in the database" do
57
+ role_class.create :name => "hacker", :resource_type => "Forum"
58
+ expect { subject.add_role "hacker".send(param_method), Forum }.not_to change { role_class.count }
59
+ end
60
+ end
61
+ end
62
+
63
+ context "with an instance scoped role", :scope => :instance do
64
+ it "should add the role to the user" do
65
+ expect { subject.add_role "visitor".send(param_method), Forum.last }.to change { subject.roles.count }.by(1)
66
+ end
67
+
68
+ it "should create a role in the roles table" do
69
+ expect { subject.add_role "member".send(param_method), Forum.last }.to change { role_class.count }.by(1)
70
+ end
71
+
72
+ context "considering a new class scoped role" do
73
+ subject { role_class.last }
74
+
75
+ its(:name) { should eq("member") }
76
+ its(:resource) { should eq(Forum.last) }
77
+ end
78
+
79
+ context "should not create another role" do
80
+ it "if the role was already assigned to the user" do
81
+ subject.add_role "anonymous".send(param_method), Forum.first
82
+ expect { subject.add_role "anonymous".send(param_method), Forum.first }.not_to change { subject.roles.size }
83
+ end
84
+
85
+ it "if already existing in the database" do
86
+ role_class.create :name => "ghost", :resource_type => "Forum", :resource_id => Forum.first.id
87
+ expect { subject.add_role "ghost".send(param_method), Forum.first }.not_to change { role_class.count }
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,65 @@
1
+ shared_examples_for "Rolify.callbacks" do
2
+ before(:all) do
3
+ reset_defaults
4
+ Rolify.dynamic_shortcuts = false
5
+ role_class.destroy_all
6
+ end
7
+
8
+ after :each do
9
+ @user.roles.destroy_all
10
+ end
11
+
12
+ describe "rolify association callbacks", :if => (Rolify.orm == "active_record") do
13
+ describe "before_add" do
14
+ it "should receive callback" do
15
+ rolify_options = { :role_cname => role_class.to_s, :before_add => :role_callback }
16
+ rolify_options[:role_join_table_name] = join_table if defined? join_table
17
+ user_class.rolify rolify_options
18
+ @user = user_class.first
19
+ @user.stub(:role_callback)
20
+ @user.should_receive(:role_callback)
21
+ @user.add_role :admin
22
+ end
23
+ end
24
+
25
+ describe "after_add" do
26
+ it "should receive callback" do
27
+ rolify_options = { :role_cname => role_class.to_s, :after_add => :role_callback }
28
+ rolify_options[:role_join_table_name] = join_table if defined? join_table
29
+ user_class.rolify rolify_options
30
+ @user = user_class.first
31
+ @user.stub(:role_callback)
32
+ @user.should_receive(:role_callback)
33
+ @user.add_role :admin
34
+ end
35
+ end
36
+
37
+ describe "before_remove" do
38
+ it "should receive callback" do
39
+ rolify_options = { :role_cname => role_class.to_s, :before_remove => :role_callback }
40
+ rolify_options[:role_join_table_name] = join_table if defined? join_table
41
+ user_class.rolify rolify_options
42
+ @user = user_class.first
43
+ @user.add_role :admin
44
+ @user.stub(:role_callback)
45
+
46
+ @user.should_receive(:role_callback)
47
+ @user.remove_role :admin
48
+ end
49
+ end
50
+
51
+ describe "after_remove" do
52
+ it "should receive callback" do
53
+ rolify_options = { :role_cname => role_class.to_s, :after_remove => :role_callback }
54
+ rolify_options[:role_join_table_name] = join_table if defined? join_table
55
+ user_class.rolify rolify_options
56
+ @user = user_class.first
57
+ @user.add_role :admin
58
+ @user.stub(:role_callback)
59
+
60
+ @user.should_receive(:role_callback)
61
+ @user.remove_role :admin
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,151 @@
1
+ shared_examples_for Rolify::Dynamic do
2
+ before(:all) do
3
+ Rolify.dynamic_shortcuts = true
4
+ role_class.destroy_all
5
+ rolify_options = { :role_cname => role_class.to_s }
6
+ rolify_options[:role_join_table_name] = join_table if defined? join_table
7
+ user_class.rolify rolify_options
8
+ Forum.resourcify :roles, :role_cname => role_class.to_s
9
+ Group.resourcify :roles, :role_cname => role_class.to_s
10
+ end
11
+
12
+ context "using a global role" do
13
+ subject do
14
+ admin = user_class.first
15
+ admin.add_role :admin
16
+ admin.add_role :moderator, Forum.first
17
+ admin.add_role :solo
18
+ admin
19
+ end
20
+
21
+ it { should respond_to(:is_admin?).with(0).arguments }
22
+ it { should respond_to(:is_moderator_of?).with(1).arguments }
23
+ it { should_not respond_to(:is_god?) }
24
+
25
+ it { subject.is_admin?.should be(true) }
26
+ it { subject.is_admin?.should be(true) }
27
+ it { subject.is_admin?.should be(true) }
28
+
29
+ context "removing the role on the last user having it" do
30
+ before do
31
+ subject.remove_role :solo
32
+ end
33
+
34
+ it { should_not respond_to(:is_solo?) }
35
+ it { subject.is_solo?.should be(false) }
36
+ end
37
+ end
38
+
39
+ context "using a resource scoped role" do
40
+ subject do
41
+ moderator = user_class.where(:login => "moderator").first
42
+ moderator.add_role :moderator, Forum.first
43
+ moderator.add_role :sole_mio, Forum.last
44
+ moderator
45
+ end
46
+
47
+ it { should respond_to(:is_admin?).with(0).arguments }
48
+ it { should respond_to(:is_moderator?).with(0).arguments }
49
+ it { should respond_to(:is_moderator_of?).with(1).arguments }
50
+ it { should_not respond_to(:is_god?) }
51
+ it { should_not respond_to(:is_god_of?) }
52
+
53
+ it { subject.is_moderator?.should be(false) }
54
+ it { subject.is_moderator_of?(Forum).should be(false) }
55
+ it { subject.is_moderator_of?(Forum.first).should be(true) }
56
+ it { subject.is_moderator_of?(Forum.last).should be(false) }
57
+ it { subject.is_moderator_of?(Group).should be(false) }
58
+ it { subject.is_moderator_of?(Group.first).should be(false) }
59
+ it { subject.is_moderator_of?(Group.last).should be(false) }
60
+
61
+ context "removing the role on the last user having it" do
62
+ before do
63
+ subject.remove_role :sole_mio, Forum.last
64
+ end
65
+
66
+ it { should_not respond_to(:is_sole_mio?) }
67
+ it { subject.is_sole_mio?.should be(false) }
68
+ end
69
+ end
70
+
71
+ context "using a class scoped role" do
72
+ subject do
73
+ manager = user_class.where(:login => "god").first
74
+ manager.add_role :manager, Forum
75
+ manager.add_role :only_me, Forum
76
+ manager
77
+ end
78
+
79
+ it { should respond_to(:is_admin?).with(0).arguments }
80
+ it { should respond_to(:is_moderator?).with(0).arguments }
81
+ it { should respond_to(:is_moderator_of?).with(1).arguments }
82
+ it { should respond_to(:is_manager?).with(0).arguments }
83
+ it { should respond_to(:is_manager_of?).with(1).arguments }
84
+ it { should_not respond_to(:is_god?) }
85
+ it { should_not respond_to(:is_god_of?) }
86
+
87
+ it { subject.is_manager?.should be(false) }
88
+ it { subject.is_manager_of?(Forum).should be(true) }
89
+ it { subject.is_manager_of?(Forum.first).should be(true) }
90
+ it { subject.is_manager_of?(Forum.last).should be(true) }
91
+ it { subject.is_manager_of?(Group).should be(false) }
92
+ it { subject.is_manager_of?(Group.first).should be(false) }
93
+ it { subject.is_manager_of?(Group.last).should be(false) }
94
+
95
+ context "removing the role on the last user having it" do
96
+ before do
97
+ subject.remove_role :only_me, Forum
98
+ end
99
+
100
+ it { should_not respond_to(:is_only_me?) }
101
+ it { subject.is_only_me?.should be(false) }
102
+ end
103
+ end
104
+
105
+ context "if the role doesn't exist in the database" do
106
+
107
+ subject { user_class.first }
108
+
109
+ context "using a global role" do
110
+ before(:all) do
111
+ other_guy = user_class.last
112
+ other_guy.add_role :superman
113
+ end
114
+
115
+ it { should respond_to(:is_superman?).with(0).arguments }
116
+ it { should_not respond_to(:is_superman_of?) }
117
+ it { should_not respond_to(:is_god?) }
118
+
119
+ it { subject.is_superman?.should be(false) }
120
+ it { subject.is_god?.should be(false) }
121
+ end
122
+
123
+ context "using a resource scope role" do
124
+ before(:all) do
125
+ other_guy = user_class.last
126
+ other_guy.add_role :batman, Forum.first
127
+ end
128
+
129
+ it { should respond_to(:is_batman?).with(0).arguments }
130
+ it { should respond_to(:is_batman_of?).with(1).arguments }
131
+ it { should_not respond_to(:is_god?) }
132
+ it { should_not respond_to(:is_god_of?) }
133
+
134
+ it { subject.is_batman?.should be(false) }
135
+ it { subject.is_batman_of?(Forum).should be(false) }
136
+ it { subject.is_batman_of?(Forum.first).should be(false) }
137
+ it { subject.is_batman_of?(Forum.last).should be(false) }
138
+ it { subject.is_batman_of?(Group).should be(false) }
139
+ it { subject.is_batman_of?(Group.first).should be(false) }
140
+ it { subject.is_batman_of?(Group.last).should be(false) }
141
+
142
+ it { subject.is_god?.should be(false) }
143
+ it { subject.is_god_of?(Forum).should be(false) }
144
+ it { subject.is_god_of?(Forum.first).should be(false) }
145
+ it { subject.is_god_of?(Forum.last).should be(false) }
146
+ it { subject.is_god_of?(Group).should be(false) }
147
+ it { subject.is_god_of?(Group.first).should be(false) }
148
+ it { subject.is_god_of?(Group.last).should be(false) }
149
+ end
150
+ end
151
+ end