mongoid_ability 0.1.7 → 0.1.8

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: 113e01e0daf944524f10664bf402096ad916a57e
4
- data.tar.gz: 3c23744ac2b168352c8ba13739d762511f1a4d37
3
+ metadata.gz: 4380837ac3e83d99bb741c4f5fa31f70598500bf
4
+ data.tar.gz: 9537c54dfcb14ee4a9d0affd7fb9291c5ecc7e55
5
5
  SHA512:
6
- metadata.gz: 1bc440f3326d1b305ff277b0c3a7b5dd577ad79c1808dc3c80d23917a534b645249f7e373f0d4a1d56da810300379d6d9d1754d3157a25b8df452fa05cfd740b
7
- data.tar.gz: 62a122fad732b65ece84495df5ec7240d6c54912a363ef3d76f4bea1188490e4e8da53eadf4b25a5aab999b2e3472e2ce32e4ec2ffc1a6b20643bc99db07a0b9
6
+ metadata.gz: 37dbd56daf307a7af85f54a254dde5a9546059fea164540dd2c3512b8543587c8f42278c49169134e2acc66d3194457117c10ae1201712baba685bbccb0b5a42
7
+ data.tar.gz: 25cd98e83f5145b3b694a6e689ae9d533558752b3f4f17561b2420c85fde2be068b5250d0099d33c2cc394012359722aa5175629c86df392dc493133dec8978e
@@ -20,55 +20,17 @@ module MongoidAbility
20
20
  @owner = owner
21
21
 
22
22
  can do |action, subject_type, subject, options|
23
- _can(action, subject_type, subject, options)
23
+ subject_class = subject_type.to_s.constantize
24
+ outcome = nil
25
+ options ||= {}
24
26
 
25
- # if defined? Rails
26
- # ::Rails.cache.fetch( [ cache_key ] + cache_keys(action, subject_type, subject, options) ) do
27
- # _can(action, subject_type, subject, options)
28
- # end
29
- # else
30
- # _can(action, subject_type, subject, options)
31
- # end
32
- end
33
- end
34
-
35
- # ---------------------------------------------------------------------
27
+ subject_class.self_and_ancestors_with_default_locks.each do |cls|
28
+ outcome = ResolveInheritedLocks.call(owner, action, cls, subject, options)
29
+ break if outcome != nil
30
+ end
36
31
 
37
- def _can action, subject_type, subject, options
38
- subject_class = subject_type.to_s.constantize
39
- outcome = nil
40
- options ||= {}
41
-
42
- subject_class.self_and_ancestors_with_default_locks.each do |cls|
43
- outcome = ResolveInheritedLocks.call(owner, action, cls, subject, options)
44
- break if outcome != nil
32
+ outcome
45
33
  end
46
-
47
- outcome
48
- end
49
-
50
- # ---------------------------------------------------------------------
51
-
52
- def cache_key
53
- ["ability", owner.cache_key, inherit_from_relation_cache_keys].compact.join('/')
54
- end
55
-
56
- def cache_keys action, subject_type, subject, options
57
- res = []
58
- res << action
59
- res << subject_type
60
- res << subject.cache_key unless subject.nil?
61
- res << options_cache_key(options)
62
- res.compact
63
- end
64
-
65
- def options_cache_key options={}
66
- Digest::SHA1.hexdigest( options.to_a.sort_by { |k,v| k.to_s }.to_s )
67
- end
68
-
69
- def inherit_from_relation_cache_keys
70
- return unless owner.respond_to?(owner.class.inherit_from_relation_name) && owner.inherit_from_relation != nil
71
- owner.inherit_from_relation.map(&:cache_key)
72
34
  end
73
35
 
74
36
  # ---------------------------------------------------------------------
@@ -1,3 +1,5 @@
1
+ # FIXME: this is extremely slow and not suitable for use, yet
2
+
1
3
  module MongoidAbility
2
4
  class AccessibleQueryBuilder < Struct.new(:base_class, :ability, :action, :options)
3
5
 
@@ -8,24 +10,11 @@ module MongoidAbility
8
10
  # =====================================================================
9
11
 
10
12
  def call
11
- _call
12
-
13
- # if defined? Rails
14
- # # FIXME: this is a bit of a dirty hack, since the marshalling of criteria does not preserve the embedded attributes
15
- # Rails.cache.fetch( [ 'ability-query', base_class, ability.cache_key, action, ability.options_cache_key(options) ] ) do
16
- # _call
17
- # end.tap { |c| c.embedded = base_criteria.embedded }
18
- # else
19
- # _call
20
- # end
13
+ base_class_and_descendants.inject(base_criteria) { |criteria, cls| criteria.merge!(criteria_for_class(cls)) }
21
14
  end
22
15
 
23
16
  private # =============================================================
24
17
 
25
- def _call
26
- base_class_and_descendants.inject(base_criteria) { |criteria, cls| criteria.merge!(criteria_for_class(cls)) }
27
- end
28
-
29
18
  def base_criteria
30
19
  @base_criteria ||= base_class.criteria
31
20
  end
@@ -40,8 +40,20 @@ module MongoidAbility
40
40
  res
41
41
  end
42
42
 
43
+ # calculates outcome as if this lock is not present
44
+ def inherited_outcome
45
+ return unless owner.present?
46
+ cloned_owner = owner.clone
47
+ cloned_owner.locks_relation = cloned_owner.locks_relation - [self]
48
+ MongoidAbility::Ability.new(cloned_owner).can? action, (id_lock? ? subject : subject_class)
49
+ end
50
+
43
51
  # ---------------------------------------------------------------------
44
52
 
53
+ def subject_class
54
+ subject_type.constantize
55
+ end
56
+
45
57
  def open? options={}
46
58
  calculated_outcome(options) == true
47
59
  end
@@ -26,6 +26,10 @@ module MongoidAbility
26
26
  self.send(self.class.locks_relation_name)
27
27
  end
28
28
 
29
+ def locks_relation= val
30
+ self.send "#{self.class.locks_relation_name}=", val
31
+ end
32
+
29
33
  def inherit_from_relation
30
34
  self.send(self.class.inherit_from_relation_name)
31
35
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidAbility
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -49,6 +49,27 @@ module MongoidAbility
49
49
 
50
50
  # ---------------------------------------------------------------------
51
51
 
52
+ describe '#inherited_outcome' do
53
+ let(:my_subject) { MySubject.new }
54
+ let(:subject_type_lock) { MyLock.new(subject_type: MySubject, action: :read, outcome: false) }
55
+ let(:subject_lock) { MyLock.new(subject: my_subject, action: :read, outcome: true) }
56
+ let(:owner) { MyOwner.new(my_locks: [subject_type_lock, subject_lock]) }
57
+ let(:ability) { Ability.new(owner) }
58
+
59
+ before { ability } # initialize owner
60
+
61
+ it 'does not affect calculated_outcome' do
62
+ ability.can?(:read, my_subject).must_equal true
63
+ end
64
+
65
+ it 'returns calculated_outcome without this lock' do
66
+ subject_lock.inherited_outcome.must_equal false
67
+ subject_type_lock.inherited_outcome.must_equal true
68
+ end
69
+ end
70
+
71
+ # ---------------------------------------------------------------------
72
+
52
73
  describe '#criteria' do
53
74
  let(:open_subject_type_lock) { MyLock.new(subject_type: MySubject, action: :read, outcome: true) }
54
75
  let(:closed_subject_type_lock) { MyLock.new(subject_type: MySubject, action: :read, outcome: false) }
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.1.7
4
+ version: 0.1.8
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-08-10 00:00:00.000000000 Z
11
+ date: 2015-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancancan