mongoid_ability 0.0.11 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +18 -13
  3. data/lib/mongoid_ability.rb +8 -2
  4. data/lib/mongoid_ability/ability.rb +32 -30
  5. data/lib/mongoid_ability/accessible_query_builder.rb +61 -41
  6. data/lib/mongoid_ability/lock.rb +16 -29
  7. data/lib/mongoid_ability/owner.rb +6 -22
  8. data/lib/mongoid_ability/resolve_default_locks.rb +15 -0
  9. data/lib/mongoid_ability/resolve_inherited_locks.rb +32 -0
  10. data/lib/mongoid_ability/resolve_locks.rb +34 -0
  11. data/lib/mongoid_ability/resolve_owner_locks.rb +25 -0
  12. data/lib/mongoid_ability/subject.rb +28 -32
  13. data/lib/mongoid_ability/version.rb +1 -1
  14. data/test/mongoid_ability/ability_role_test.rb +11 -5
  15. data/test/mongoid_ability/ability_test.rb +67 -91
  16. data/test/mongoid_ability/accessible_query_builder_test.rb +9 -5
  17. data/test/mongoid_ability/can_options_test.rb +17 -0
  18. data/test/mongoid_ability/lock_test.rb +51 -70
  19. data/test/mongoid_ability/owner_locks_test.rb +42 -0
  20. data/test/mongoid_ability/owner_test.rb +12 -39
  21. data/test/mongoid_ability/resolve_default_locks_test.rb +27 -0
  22. data/test/mongoid_ability/resolve_inherited_locks_test.rb +49 -0
  23. data/test/mongoid_ability/resolve_locks_test.rb +25 -0
  24. data/test/mongoid_ability/resolve_owner_locks_test.rb +50 -0
  25. data/test/mongoid_ability/subject_accessible_by_test.rb +135 -0
  26. data/test/mongoid_ability/subject_test.rb +20 -201
  27. data/test/support/test_classes.rb +136 -61
  28. data/test/test_helper.rb +3 -2
  29. metadata +20 -5
  30. data/lib/mongoid_ability/ability_resolver.rb +0 -42
  31. data/test/mongoid_ability/ability_resolver_test.rb +0 -78
data/test/test_helper.rb CHANGED
@@ -30,7 +30,8 @@ CONFIG = {
30
30
  database: database_id,
31
31
  hosts: [ "#{HOST}:#{PORT}" ]
32
32
  }
33
- }
33
+ },
34
+ options: { preload_models: true }
34
35
  }
35
36
 
36
37
  Mongoid.configure do |config|
@@ -43,4 +44,4 @@ DatabaseCleaner.strategy = :truncation
43
44
  class MiniTest::Spec
44
45
  before(:each) { DatabaseCleaner.start }
45
46
  after(:each) { DatabaseCleaner.clean }
46
- end
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_ability
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Celizna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-13 00:00:00.000000000 Z
11
+ date: 2015-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancancan
@@ -154,19 +154,28 @@ files:
154
154
  - Rakefile
155
155
  - lib/mongoid_ability.rb
156
156
  - lib/mongoid_ability/ability.rb
157
- - lib/mongoid_ability/ability_resolver.rb
158
157
  - lib/mongoid_ability/accessible_query_builder.rb
159
158
  - lib/mongoid_ability/lock.rb
160
159
  - lib/mongoid_ability/owner.rb
160
+ - lib/mongoid_ability/resolve_default_locks.rb
161
+ - lib/mongoid_ability/resolve_inherited_locks.rb
162
+ - lib/mongoid_ability/resolve_locks.rb
163
+ - lib/mongoid_ability/resolve_owner_locks.rb
161
164
  - lib/mongoid_ability/subject.rb
162
165
  - lib/mongoid_ability/version.rb
163
166
  - mongoid_ability.gemspec
164
- - test/mongoid_ability/ability_resolver_test.rb
165
167
  - test/mongoid_ability/ability_role_test.rb
166
168
  - test/mongoid_ability/ability_test.rb
167
169
  - test/mongoid_ability/accessible_query_builder_test.rb
170
+ - test/mongoid_ability/can_options_test.rb
168
171
  - test/mongoid_ability/lock_test.rb
172
+ - test/mongoid_ability/owner_locks_test.rb
169
173
  - test/mongoid_ability/owner_test.rb
174
+ - test/mongoid_ability/resolve_default_locks_test.rb
175
+ - test/mongoid_ability/resolve_inherited_locks_test.rb
176
+ - test/mongoid_ability/resolve_locks_test.rb
177
+ - test/mongoid_ability/resolve_owner_locks_test.rb
178
+ - test/mongoid_ability/subject_accessible_by_test.rb
170
179
  - test/mongoid_ability/subject_test.rb
171
180
  - test/support/test_classes.rb
172
181
  - test/test_helper.rb
@@ -196,12 +205,18 @@ specification_version: 4
196
205
  summary: Custom Ability class that allows CanCanCan authorization library store permissions
197
206
  in MongoDB via the Mongoid gem.
198
207
  test_files:
199
- - test/mongoid_ability/ability_resolver_test.rb
200
208
  - test/mongoid_ability/ability_role_test.rb
201
209
  - test/mongoid_ability/ability_test.rb
202
210
  - test/mongoid_ability/accessible_query_builder_test.rb
211
+ - test/mongoid_ability/can_options_test.rb
203
212
  - test/mongoid_ability/lock_test.rb
213
+ - test/mongoid_ability/owner_locks_test.rb
204
214
  - test/mongoid_ability/owner_test.rb
215
+ - test/mongoid_ability/resolve_default_locks_test.rb
216
+ - test/mongoid_ability/resolve_inherited_locks_test.rb
217
+ - test/mongoid_ability/resolve_locks_test.rb
218
+ - test/mongoid_ability/resolve_owner_locks_test.rb
219
+ - test/mongoid_ability/subject_accessible_by_test.rb
205
220
  - test/mongoid_ability/subject_test.rb
206
221
  - test/support/test_classes.rb
207
222
  - test/test_helper.rb
@@ -1,42 +0,0 @@
1
- module MongoidAbility
2
- class AbilityResolver
3
-
4
- def initialize owner, action, subject_type, subject=nil
5
- @subject_class = subject_type.to_s.constantize
6
-
7
- raise StandardError, "#{subject_type} class does not have default locks" unless @subject_class.respond_to?(:default_locks)
8
- raise StandardError, "#{subject_type} class does not have default lock for :#{action} action" unless @subject_class.self_and_ancestors_with_default_locks.any? do |cls|
9
- cls.default_locks.any?{ |l| l.action == action }
10
- end
11
-
12
- @owner = owner
13
- @action = action.to_sym
14
- @subject_type = subject_type.to_s
15
- @subject = subject
16
- @subject_id = subject.id if subject.present?
17
- end
18
-
19
- # ---------------------------------------------------------------------
20
-
21
- def outcome
22
- locks_for_subject_type = @owner.locks_relation.for_action(@action).for_subject_type(@subject_type)
23
-
24
- return unless locks_for_subject_type.exists?
25
-
26
- # return outcome if owner defines lock for id
27
- if @subject.present?
28
- id_locks = locks_for_subject_type.id_locks.for_subject_id(@subject_id)
29
- return false if id_locks.any?(&:closed?)
30
- return true if id_locks.any?(&:open?)
31
- end
32
-
33
- # return outcome if owner defines lock for subject_type
34
- class_locks = locks_for_subject_type.class_locks
35
- return false if class_locks.class_locks.any?(&:closed?)
36
- return true if class_locks.class_locks.any?(&:open?)
37
-
38
- nil
39
- end
40
-
41
- end
42
- end
@@ -1,78 +0,0 @@
1
- require "test_helper"
2
-
3
- module MongoidAbility
4
- describe AbilityResolver do
5
-
6
- let(:user) { TestUser.new }
7
- let(:role_editor) { TestRole.new(name: 'Editor') }
8
- let(:role_sysop) { TestRole.new(name: 'SysOp') }
9
-
10
- let(:ability_resolver_subject) { TestAbilityResolverSubject.new }
11
-
12
- subject { AbilityResolver.new(user, :read, TestAbilityResolverSubject.to_s) }
13
- let(:subject_with_id) { AbilityResolver.new(user, :read, TestAbilityResolverSubject.to_s, ability_resolver_subject) }
14
-
15
- # ---------------------------------------------------------------------
16
-
17
- describe 'errors' do
18
- it 'raises NameError for invalid subject_type' do
19
- -> { ar = AbilityResolver.new(user, :read, 'Foo') }.must_raise NameError
20
- end
21
-
22
- it 'raises StandardError when subject_type does not have default_locks' do
23
- -> { ar = AbilityResolver.new(user, :read, Object.to_s) }.must_raise StandardError
24
- end
25
-
26
- it 'raises StandardError when subject_type class or its ancestors does not have default_lock' do
27
- TestAbilityResolverSubject.stub(:default_locks, []) do
28
- -> { ar = AbilityResolver.new(user, :read, TestAbilityResolverSubject.to_s) }.must_raise StandardError
29
- end
30
- end
31
- end
32
-
33
- # ---------------------------------------------------------------------
34
-
35
- describe '#outcome' do
36
- describe 'no locks' do
37
- it 'returns nil if no locks for subject_type and action exists' do
38
- subject.outcome.must_be_nil
39
- end
40
- end
41
-
42
- describe 'id locks' do
43
- it 'returns outcome' do
44
- user.test_locks = [
45
- TestLock.new(action: :read, subject: ability_resolver_subject, outcome: true)
46
- ]
47
- subject_with_id.outcome.must_equal true
48
- end
49
-
50
- it 'prefers negative outcome' do
51
- user.test_locks = [
52
- TestLock.new(action: :read, subject: ability_resolver_subject, outcome: true),
53
- TestLock.new(action: :read, subject: ability_resolver_subject, outcome: false)
54
- ]
55
- subject_with_id.outcome.must_equal false
56
- end
57
- end
58
-
59
- describe 'class locks' do
60
- it 'returns outcome' do
61
- user.test_locks = [
62
- TestLock.new(action: :read, subject_type: TestAbilityResolverSubject.to_s, outcome: true)
63
- ]
64
- subject.outcome.must_equal true
65
- end
66
-
67
- it 'prefers negative outcome' do
68
- user.test_locks = [
69
- TestLock.new(action: :read, subject_type: TestAbilityResolverSubject.to_s, outcome: true),
70
- TestLock.new(action: :read, subject_type: TestAbilityResolverSubject.to_s, outcome: false)
71
- ]
72
- subject.outcome.must_equal false
73
- end
74
- end
75
- end
76
-
77
- end
78
- end