mongoid_ability 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fb77bedd14bcfb7ebb1273b4ef828ff32bad598
4
- data.tar.gz: 64be0c5963ed532e20fe346c11ac7b2e8c8e1597
3
+ metadata.gz: 9e8efe06a6a08b5f7d80532153f9324597c3a932
4
+ data.tar.gz: e77cc428a3509f03cca38a40b9a25155d3cd3539
5
5
  SHA512:
6
- metadata.gz: 9d6c8e052c01530819e06b298bfb9e8cb49bb225c853c157a3789e07f4f722a06f4a24f9a129bf21194cf48a913637033c2b22efb499a3a73193ba5e0e06f758
7
- data.tar.gz: 85c46d10706e25f424138cc5645d6030aa97446e8a3b430ecd6ef751c13102671c098d9605f74f624f4c4cb5bd46a6f4cf4a6f05ca1d0ef4ca6e45b5b4c96c56
6
+ metadata.gz: ddef2f4d29749491c06f0dd21601fa509208808c2542b87eade4eb73fb91644b0500aabd9eccf30e6b8bb150cad60101b88d34d0ed1b6a3f994cc55706f623c2
7
+ data.tar.gz: d774fe84fded69148b77b5edddfcc21fed50371159dfc8454b58f916f0d1e2c5a5eddc9f87a4859235c52d15ece619966f1af1bbfbca1197e6cc91d11b6d7e11
@@ -11,6 +11,7 @@ require "mongoid_ability/resolve_default_locks"
11
11
  require "mongoid_ability/resolve_inherited_locks"
12
12
  require "mongoid_ability/resolve_owner_locks"
13
13
 
14
+ require "mongoid_ability/values_for_accessible_query"
14
15
  require "mongoid_ability/accessible_query_builder"
15
16
 
16
17
  # ---------------------------------------------------------------------
@@ -6,80 +6,42 @@ module MongoidAbility
6
6
 
7
7
  # =====================================================================
8
8
 
9
- # TODO: cleanup
10
9
  def call
11
- closed_classes = [] # [cls]
12
- open_ids = [] # [cls, id]
13
- closed_ids = [] # [id]
14
-
15
- base_class_and_descendants.each do |cls|
16
- closed_classes << cls.to_s if ability.cannot?(action, cls, options)
17
-
18
- id_locks(cls).each do |lock|
19
- if ability.can?(action, cls.new(_id: lock.subject_id), options)
20
- open_ids << [cls.to_s, lock.subject_id]
21
- else
22
- closed_ids << lock.subject_id
23
- end
24
- end
25
- end
26
-
27
- closed_classes_condition = { :_type.nin => closed_classes }
28
- open_ids_condition = { :_type.in => open_ids.map(&:first), :_id.in => open_ids.map(&:last) }
29
- closed_ids_condition = { :_id.nin => closed_ids }
30
- or_conditions = [ closed_classes_condition, open_ids_condition ].reject(&:blank?)
31
-
32
- base_criteria.where( :$and => [ { :$or => or_conditions }, closed_ids_condition ])
10
+ or_conditions = [closed_types_condition, open_ids_condition].reject(&:blank?)
11
+ base_class.criteria.where(:$and => [{ :$or => or_conditions }, closed_ids_condition])
33
12
  end
34
13
 
35
14
  private # =============================================================
36
15
 
37
- def base_criteria
38
- @base_criteria ||= base_class.criteria
39
- end
40
-
41
- # ---------------------------------------------------------------------
42
-
43
- def base_class_superclass
44
- @base_class_superclass ||= (base_class.ancestors_with_default_locks.last || base_class)
16
+ def closed_types_condition
17
+ { :_type.nin => values.closed_types }
45
18
  end
46
19
 
47
- def default_lock(_cls, action)
48
- base_class_superclass.default_locks.detect { |l| l.action.to_s == action.to_s }
20
+ def open_ids_condition
21
+ {
22
+ :_type.in => values.open_types_and_ids.map(&:type),
23
+ :_id.in => values.open_types_and_ids.map(&:id)
24
+ }
49
25
  end
50
26
 
51
- def base_class_and_descendants
52
- @base_class_and_descendants ||= [base_class].concat(base_class.descendants)
27
+ def closed_ids_condition
28
+ { :_id.nin => values.closed_ids }
53
29
  end
54
30
 
55
31
  # ---------------------------------------------------------------------
56
32
 
57
- def owner
58
- @owner ||= ability.owner
59
- end
60
-
61
- def inherited_from_relation
62
- return unless owner.respond_to?(owner.class.inherit_from_relation_name)
63
- owner.inherit_from_relation
33
+ def values
34
+ @values ||= MongoidAbility::ValuesForAccessibleQuery.call(base_class, ability, action, options)
64
35
  end
65
36
 
66
37
  # ---------------------------------------------------------------------
67
38
 
68
- def id_locks(cls)
69
- (Array(owner_id_locks_for_subject_type(cls)) + Array(inherited_from_relation_ids_locks_for_subject_type(cls))).flatten
70
- end
71
-
72
- def owner_id_locks_for_subject_type(cls)
73
- @owner_id_locks_for_subject_type ||= {}
74
- @owner_id_locks_for_subject_type[cls] ||= owner.locks_relation.id_locks.for_action(action).for_subject_type(cls.to_s)
39
+ def base_class_superclass
40
+ @base_class_superclass ||= (base_class.ancestors_with_default_locks.last || base_class)
75
41
  end
76
42
 
77
- def inherited_from_relation_ids_locks_for_subject_type(cls)
78
- return [] unless inherited_from_relation
79
- @inherited_from_relation_ids_locks_for_subject_type ||= {}
80
- @inherited_from_relation_ids_locks_for_subject_type[cls] ||= inherited_from_relation.collect do |o|
81
- o.locks_relation.id_locks.for_action(action).for_subject_type(cls.to_s)
82
- end.flatten
43
+ def default_lock(_cls, action)
44
+ base_class_superclass.default_locks.detect { |l| l.action.to_s == action.to_s }
83
45
  end
84
46
  end
85
47
  end
@@ -18,7 +18,12 @@ module MongoidAbility
18
18
  scope :for_action, -> (action) { where(action: action.to_sym) }
19
19
 
20
20
  scope :for_subject_type, -> (subject_type) { where(subject_type: subject_type.to_s) }
21
- scope :for_subject_id, -> (subject_id) { where(subject_id: subject_id.presence) }
21
+
22
+ scope :for_subject_id, -> (subject_id) {
23
+ return where(subject_id: nil) unless subject_id.present?
24
+ where(subject_id: BSON::ObjectId.from_string(subject_id))
25
+ }
26
+
22
27
  scope :for_subject, -> (subject) {
23
28
  return where(subject_id: nil) unless subject.present?
24
29
  where(subject_type: subject.class.model_name, subject_id: subject.id)
@@ -0,0 +1,70 @@
1
+ module MongoidAbility
2
+ class ValuesForAccessibleQuery < Struct.new(:base_class, :ability, :action, :options)
3
+ def self.call(*args)
4
+ new(*args).call
5
+ end
6
+
7
+ # =====================================================================
8
+
9
+ def call
10
+ closed_types = [] # [cls]
11
+ open_types_and_ids = [] # OpenStruct.new(type: …, id: …)
12
+ closed_ids = [] # [id]
13
+
14
+ base_class_and_descendants.each do |cls|
15
+ closed_types << cls.to_s if ability.cannot?(action, cls, options)
16
+ id_locks(cls).each do |lock|
17
+ if ability.can?(action, cls.new(_id: lock.subject_id), options)
18
+ open_types_and_ids << OpenStruct.new(type: cls.to_s, id: lock.subject_id)
19
+ else
20
+ closed_ids << lock.subject_id
21
+ end
22
+ end
23
+ end
24
+
25
+ OpenStruct.new(
26
+ closed_types: closed_types,
27
+ open_types_and_ids: open_types_and_ids,
28
+ closed_ids: closed_ids
29
+ )
30
+ end
31
+
32
+ private # =============================================================
33
+
34
+ def base_class_and_descendants
35
+ @base_class_and_descendants ||= [base_class].concat(base_class.descendants)
36
+ end
37
+
38
+ # ---------------------------------------------------------------------
39
+
40
+ def id_locks(cls)
41
+ (Array(owner_id_locks_for_subject_type(cls)) + Array(inherited_from_relation_ids_locks_for_subject_type(cls))).flatten
42
+ end
43
+
44
+ def owner_id_locks_for_subject_type(cls)
45
+ @owner_id_locks_for_subject_type ||= {}
46
+ @owner_id_locks_for_subject_type[cls] ||= owner.locks_relation.id_locks.for_action(action).for_subject_type(cls.to_s)
47
+ end
48
+
49
+ def inherited_from_relation_ids_locks_for_subject_type(cls)
50
+ return [] unless inherited_from_relation
51
+ @inherited_from_relation_ids_locks_for_subject_type ||= {}
52
+ @inherited_from_relation_ids_locks_for_subject_type[cls] ||= inherited_from_relation.collect do |o|
53
+ o.locks_relation.id_locks.for_action(action).for_subject_type(cls.to_s)
54
+ end.flatten
55
+ end
56
+
57
+ # ---------------------------------------------------------------------
58
+
59
+ def owner
60
+ @owner ||= ability.owner
61
+ end
62
+
63
+ # ---------------------------------------------------------------------
64
+
65
+ def inherited_from_relation
66
+ return unless owner.respond_to?(owner.class.inherit_from_relation_name)
67
+ owner.inherit_from_relation
68
+ end
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module MongoidAbility
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  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.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomáš Celizna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-26 00:00:00.000000000 Z
11
+ date: 2016-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancancan
@@ -162,6 +162,7 @@ files:
162
162
  - lib/mongoid_ability/resolve_locks.rb
163
163
  - lib/mongoid_ability/resolve_owner_locks.rb
164
164
  - lib/mongoid_ability/subject.rb
165
+ - lib/mongoid_ability/values_for_accessible_query.rb
165
166
  - lib/mongoid_ability/version.rb
166
167
  - mongoid_ability.gemspec
167
168
  - test/mongoid_ability/ability_role_test.rb