simple_roles 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/CHANGELOG.md +21 -0
  2. data/Gemfile +2 -4
  3. data/README.md +100 -17
  4. data/Rakefile +6 -12
  5. data/db/migrate/001_create_user_roles.rb +5 -4
  6. data/db/migrate/002_create_roles.rb +6 -3
  7. data/lib/simple_roles/configuration.rb +64 -11
  8. data/lib/simple_roles/engine.rb +2 -0
  9. data/lib/simple_roles/macros.rb +2 -5
  10. data/lib/simple_roles/many/persistence.rb +44 -0
  11. data/lib/simple_roles/many/roles_methods.rb +88 -0
  12. data/lib/simple_roles/many.rb +5 -0
  13. data/lib/simple_roles/one/persistence.rb +13 -0
  14. data/lib/simple_roles/one/roles_methods.rb +38 -0
  15. data/lib/simple_roles/one.rb +5 -0
  16. data/lib/simple_roles/packager.rb +18 -0
  17. data/lib/simple_roles/version.rb +1 -1
  18. data/lib/simple_roles.rb +27 -12
  19. data/simple_roles.gemspec +36 -30
  20. data/spec/dummy/config/database.yml +2 -13
  21. data/spec/dummy/config/environments/development.rb +0 -3
  22. data/spec/dummy/config/initializers/simple_roles.rb +2 -1
  23. data/spec/integration/main_spec.rb +6 -6
  24. data/spec/integration/requests/main_spec.rb +16 -17
  25. data/spec/simple_roles/configuration_spec.rb +66 -0
  26. data/spec/simple_roles/integration_many_spec.rb +47 -0
  27. data/spec/simple_roles/macros_spec.rb +25 -10
  28. data/spec/{support/aliases.rb → simple_roles/many/persistence_spec.rb} +0 -0
  29. data/spec/simple_roles/many_spec.rb +186 -0
  30. data/spec/simple_roles/one_spec.rb +77 -0
  31. data/spec/spec_helper.rb +40 -21
  32. data/spec/support/controller_macros.rb +1 -18
  33. data/spec/support/database.yml +1 -1
  34. data/spec/support/factories.rb +6 -17
  35. data/spec/support/migrations/010_create_one_users.rb +16 -0
  36. data/spec/support/migrations/{010_create_users.rb → 011_create_users.rb} +3 -3
  37. data/spec/support/{fixtures/models → models}/.gitkeep +0 -0
  38. data/spec/support/models/one_user.rb +2 -0
  39. data/spec/support/{fixtures/models → models}/user.rb +0 -1
  40. data/spec/support/setup_roles.rb +5 -0
  41. data/spec/support/transaction.rb +30 -0
  42. data/spec/transaction_spec.rb +15 -0
  43. metadata +176 -137
  44. data/lib/simple_roles/base.rb +0 -111
  45. data/lib/simple_roles/roles_array.rb +0 -63
  46. data/spec/integration/messages_spec.rb +0 -62
  47. data/spec/simple_roles/base_spec.rb +0 -191
  48. data/spec/support/rspec_helpers.rb +0 -22
@@ -1,111 +0,0 @@
1
- module SimpleRoles
2
- module Base
3
-
4
- class << self
5
- def included base
6
- base.class_eval %{
7
- has_many :user_roles
8
- has_many :db_roles, :through => :user_roles, :class_name => 'Role', :source => :role
9
- }
10
-
11
- base.send :include, SimpleRoles::Base::InstanceMethods
12
- SimpleRoles::Configuration.user_models << base
13
- base.register_roles_methods
14
- end
15
- end
16
-
17
- module ClassMethods
18
- def register_roles_methods
19
- valid_roles.each do |role|
20
- class_eval %{
21
- def self.#{role}s
22
- Role.find_by_name("#{role}").users
23
- end
24
-
25
- def self.#{role}s_ids
26
- Role.find_by_name("#{role}").user_ids
27
- end
28
- }
29
-
30
- define_method :"#{role}?" do
31
- roles.include?(:"#{role}")
32
- end
33
-
34
- alias_method :"is_#{role}?", :"#{role}?"
35
- end
36
- end
37
-
38
- def valid_roles
39
- SimpleRoles::Configuration.valid_roles
40
- end
41
- end
42
-
43
- module InstanceMethods
44
-
45
- def self.included base
46
- base.send :extend, ClassMethods
47
- end
48
-
49
- def mass_assignment_authorizer *args
50
- super.empty? ? super : (super + [:roles])
51
- end
52
-
53
- def roles
54
- roles_array
55
- end
56
-
57
- alias_method :roles_list, :roles
58
-
59
- def has_roles? *rolez
60
- rolez.flatten!
61
- rolez.each do |role|
62
- return false if !roles.include? role
63
- end
64
- true
65
- end
66
-
67
- alias_method :has_role?, :has_roles?
68
-
69
- def has_any_role? *rolez
70
- rolez.flatten!
71
- rolez.each do |role|
72
- return true if roles.include? role
73
- end
74
- false
75
- end
76
-
77
- def add_roles *rolez
78
- roles_array.add *rolez
79
- end
80
-
81
- alias_method :add_role, :add_roles
82
-
83
- def remove_roles *rolez
84
- roles_array.remove *rolez
85
- end
86
-
87
- alias_method :remove_role, :remove_roles
88
-
89
- def roles= *rolez
90
- roles_array.roles = *rolez
91
- end
92
-
93
- # TODO: implement
94
- #
95
- def role_groups_list
96
- []
97
- end
98
-
99
- private
100
-
101
- def roles_array
102
- @roles_array ||= SimpleRoles::RolesArray.new self
103
- end
104
-
105
- end
106
-
107
- end
108
- end
109
-
110
-
111
-
@@ -1,63 +0,0 @@
1
- module SimpleRoles
2
- class RolesArray < Array
3
-
4
- attr_reader :base
5
-
6
- def initialize *args
7
- @base = args.delete(args.first)
8
- super
9
- synchronize
10
- end
11
-
12
- def synchronize
13
- replace real_roles
14
- end
15
-
16
- def real_roles_db
17
- base.db_roles
18
- end
19
-
20
- def real_roles
21
- real_roles_db.map(&:name).map(&:to_sym)
22
- end
23
-
24
- def roles= *rolez
25
- rolez.to_symbols!.flatten!
26
- raise "Not a valid role!" if (rolez.to_a - SimpleRoles::Configuration.valid_roles).size > 0
27
-
28
- base.db_roles = rolez.map do |rolle|
29
- begin
30
- Role.find_by_name!(rolle.to_s)
31
- rescue
32
- raise "Couldn't find Role for #{rolle}. Maybe you need to re-run migrations?"
33
- end
34
- end
35
-
36
- synchronize
37
- end
38
-
39
- def << *rolez
40
- rolez.flatten!
41
- self.roles = self.to_a + rolez
42
- end
43
-
44
- alias_method :add, :<<
45
-
46
- def remove *rolez
47
- rolez.flatten!
48
- self.roles = self.to_a - rolez
49
- end
50
-
51
- def clear!
52
- real_roles_db.clear
53
- base.save!
54
- clear
55
- end
56
-
57
- private
58
-
59
- def clear
60
- super
61
- end
62
- end
63
- end
@@ -1,62 +0,0 @@
1
- require 'dummy_spec_helper'
2
-
3
- module DeviseSessionHelpers
4
- def login_with email, password
5
- fill_in "Email", :with => email
6
- fill_in "Password", :with => password
7
- click_button "Sign in"
8
- end
9
-
10
- def login_user
11
- visit new_user_session_path
12
- login_with 'stanislaw@gmail.com', "666666"
13
- end
14
- end
15
-
16
- feature "Messages", %q{
17
- In order to have...
18
- As an user
19
- I want to do something with messages} do
20
-
21
- background do
22
- Capybara.reset_sessions!
23
- end
24
-
25
- include DeviseSessionHelpers
26
-
27
- scenario "Show messages index", :js => true do
28
- login_user
29
- visit '/carrier'
30
- end
31
-
32
- scenario "Show concerto index to musician", :js => true do
33
- pending
34
- login_musician
35
- # save_and_open_page
36
-
37
- end
38
-
39
- scenario "Show concerto to musician" do
40
- pending
41
-
42
- login_musician
43
-
44
- visit '/concertos/one' # using friendly id :)
45
- page.should have_content('one')
46
- visit '/concertos/two'
47
- page.should have_content('two')
48
- end
49
-
50
- scenario "Show concerto admin index to composer", :js => true do
51
- pending
52
- login_composer
53
-
54
- visit '/concertos/admin'
55
- # save_and_open_page
56
-
57
- #puts page.body.inspect
58
- page.should have_content('one')
59
- page.should have_content('two')
60
- end
61
- end
62
-
@@ -1,191 +0,0 @@
1
- require 'spec_helper'
2
-
3
- SimpleRoles.configure do |config|
4
- config.valid_roles = [:user, :admin, :editor]
5
- end
6
-
7
- describe SimpleRoles::Base do
8
-
9
- context "Class Methods" do
10
- subject { User }
11
-
12
- context "Scopes" do
13
- before do
14
- end
15
-
16
- SimpleRoles::Configuration.valid_roles.each do |vr|
17
- it {should respond_to(:"#{vr}s")}
18
- it {should respond_to(:"#{vr}s_ids")}
19
-
20
- its(:"#{vr}s") { should be_kind_of(Array) }
21
- end
22
-
23
- end
24
- specify { should respond_to(:valid_roles) }
25
- its(:valid_roles) { should include(:user, :admin)}
26
- end
27
-
28
- context "Instance methods" do
29
- subject {User.new}
30
-
31
- [:db_roles, :user_roles].each do |meth|
32
- specify { should respond_to(meth) }
33
- its(:"#{meth}") { should be_empty }
34
- end
35
-
36
- [:roles, :roles_list, :role_groups_list].each do |meth|
37
- specify { should respond_to(meth) }
38
- its(:"#{meth}") { should be_empty }
39
- end
40
-
41
- context "#roles" do
42
- it "call on #roles.clear should raise error" do
43
- lambda {
44
- roles.clear
45
- }.should raise_error
46
- end
47
- end
48
-
49
- end
50
-
51
- context "Read API" do
52
- subject do
53
- @user ||= User.new(:name => "stanislaw")
54
- end
55
-
56
- it "#has_role?, #has_roles?" do
57
- subject.roles << :admin
58
- subject.has_role?(:admin).should be_true
59
- subject.has_role?(:admin, :user).should be_false
60
- subject.has_roles?(:editor).should be_false
61
- subject.roles << :user
62
- subject.has_role?(:admin, :user).should be_true
63
- subject.has_role?([:admin, :user]).should be_true
64
- end
65
-
66
- it "#admin?, #user?, #editor? ..." do
67
- subject.roles << :admin
68
- subject.admin?.should be_true
69
- subject.is_admin?.should be_true
70
- subject.user?.should be_false
71
- subject.editor?.should be_false
72
- subject.roles << :editor
73
- subject.editor?.should be_true
74
- end
75
- end
76
-
77
- context "Write API" do
78
- subject do
79
- @user ||= User.new(:name => "stanislaw")
80
- end
81
-
82
- it "#roles= should set roles" do
83
- subject.roles = :admin
84
- subject.roles.should == Array.new([:admin])
85
- subject.roles = :user
86
- subject.roles.should == Array.new([:user])
87
- end
88
-
89
- it "#roles= should set roles if array of strings passed (sh accept strings too!)" do
90
- subject.roles = 'admin'
91
- subject.roles.should == Array.new([:admin])
92
- subject.roles = ['user', 'editor']
93
- subject.roles.should == Array.new([:user, :editor])
94
- end
95
-
96
- it "#roles << should add roles" do
97
- subject.roles << :admin
98
- subject.roles.should == Array.new([:admin])
99
- subject.roles << :user
100
- subject.roles.should == Array.new([:admin, :user])
101
- end
102
-
103
- it "#remove_roles should remove roles" do
104
- subject.roles << :admin
105
- subject.roles << :user
106
- subject.roles << :editor
107
-
108
- subject.roles.should == Array.new([:admin, :user, :editor])
109
-
110
- subject.remove_roles :admin
111
- subject.roles.should == Array.new([:user, :editor])
112
-
113
- subject.remove_roles :admin, :user, :editor
114
- subject.roles.should == Array.new([])
115
- end
116
- end
117
-
118
- context "Integration for roles methods" do
119
- it "should work when #flatten is called over #roles" do
120
- user = User.new(:name => "stanislaw")
121
- user.roles << :admin
122
-
123
- user.roles_list.should == Array.new([:admin])
124
- user.roles_list.flatten.should == Array.new([:admin])
125
- end
126
-
127
- it "should add :roles to accessible_attributes if they are Whitelisted" do
128
- user = User.new(:name => "stanislaw")
129
- user.roles << :admin
130
-
131
- user.roles_list.should include(:admin)
132
- user.save!
133
- User.find_by_name!("stanislaw").should be_kind_of(User)
134
- User.delete_all
135
-
136
- User.attr_accessible :name
137
-
138
- user = User.new(:name => "stanislaw")
139
- user.roles << :admin
140
- user.roles_list.should include(:admin)
141
- user.save!
142
- User.find_by_name!("stanislaw").should be_kind_of(User)
143
- end
144
-
145
- pending "should not duplicate roles when adding" do
146
-
147
- end
148
-
149
- it "should all work" do
150
- admin_role = Role.find_by_name("admin")
151
- user = User.new(:name => "stanislaw")
152
- user.roles_list.should be_empty
153
- user.has_any_role?(:admin).should be_false
154
- user.roles << :admin
155
- user.db_roles.should include(admin_role)
156
- user.roles_list.should include(:admin)
157
- user.roles.should include(:admin)
158
- user.has_role?(:admin).should be_true
159
- user.admin?.should be_true
160
- user.is_admin?.should be_true
161
- user.has_roles?(:admin).should be_true
162
- user.save!
163
- user.db_roles.should include(admin_role)
164
- user.roles.should include(:admin)
165
- user = User.find_by_name! "stanislaw"
166
- user.roles.should include(:admin)
167
- user.roles.remove(:admin)
168
- user.roles.should be_empty
169
- user.save!
170
- user.roles.should be_empty
171
- user.roles = [:admin, :user]
172
- user.roles.should == Array.new([:admin, :user])
173
- user.has_role?(:admin, :user).should be_true
174
- user.has_roles?([:admin, :user]).should be_true
175
- user.db_roles.size.should == 2
176
- user.roles.clear!
177
- user.db_roles.should be_empty
178
- user.roles.should be_empty
179
- user.roles << :admin
180
- user.db_roles.should include(admin_role)
181
- user.roles.should include(:admin)
182
- user.add_role :user
183
- user.roles.should include(:user, :admin)
184
- user.has_any_role?(:user).should be_true
185
- user.has_any_role?(:user, :admin).should be_true
186
- user.has_any_role?([:user, :admin])
187
- user.has_any_role?(:blip).should be_false
188
- end
189
- end
190
-
191
- end
@@ -1,22 +0,0 @@
1
-
2
- module RSpec
3
- module Core
4
- class ExampleGroup
5
-
6
- class << self
7
- alias_method :concern, :describe if instance_methods.include?(:describe)
8
-
9
- def register_concern
10
- alias_method :concern, :describe if instance_methods.include?(:describe)
11
- end
12
-
13
- def singleton_method_added name
14
- class << self
15
- undef_method :register_concern
16
- end if name == :describe
17
- end
18
-
19
- end
20
- end
21
- end
22
- end