mongoid_ability 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9a16f215f974b85cd17bd6675d923602799f392
4
- data.tar.gz: 279a119c7ffab5ff22f94dd0879f835167013341
3
+ metadata.gz: f40f0a70a06a2ef26a9e545cd18a940cd46e4be5
4
+ data.tar.gz: 0f105daba66547732f737fb1928ab7615d2e9162
5
5
  SHA512:
6
- metadata.gz: 1c3d0e88ad2427638da638689a72f19c2d6ba605aa9a0e0c4355dd57f44d5cc3940daa82e0658a029eb0a66baba24f04f63349bb3a3de9440c9171dab2681966
7
- data.tar.gz: f1f8f836c9961bf9c114fb63993d9a99512deabb5cd09b85a028bceec24c9cc644aaf88a90e1c68a42eeeada20f074b9fe6330c81a30eb445a322cdf87f0d436
6
+ metadata.gz: c74b4310a0a87557bd113f71b067116137a3b9f6f09d0804ed3fb4757f6c090887520c2cedaac882c60e96cfbc76ee96e7e29193cf08d32a13952a55c3bd1215
7
+ data.tar.gz: 999a48cd89887c46366621f781a359208db66cd50fc512aeee4c812c52556d20abf6c4628852a59274fd83c4782f4f46c792b7efe157d03da2a0e73c4c0702e3
@@ -43,9 +43,9 @@ module MongoidAbility
43
43
  # ---------------------------------------------------------------------
44
44
 
45
45
  def accessible_by ability, action=:read
46
- criteria = Mongoid::Criteria.new(self)
46
+ cr = self.criteria
47
47
 
48
- return criteria unless ability.user.present?
48
+ return cr unless ability.user.present?
49
49
 
50
50
  id_locks = [
51
51
  ability.user,
@@ -55,7 +55,7 @@ module MongoidAbility
55
55
  }.flatten
56
56
 
57
57
  if ability.can?(action, self)
58
- criteria.nin({
58
+ cr.nin({
59
59
  _id: id_locks.map(&:subject_id).select do |subject_id|
60
60
  subject = self.new
61
61
  subject.id = subject_id
@@ -63,7 +63,7 @@ module MongoidAbility
63
63
  end
64
64
  })
65
65
  else
66
- criteria.in({
66
+ cr.in({
67
67
  _id: id_locks.map(&:subject_id).select do |subject_id|
68
68
  subject = self.new
69
69
  subject.id = subject_id
@@ -1,3 +1,3 @@
1
1
  module MongoidAbility
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -3,24 +3,29 @@ require 'test_helper'
3
3
  module MongoidAbility
4
4
  describe Subject do
5
5
 
6
- subject { TestSubject.new }
7
- let(:subject_super) { TestSubjectSuper.new }
6
+ def default_lock subject_cls, outcome
7
+ TestLock.new(subject_type: subject_cls.to_s, action: :read, outcome: outcome)
8
+ end
8
9
 
9
- let(:user) { TestUser.new }
10
- let(:ability) { Ability.new(user) }
10
+ def subject_lock subject, outcome
11
+ TestLock.new(subject: subject, action: :read, outcome: outcome)
12
+ end
11
13
 
12
- let(:open_lock) { TestLock.new(outcome: true, action: :read, subject: subject) }
13
- let(:closed_lock) { TestLock.new(outcome: false, action: :read, subject: subject) }
14
+ # ---------------------------------------------------------------------
14
15
 
15
- # =====================================================================
16
+ subject { TestSubject.new }
16
17
 
17
- describe 'fields' do
18
- end
18
+ let(:subject_test_1) { TestSubject.create! }
19
+ let(:subject_test_2) { TestSubject.create! }
19
20
 
20
- # =====================================================================
21
+ let(:subject_super) { TestSubjectSuper.new }
21
22
 
22
- describe 'relations' do
23
- end
23
+ let(:embedded_test_subject_1) { EmbeddedTestSubject.new }
24
+ let(:embedded_test_subject_2) { EmbeddedTestSubject.new }
25
+ let(:embedded_test_subject_owner) { EmbeddedTestSubjectOwner.new(embedded_test_subjects: [ embedded_test_subject_1, embedded_test_subject_2 ]) }
26
+
27
+ let(:user) { TestUser.new }
28
+ let(:ability) { Ability.new(user) }
24
29
 
25
30
  # =====================================================================
26
31
 
@@ -44,8 +49,8 @@ module MongoidAbility
44
49
  end
45
50
  end
46
51
 
47
- # ---------------------------------------------------------------------
48
-
52
+ # =====================================================================
53
+
49
54
  describe '.ancestors_with_default_locks' do
50
55
  it 'lists ancestors with default locks' do
51
56
  subject.class.ancestors_with_default_locks.must_equal [TestSubjectSuper]
@@ -58,45 +63,128 @@ module MongoidAbility
58
63
  end
59
64
  end
60
65
 
61
- # ---------------------------------------------------------------------
62
-
66
+ # =====================================================================
67
+
63
68
  describe '.accessible_by' do
69
+
64
70
  it 'returns Mongoid::Criteria' do
65
71
  subject.class.accessible_by(ability).must_be_kind_of Mongoid::Criteria
72
+ embedded_test_subject_1.class.accessible_by(ability).must_be_kind_of Mongoid::Criteria
73
+ end
74
+
75
+ describe 'embedded relations' do
76
+ it 'returns correct criteria type' do
77
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).embedded?.must_equal true
78
+ end
79
+ end
80
+
81
+ # ---------------------------------------------------------------------
82
+
83
+ describe 'default locks' do
84
+ describe 'referenced relations' do
85
+ it 'returns everything when open' do
86
+ subject_test_1
87
+ subject_test_2
88
+
89
+ subject.class.stub(:default_locks, [ default_lock(subject_test_1.class, true) ]) do
90
+ subject.class.accessible_by(ability).must_include subject_test_1
91
+ subject.class.accessible_by(ability).must_include subject_test_2
92
+ end
93
+ end
94
+
95
+ it 'returns nothing when closed' do
96
+ subject.class.stub(:default_locks, [ default_lock(subject_test_1.class, false) ]) do
97
+ subject.class.accessible_by(ability).must_be :empty?
98
+ end
99
+ end
100
+ end
101
+
102
+ describe 'embedded relations' do
103
+ it 'returns everything when open' do
104
+ subject.class.stub(:default_locks, [ default_lock(embedded_test_subject_1.class, true) ]) do
105
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_1
106
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_2
107
+ end
108
+ end
109
+
110
+ it 'returns nothing when closed' do
111
+ subject.class.stub(:default_locks, [ default_lock(embedded_test_subject_1.class, false) ]) do
112
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_be :empty?
113
+ end
114
+ end
115
+ end
66
116
  end
67
117
 
68
- describe 'when closed lock on user' do
69
- before { user.test_locks = [closed_lock] }
70
- it 'returns criteria excluding such ids' do
71
- subject.class.accessible_by(ability).selector.fetch('_id', {}).fetch('$nin', []).must_include subject.id
118
+ # ---------------------------------------------------------------------
119
+
120
+ describe 'id locks' do
121
+ describe 'referenced relations' do
122
+ it 'excludes subject when closed' do
123
+ user.test_locks = [ subject_lock(subject_test_1, true), subject_lock(subject_test_2, false) ]
124
+ subject.class.accessible_by(ability).must_include subject_test_1
125
+ subject.class.accessible_by(ability).wont_include subject_test_2
126
+ end
127
+ end
128
+
129
+ describe 'embedded relations' do
130
+ it 'excludes subject when closed' do
131
+ user.test_locks = [ subject_lock(embedded_test_subject_1, true), subject_lock(embedded_test_subject_2, false) ]
132
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_1
133
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).wont_include embedded_test_subject_2
134
+ end
72
135
  end
73
136
  end
74
137
 
75
- # describe "when closed lock on user's role" do
76
- # before { user.roles = [TestRole.new(test_locks: [closed_lock])] }
77
- # it 'returns criteria excluding such ids' do
78
- # subject.class.accessible_by(ability).selector.fetch('_id', {}).fetch('$nin', []).must_include subject.id
79
- # end
80
- # end
138
+ # ---------------------------------------------------------------------
139
+
140
+ describe 'default locks & id locks' do
141
+ describe 'referenced relations' do
142
+ describe 'default open' do
143
+ it 'excludes subject when id lock closed' do
144
+ subject.class.stub(:default_locks, [ default_lock(subject_test_1.class, true) ]) do
145
+ user.test_locks = [ subject_lock(subject_test_2, false) ]
146
+ subject.class.accessible_by(ability).must_include subject_test_1
147
+ subject.class.accessible_by(ability).wont_include subject_test_2
148
+ end
149
+ end
150
+ end
81
151
 
82
- describe "when class does not permit" do
83
- before do
84
- user.test_locks = [open_lock]
152
+ describe 'default closed' do
153
+ it 'includes subject when id lock open' do
154
+ subject.class.stub(:default_locks, [ default_lock(subject_test_1.class, false) ]) do
155
+ user.test_locks = [ subject_lock(subject_test_2, true) ]
156
+ subject.class.accessible_by(ability).wont_include subject_test_1
157
+ subject.class.accessible_by(ability).must_include subject_test_2
158
+ end
159
+ end
160
+ end
85
161
  end
86
- it 'returns criteria excluding everything but open id_locks' do
87
- subject.class.stub(:default_locks, [TestLock.new(outcome: false, action: :read, subject_type: subject.class, outcome: false)]) do
88
- subject.class.accessible_by(ability).selector.fetch('_id', {}).fetch('$in', []).must_include subject.id
162
+
163
+ describe 'embedded relations' do
164
+ describe 'default open' do
165
+ it 'excludes subject when id lock closed' do
166
+ subject.class.stub(:default_locks, [ default_lock(embedded_test_subject_1.class, true) ]) do
167
+ user.test_locks = [ subject_lock(embedded_test_subject_2, false) ]
168
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_1
169
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).wont_include embedded_test_subject_2
170
+ end
171
+ end
172
+ end
173
+
174
+ describe 'default closed' do
175
+ it 'includes subject when id lock open' do
176
+ subject.class.stub(:default_locks, [ default_lock(embedded_test_subject_1.class, false) ]) do
177
+ user.test_locks = [ subject_lock(embedded_test_subject_2, true) ]
178
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).wont_include embedded_test_subject_1
179
+ embedded_test_subject_owner.embedded_test_subjects.accessible_by(ability).must_include embedded_test_subject_2
180
+ end
181
+ end
89
182
  end
90
183
  end
91
184
  end
92
-
185
+
93
186
  end
94
187
  end
95
188
  end
96
189
 
97
- # =====================================================================
98
-
99
- describe 'instance methods' do
100
- end
101
-
102
- end
190
+ end
data/test/test_helper.rb CHANGED
@@ -70,7 +70,7 @@ class TestOwner < TestOwnerSuper
70
70
  end
71
71
 
72
72
  # ---------------------------------------------------------------------
73
-
73
+
74
74
  class TestSubjectSuper
75
75
  include Mongoid::Document
76
76
  include MongoidAbility::Subject
@@ -80,6 +80,17 @@ class TestSubject < TestSubjectSuper
80
80
  default_lock :read, true
81
81
  end
82
82
 
83
+ class EmbeddedTestSubjectOwner
84
+ include Mongoid::Document
85
+ include MongoidAbility::Subject
86
+
87
+ embeds_many :embedded_test_subjects
88
+ end
89
+
90
+ class EmbeddedTestSubject < TestSubject
91
+ embedded_in :embedded_test_subject_owner
92
+ end
93
+
83
94
  # ---------------------------------------------------------------------
84
95
 
85
96
  class TestAbilityResolverSubject
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.2
4
+ version: 0.0.3
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-05-10 00:00:00.000000000 Z
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancancan