controller_policies 1.1.0 → 1.1.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
  SHA256:
3
- metadata.gz: 876b65cbe173231e141e578fd2e650dcbbcbbba02b0ece615c3ddf80395ba96f
4
- data.tar.gz: bcf69abce9066f2b99fa99d21481ecca033e2408d2ed0ec752f2b5cf080a482f
3
+ metadata.gz: 85d80d00d5bb31de269ac2190cd44a77e9d2aa1623aa7d6e00cf86de12565baa
4
+ data.tar.gz: ec23ef9100a962f17386693889903182f84cbb247c9dc275da54cec789acc445
5
5
  SHA512:
6
- metadata.gz: 8031665a97f07ab1e47534f38376eb4b5170538ffa9da42bc69d7e6c2de847e855d64acfdc2db32bbe5fbaee6d1f709e4be147fba9912e39c0089d3d72cecd24
7
- data.tar.gz: 2b30ca3aae95937a5625268853a9caa5697c460f1b24b7cffa5dc8dd875d5dee6005e367fb8029627227f7829b808c5f1485914869159e18e9d6545dccc1682f
6
+ metadata.gz: 9ec83f851cc09988e4664c404b2e99b2a8c48e954f3804652fb0477f66545d75b12887261e4b72c265da16dd3c8ce277e5fdcf6d170eea535b7dfe07c53aa2e8
7
+ data.tar.gz: 6afa4ececa6d5c04164eafc5c66a578290d6ff3edfcffff76260f6bb645450000b2177d33b49a9d3105eb8f6463a0cda3902a7caee3baa4acfa665b0395e9f39
data/lib/ability.rb CHANGED
@@ -34,41 +34,28 @@ class Ability
34
34
 
35
35
  # Filter abilities based on namespace.
36
36
  def where(*queries)
37
- results = []
38
- queries.each do |query|
39
- case query.class.to_s
40
- when 'String'
41
- # Handle empty string or root namespace
42
- if query.empty? || query == '/'
43
- results += all.select { |ability| ability.namespace == Policies }
44
- else
45
- results += all.select { |ability| ability.namespace.to_s == "Policies::#{trim(query).camelize}" }
46
- end
47
- when 'Module', 'Class'
48
- results += all.select { |ability| ability.namespace == query }
37
+ queries.flat_map do |query|
38
+ case query
39
+ when String
40
+ filter_by_string_query(query)
41
+ when Module, Class
42
+ all.select { |ability| ability.namespace == query }
43
+ else
44
+ []
49
45
  end
50
46
  end
51
- results
52
47
  end
53
48
 
54
- # Find an ability within a namespace.
55
- # def find(query_string)
56
- # where(query_string).first
57
- # end
58
-
59
49
  # Match abilities based on a matching string or regex. The matcher is based on the namespace.
60
50
  def match(expression)
61
- case expression.class.to_s
62
- when 'String' then all.select { |ability| ability.namespace.to_s.match?(/#{trim(expression).camelize}/) }
63
- when 'Regexp' then regex_matcher(expression)
51
+ case expression
52
+ when String
53
+ all.select { |ability| ability.namespace.to_s.match?(/#{trim(expression).camelize}/) }
54
+ when Regexp
55
+ regex_matcher(expression)
64
56
  end
65
57
  end
66
58
 
67
- # Find an ability based on a matching string or regex. The matcher is based on the namespace.
68
- # def mill(expression)
69
- # match(expression).first
70
- # end
71
-
72
59
  # Path to the policy folder.
73
60
  def policy_path
74
61
  @policy_path ||= Rails.root.join('app/policies')
@@ -76,6 +63,15 @@ class Ability
76
63
 
77
64
  private
78
65
 
66
+ def filter_by_string_query(query)
67
+ if query.empty? || query == '/'
68
+ all.select { |ability| ability.namespace == Policies }
69
+ else
70
+ target_namespace = "Policies::#{trim(query).camelize}"
71
+ all.select { |ability| ability.namespace.to_s == target_namespace }
72
+ end
73
+ end
74
+
79
75
  def definitions
80
76
  @definitions ||= begin
81
77
  data = []
@@ -88,19 +84,17 @@ class Ability
88
84
 
89
85
  def definition_files_post_processing(file_path)
90
86
  namespace_path = convert_namespace(file_path)
87
+ module_constant = resolve_module_constant(namespace_path)
91
88
 
92
- if namespace_path.empty?
93
- # Handle root-level definitions.rb file
94
- module_constant = Policies
95
- else
96
- module_constant = "Policies::#{namespace_path}".constantize
89
+ module_constant::DEFINITIONS.map do |policy_definition|
90
+ policy_definition.merge(namespace: module_constant)
97
91
  end
92
+ end
98
93
 
99
- policy_definitions = module_constant::DEFINITIONS
100
- policy_definitions.map do |policy_definition|
101
- policy_definition[:namespace] = module_constant
102
- policy_definition
103
- end
94
+ def resolve_module_constant(namespace_path)
95
+ return Policies if namespace_path.empty?
96
+
97
+ "Policies::#{namespace_path}".constantize
104
98
  end
105
99
 
106
100
  def regex_matcher(expression)
@@ -35,19 +35,22 @@ module ControllerPolicies
35
35
  # If it finds a match, it will be an applicable definition to check abilities for.
36
36
  def applicable_abilities
37
37
  @applicable_abilities ||= begin
38
- # Get abilities for the current controller namespace, or root if no namespace
39
- namespace_abilities = if controller_namespace.empty?
40
- Ability.where('') # This will get root-level policies
41
- else
42
- Ability.where(controller_namespace)
43
- end
44
-
45
- namespace_abilities.select do |ability|
46
- matching_actions = ability.actions.select { |action| /#{action}/.match?("#{controller_path}##{action_name}") }
47
- matching_actions.present?
48
- end
38
+ namespace_abilities = fetch_namespace_abilities
39
+ namespace_abilities.select { |ability| ability_matches_current_action?(ability) }
49
40
  end
50
41
  end
42
+
43
+ # Get abilities for the current controller namespace, or root if no namespace
44
+ def fetch_namespace_abilities
45
+ namespace = controller_namespace.empty? ? '' : controller_namespace
46
+ Ability.where(namespace)
47
+ end
48
+
49
+ # Check if an ability matches the current controller action
50
+ def ability_matches_current_action?(ability)
51
+ current_controller_action = "#{controller_path}##{action_name}"
52
+ ability.actions.any? { |action| /#{action}/.match?(current_controller_action) }
53
+ end
51
54
  end
52
55
  end
53
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ControllerPolicies
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: controller_policies
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tien