mongoid_ability 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mongoid_ability/subject.rb +11 -3
- data/lib/mongoid_ability/version.rb +1 -1
- data/test/mongoid_ability/subject_test.rb +23 -17
- data/test/test_helper.rb +5 -15
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4350f8ee2aa6783ba0bd4790264aae87341062fa
|
4
|
+
data.tar.gz: 7f58c17ff7e7e441fb761d0ed33f48ab298c3ec3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 701fa1fee1c99245b7c6dada06da608352bfe4e153ac728334d54d1394516ea1ac0707f24550ff7ab88b7e7aa96eb18db396aeeaa814e29933af2217b2e6a2ee
|
7
|
+
data.tar.gz: ac018c43208f7089fe308504fec8188c5fece9cdccccc6ec76447a2ce9917740c6ef4aa5984183625ff9f3b4d3db23c04e5e5472c842ee025c83e7581017690e
|
@@ -27,7 +27,7 @@ module MongoidAbility
|
|
27
27
|
# override if needed
|
28
28
|
# return for example 'MyLock'
|
29
29
|
def lock_class_name
|
30
|
-
Object.descendants.detect{ |cls| cls < MongoidAbility::Lock }.name
|
30
|
+
@lock_class_name ||= Object.descendants.detect{ |cls| cls < MongoidAbility::Lock }.name
|
31
31
|
end
|
32
32
|
|
33
33
|
# ---------------------------------------------------------------------
|
@@ -73,7 +73,11 @@ module MongoidAbility
|
|
73
73
|
|
74
74
|
excluded_ids << id_locks.map(&:subject_id)
|
75
75
|
|
76
|
-
|
76
|
+
if subject_classes.count == 1
|
77
|
+
cr = cr.or(:_id.nin => excluded_ids.flatten)
|
78
|
+
else
|
79
|
+
cr = cr.or(_type: cls.to_s, :_id.nin => excluded_ids.flatten)
|
80
|
+
end
|
77
81
|
else
|
78
82
|
included_ids = []
|
79
83
|
|
@@ -82,7 +86,11 @@ module MongoidAbility
|
|
82
86
|
|
83
87
|
included_ids << id_locks.map(&:subject_id)
|
84
88
|
|
85
|
-
|
89
|
+
if subject_classes.count == 1
|
90
|
+
cr = cr.or(:_id.in => included_ids.flatten)
|
91
|
+
else
|
92
|
+
cr = cr.or(_type: cls.to_s, :_id.in => included_ids.flatten)
|
93
|
+
end
|
86
94
|
end
|
87
95
|
end
|
88
96
|
|
@@ -15,13 +15,11 @@ module MongoidAbility
|
|
15
15
|
|
16
16
|
subject { SubjectTest.new }
|
17
17
|
|
18
|
+
let(:subject_single_test) { SubjectSingleTest.create! }
|
19
|
+
|
18
20
|
let(:subject_test_1) { SubjectTestOne.create! }
|
19
21
|
let(:subject_test_2) { SubjectTestTwo.create! }
|
20
22
|
|
21
|
-
# let(:embedded_test_subject_1) { EmbeddedTestSubject.new }
|
22
|
-
# let(:embedded_test_subject_2) { EmbeddedTestSubjectTwo.new }
|
23
|
-
# let(:embedded_test_subject_owner) { EmbeddedTestSubjectOwner.new(embedded_test_subjects: [ embedded_test_subject_1, embedded_test_subject_2 ]) }
|
24
|
-
|
25
23
|
let(:role_1) { TestRole.new }
|
26
24
|
let(:role_2) { TestRole.new }
|
27
25
|
|
@@ -70,33 +68,41 @@ module MongoidAbility
|
|
70
68
|
before do
|
71
69
|
subject_test_1
|
72
70
|
subject_test_2
|
71
|
+
subject_single_test
|
73
72
|
end
|
74
73
|
|
75
74
|
it 'returns Mongoid::Criteria' do
|
76
75
|
subject.class.accessible_by(ability).must_be_kind_of Mongoid::Criteria
|
77
|
-
# embedded_test_subject_1.class.accessible_by(ability).must_be_kind_of Mongoid::Criteria
|
78
76
|
end
|
79
77
|
|
80
|
-
# describe 'embedded relations' do
|
81
|
-
# it 'returns correct criteria type' do
|
82
|
-
# embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).embedded?.must_equal true
|
83
|
-
# end
|
84
|
-
# end
|
85
|
-
|
86
78
|
# ---------------------------------------------------------------------
|
87
79
|
|
88
80
|
describe 'default locks' do
|
89
|
-
|
81
|
+
it 'returns everything when open' do
|
82
|
+
subject.class.stub(:default_locks, [ subject_type_lock(subject.class, true) ]) do
|
83
|
+
subject.class.accessible_by(ability).must_include subject_test_1
|
84
|
+
subject.class.accessible_by(ability).must_include subject_test_2
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe 'single class' do
|
90
89
|
it 'returns everything when open' do
|
91
|
-
|
92
|
-
|
93
|
-
subject.class.accessible_by(ability).must_include subject_test_2
|
90
|
+
subject_single_test.class.stub(:default_locks, [ subject_type_lock(subject_single_test.class, true) ]) do
|
91
|
+
subject_single_test.class.accessible_by(ability).must_include subject_single_test
|
94
92
|
end
|
95
93
|
end
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'returns nothing when closed' do
|
97
|
+
subject.class.stub(:default_locks, [ subject_type_lock(subject.class, false) ]) do
|
98
|
+
subject.class.accessible_by(ability).must_be :empty?
|
99
|
+
end
|
100
|
+
end
|
96
101
|
|
102
|
+
describe 'single class' do
|
97
103
|
it 'returns nothing when closed' do
|
98
|
-
|
99
|
-
|
104
|
+
subject_single_test.class.stub(:default_locks, [ subject_type_lock(subject_single_test.class, false) ]) do
|
105
|
+
subject_single_test.class.accessible_by(ability).wont_include subject_single_test
|
100
106
|
end
|
101
107
|
end
|
102
108
|
end
|
data/test/test_helper.rb
CHANGED
@@ -84,22 +84,12 @@ end
|
|
84
84
|
class SubjectTestTwo < SubjectTest
|
85
85
|
end
|
86
86
|
|
87
|
+
class SubjectSingleTest
|
88
|
+
include Mongoid::Document
|
89
|
+
include MongoidAbility::Subject
|
87
90
|
|
88
|
-
|
89
|
-
|
90
|
-
# include Mongoid::Document
|
91
|
-
# include MongoidAbility::Subject
|
92
|
-
|
93
|
-
# embeds_many :embedded_test_subjects
|
94
|
-
# end
|
95
|
-
|
96
|
-
# class EmbeddedTestSubject < TestSubject
|
97
|
-
# embedded_in :embedded_test_subject_owner
|
98
|
-
# end
|
99
|
-
|
100
|
-
# class EmbeddedTestSubjectTwo < TestSubject
|
101
|
-
# embedded_in :embedded_test_subject_owner
|
102
|
-
# end
|
91
|
+
default_lock :read, true
|
92
|
+
end
|
103
93
|
|
104
94
|
# ---------------------------------------------------------------------
|
105
95
|
|