can_camel 0.3.0 → 0.3.1

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: b7327da083e2094e5ec46012cd8d677fa1d580a3
4
- data.tar.gz: de42690b702c3cf0be312f3a284efdd7d2a8b9da
3
+ metadata.gz: 597db731eda1993d1204d7735b945635f5200f7d
4
+ data.tar.gz: b9dd5fb704e2a6ccecd98c4bb2d36c09d458ac22
5
5
  SHA512:
6
- metadata.gz: d990335cb5d604df985bb522370aabe0ac0750e516a51570c9d1485309bfec14acad9d254cc450e02f570766b01311db060fe682da20a353f3fd53a41fbda428
7
- data.tar.gz: d43fd37f23f2cdcfcf28e63281b312ab7d9e714e79412a78a3d90547261e256c5886fc08a5c5e648719d753f8f8d94beae5bc02fad7c56e2207291b8ad878cf4
6
+ metadata.gz: 36d14e9c45ad4a068544834930f1431cf0a9b349ca0de18fdaa4e7ec4c013cbeabb32b076df349c84a3d5750971617cdcc49c2434afb1b16fe2e37b774aa776e
7
+ data.tar.gz: 29e688f12ddf23d6a5a41e415420d1fb78252050241b899917c31ae5c9d89031a1ca5f09721e4d203867efa6e7839b6ac4d5cdfe3b0eb406b8c27411fb9b4ffd
@@ -1,7 +1,7 @@
1
1
  module CanCamel
2
2
  extend ActiveSupport::Autoload
3
3
  %i(Base Node SubjectNode ActionNode GroupNode ConcernNode Linter Filters
4
- Validators Cache Filter)
4
+ Validators Cache Filter Root)
5
5
  .each { |x| autoload(x) }
6
6
 
7
7
  extend CanCamel::Base
@@ -1,7 +1,6 @@
1
1
  module CanCamel
2
2
  class ActionNode < Node
3
3
  has_many :groups, foreign_key: :parent_id, class_name: "CanCamel::GroupNode"
4
- validates_presence_of :parent_id
5
4
 
6
5
  def rank
7
6
  2
@@ -8,8 +8,8 @@ module CanCamel
8
8
  # @param action [Symbol] symbol relied to action node name
9
9
  # @param subject [Symbol] symbol relied to subject node name
10
10
  # @return [Hash, nil] hash with specific params or nil
11
- def can?(user, action, subject, **args)
12
- Cache[[user.send(GROUP_METHOD).to_sym, action, subject]]
11
+ def can?(user, *path, **args)
12
+ Cache[[user.send(GROUP_METHOD).to_sym, *path]]
13
13
  .try(:can?, user: user, **args)
14
14
  end
15
15
 
@@ -9,6 +9,7 @@ module CanCamel
9
9
  end
10
10
 
11
11
  def children_of(node)
12
+ return [] unless node
12
13
  cache.select do |path, _value|
13
14
  _, *subpath = path
14
15
  node.path == subpath
@@ -17,8 +18,16 @@ module CanCamel
17
18
 
18
19
  def append(node, path)
19
20
  path = [node.name, *path]
20
- @cache[path] = node
21
- children_of(node).each { |child| append child, path }
21
+ old = @cache[path]
22
+
23
+ if old
24
+ old.inherit! node
25
+ else
26
+ new = node.dup
27
+ new.path = path
28
+ @cache[path] = new
29
+ children_of(node).each { |child| append child, path }
30
+ end
22
31
  end
23
32
 
24
33
  private
@@ -37,7 +46,7 @@ module CanCamel
37
46
  def build_cache
38
47
  Node.all.each_with_object({}) do |node, object|
39
48
  object[node.path] = node
40
- end
49
+ end.merge([] => Root.new)
41
50
  end
42
51
 
43
52
  def inherit_all!
@@ -1,9 +1,9 @@
1
1
  module CanCamel
2
2
  class ConcernNode < Node
3
3
  validates :parent_id, absence: true
4
- end
5
4
 
6
- def rank
7
- 0
5
+ def rank
6
+ 0
7
+ end
8
8
  end
9
9
  end
@@ -1,7 +1,5 @@
1
1
  module CanCamel
2
2
  class GroupNode < Node
3
- validates_presence_of :parent_id
4
-
5
3
  def can?(**additional_params)
6
4
  condition.each_with_object(result.dup.merge(additional_params)) do |(method, args), result|
7
5
  args = args.symbolize_keys
@@ -30,15 +30,16 @@ module CanCamel
30
30
  end
31
31
 
32
32
  def inherit!(source = nil)
33
- return if inherited
34
-
35
33
  raise 'inherit nodes only after cache built' unless Cache[path]
34
+ inherit_source! source
35
+
36
+ return if inherited
36
37
 
37
38
  @inherited = true
38
- return inherit_source! source if source
39
39
 
40
40
  inherit_source! cached_parent
41
41
  inherit_source! cached_inherited_node
42
+ # inherit_source! cached_uncle
42
43
  end
43
44
 
44
45
  validates! :inherited, absence: { message: "You should not save inherited nodes!" }
@@ -51,6 +52,10 @@ module CanCamel
51
52
  super.symbolize_keys
52
53
  end
53
54
 
55
+ def cached_uncle
56
+ Cache.children_of(cached_parent.cached_parent).select { |x| x.name == name }.first
57
+ end
58
+
54
59
  attr_writer :path
55
60
 
56
61
  def path
@@ -61,22 +66,33 @@ module CanCamel
61
66
 
62
67
  attr_reader :inherited
63
68
 
64
- def inherit_source!(source)
65
- return unless source
66
-
67
- source.inherit!
68
-
69
+ def inherit_neighbours!(source)
69
70
  Cache.children_of(source).each do |child|
70
- next unless child.rank > rank
71
- next if child == self
72
- Cache.append child, path
71
+ Cache.append child, path if child.rank > rank
73
72
  end
73
+ end
74
74
 
75
+ def inherit_fields!(source)
75
76
  inherit_field(source, :description) { |x| self.description ||= x }
76
77
  inherit_field(source, :result) { |x| result.merge!(x) { |_k, l, _r| l } }
77
78
  inherit_field(source, :condition) { |x| self.condition = x.merge(condition) }
78
79
  end
79
80
 
81
+ def inherit_source!(source)
82
+ return unless source
83
+
84
+ source.inherit!
85
+
86
+ # WTF: if we inherit neighbours only once, it fails. Two times should be fine enought,
87
+ # but we have to do find a way to understand when it enought in code
88
+ # TODO: Refactor
89
+ # FIXME: Refactor
90
+ inherit_neighbours!(source)
91
+ inherit_neighbours!(source)
92
+
93
+ inherit_fields!(source) unless source.is_a? Root
94
+ end
95
+
80
96
  def inherit_field(source, field)
81
97
  return if override_fields.include?(field.to_s)
82
98
  yield source.send(field)
@@ -0,0 +1,17 @@
1
+ class Root
2
+ def inherit!; end
3
+
4
+ def cached_parent; end
5
+
6
+ def path
7
+ []
8
+ end
9
+
10
+ def rank
11
+ 0
12
+ end
13
+
14
+ def name
15
+ :root
16
+ end
17
+ end
@@ -1,7 +1,5 @@
1
1
  module CanCamel
2
2
  class SubjectNode < Node
3
- validates :parent_id, absence: true
4
-
5
3
  def rank
6
4
  1
7
5
  end
@@ -1,3 +1,3 @@
1
1
  module CanCamel
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: can_camel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Smirnov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-18 00:00:00.000000000 Z
11
+ date: 2015-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4'
27
- - !ruby/object:Gem::Dependency
28
- name: pg
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  description: |2
42
28
  CanCamel allows to manage access levels with all the power of cancan style (saving semantics). It means, that you can use `can?` method anywhere you want to check privileges of a selected user to access something in some way. See readme file at https://github.com/JelF/can_camel/blob/master/README.md to learn more
43
29
  email:
@@ -63,6 +49,7 @@ files:
63
49
  - lib/can_camel/group_node.rb
64
50
  - lib/can_camel/linter.rb
65
51
  - lib/can_camel/node.rb
52
+ - lib/can_camel/root.rb
66
53
  - lib/can_camel/subject_node.rb
67
54
  - lib/can_camel/validators.rb
68
55
  - lib/can_camel/validators/general_validators.rb