verbose_cancancan 0.0.2 → 0.0.4

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: 894cfad8bd3a07f8455bd7d37720b3289123db0afb9c6e66dda92dbdc775bad5
4
- data.tar.gz: eab446beeb6ba948ab8959127bb20ba2abd2c9091593c583f422678334d1d6cd
3
+ metadata.gz: 16f2ffacc1c88314db8618166040f6032cea28576450dc2854e4316834fb3e28
4
+ data.tar.gz: '0487e37445d759e16c7aad9e6f1eead08f40411d392c39d65d358a3b884fc1d9'
5
5
  SHA512:
6
- metadata.gz: d12fd5faddc6ef40bcadee9fe600dd548ea12224b8220160d7ee6f756ad64c84e54787b848396620a841a4b06ad7a845ac5d58875e71a823bda8258efc30e41c
7
- data.tar.gz: d1c5b4cdaeaa2a178c033a1f6268b703ef9f05f84a0d2f3625adfddb8ffc0b78a88908da9415841486a2c11497cd311620b3e383e716d1ba811287ff57581921
6
+ metadata.gz: '068b7555d874f6e01e2244b931a4370213ab0789cc44cc9eb1dd5d838fa6f6e31549c63d27cdc78274ff46c47c36e57b4d9359becc212a0afece3525b1671ecf'
7
+ data.tar.gz: '06794b457710e86b80c5e7b6619624bf49e045f213efdcca9a1a1481dfd1a0dda7b76d0e81127de3b9da6942155f7e5f2a55780a8d4bae8e7c58a6a5e996e574'
@@ -1,7 +1,11 @@
1
1
  module VerboseCancancan
2
+ module Ability
2
3
 
4
+ extend ActiveSupport::Concern
3
5
 
4
- module Ability
6
+ prepended do
7
+ delegate :colourize, :logger, to: VerboseCancancan, prefix: :verb
8
+ end
5
9
 
6
10
  def can?(action, subject, attribute = nil, *extra_args)
7
11
  match = extract_subjects(subject).lazy.map do |a_subject|
@@ -15,17 +19,16 @@ module VerboseCancancan
15
19
  match ? match.base_behavior : false
16
20
  end
17
21
 
18
- # Only logged the rule if authorize! or load_and_authorize! is called, to avoid cluttering the terminal with can? checks.
22
+ # Only logged the rule if authorize! or load_and_authorize! is called, to avoid cluttering the terminal due to can? checks.
19
23
  def log_rule? = caller.find { |path| path.include?('authorize!') }
20
24
 
21
-
22
25
  def log_rule(matched_rule, action, subject)
23
26
  subject = pretty_subject(subject)
24
27
 
25
28
  if matched_rule
26
- VerboseCancancan.logger.info matched_rule_info(matched_rule, subject, action)
29
+ verb_logger.info matched_rule_info(matched_rule, subject, action)
27
30
  else
28
- VerboseCancancan.logger.info no_rule_matched(subject, action)
31
+ verb_logger.info no_rule_matched(subject, action)
29
32
  end
30
33
  end
31
34
 
@@ -33,32 +36,32 @@ module VerboseCancancan
33
36
  auth_status = rule.base_behavior ? auth_success : auth_failed
34
37
  rule_type = rule.base_behavior ? 'Can' : 'Cannot'
35
38
 
36
- "#{auth_status} #{rule_type} Rule applied for #{colourize(subject, :cyan)} (Action: #{colourize(action, :yellow)})\n ↳ #{rule.path}"
39
+ "#{auth_status} Rule of #{rule_type} type applied for #{verb_colourize(subject, :magenta)} (Action: #{verb_colourize(action, :green)})\n ↳ #{rule.path}"
37
40
  end
38
41
 
39
42
  def no_rule_matched(subject, action)
40
- "#{auth_failed} No matching rule for #{colourize(subject, :cyan)} (Action: #{colourize(action, :yellow)})"
43
+ "#{auth_failed} No matching rule for #{verb_colourize(subject, :magenta)} (Action: #{verb_colourize(action, :green)})"
41
44
  end
42
45
 
43
46
  def pretty_subject(subject)
44
47
  if subject.is_a?(ActiveRecord::Base)
48
+ # In case of member actions subject is `ActiveRecord::Base`
49
+
45
50
  "#{subject.class.name} (ID: #{subject.id})"
46
51
  else
52
+ # In case of Collection actions subject can be
53
+ # 1. Hash if resources are being loaded through parent. => { parent_resource => resource_class }
54
+ # 2. ActiveRecord::Model if if resources are not loaded through parent.
55
+
47
56
  orignal_subject = subject.is_a?(Hash) ? subject.values.first : subject
48
57
 
49
58
  orignal_subject.respond_to?(:name) ? orignal_subject.name : orignal_subject
50
59
  end
51
60
  end
52
61
 
53
- def auth_failed
54
- colourize('[❌ AUTHORIZATION FAILED]', :red)
55
- end
56
-
57
- def auth_success
58
- colourize('[✅ AUTHORIZATION SUCCESS]', :green)
59
- end
62
+ def auth_failed = verb_colourize('[❌ AUTHORIZATION FAILED]', :red)
60
63
 
61
- def colourize(...) = VerboseCancancan.colourize(...)
64
+ def auth_success = verb_colourize('[✅ AUTHORIZATION SUCCESS]', :green)
62
65
 
63
66
  end
64
67
  end
@@ -1,15 +1,23 @@
1
1
  module VerboseCancancan
2
2
  module ControllerResource
3
3
 
4
+ extend ActiveSupport::Concern
5
+
6
+ prepended do
7
+ delegate :cleaner_backtrace, :logger, :colourize, to: VerboseCancancan, prefix: :verb
8
+ end
9
+
4
10
  def add_before_action(controller_class, method, *args)
5
11
  options = args.extract_options!
6
12
  resource_name = args.first
7
13
  before_action_method = before_callback_name(options)
8
- bk_trace = VerboseCancancan.cleaner_backtrace(caller).first
14
+ bk_trace = verb_cleaner_backtrace(caller).first
9
15
 
10
16
  controller_class.send(before_action_method, options.slice(:only, :except, :if, :unless)) do |controller|
11
17
 
12
- VerboseCancancan::ControllerResource.log_hook_added(method, controller_class, bk_trace) if VerboseCancancan::ControllerResource.log_hook_added?(method)
18
+ controller.class
19
+ .cancan_resource_class
20
+ .log_hook_added(method, controller_class, resource_name, bk_trace)
13
21
 
14
22
 
15
23
  controller.class.cancan_resource_class
@@ -17,15 +25,15 @@ module VerboseCancancan
17
25
  end
18
26
  end
19
27
 
20
- def self.log_hook_added?(hook)
21
- hook.to_s.include? ('authorize_resource')
22
- end
28
+ def log_hook_added(method, controller_class, resource_name, bk_trace)
29
+ return if method.to_s.exclude? 'authorize_resource'
23
30
 
24
- def self.log_hook_added(method, controller_class, bk_trace)
25
- VerboseCancancan.logger.info "#{VerboseCancancan::ControllerResource.colourize('[⏳ CanCan Hook Added]', :green)}(#{method}) on #{controller_class} \n ↳ #{bk_trace}"
26
- end
31
+ log_msg = [verb_colourize('[⏳ CanCan Hook Added]', :orange), "(#{method})"]
32
+ log_msg << "for #{resource_name}" if resource_name
33
+ log_msg += ["on #{controller_class}", "\n ↳ #{bk_trace}"]
27
34
 
28
- def self.colourize(...) = VerboseCancancan.colourize(...)
35
+ verb_logger.info log_msg.join(' ')
36
+ end
29
37
 
30
38
  end
31
39
  end
@@ -1,15 +1,17 @@
1
+ require 'verbose_cancancan/ability'
2
+ require 'verbose_cancancan/rule'
3
+ require 'verbose_cancancan/controller_resource'
4
+
1
5
  module VerboseCancancan
6
+
2
7
  class Railtie < Rails::Railtie
3
- initializer "my_gem.configure_rails_initialization" do
4
- # some initialization behavior
5
- puts "\n\n\nhola\n\n\n"
6
8
 
9
+ initializer 'verbose_cancancan.patch_cancancan' do
7
10
  CanCan::Rule.prepend(Rule)
8
11
  CanCan::Ability.prepend(Ability)
9
12
 
10
13
  CanCan::ControllerResource.singleton_class.prepend(VerboseCancancan::ControllerResource)
11
-
12
-
13
14
  end
15
+
14
16
  end
15
17
  end
@@ -1,10 +1,16 @@
1
1
  module VerboseCancancan
2
-
3
2
  module Rule
3
+ extend ActiveSupport::Concern
4
+
5
+ prepended do
6
+ attr_reader :path
7
+
8
+ delegate :cleaner_backtrace, to: VerboseCancancan, prefix: :verb
9
+ end
10
+
4
11
  def initialize(...)
5
- @path = VerboseCancancan.cleaner_backtrace(caller).first
12
+ @path = verb_cleaner_backtrace(caller).first
6
13
  super
7
14
  end
8
15
  end
9
-
10
16
  end
@@ -1,6 +1,3 @@
1
- require 'verbose_cancancan/ability'
2
- require 'verbose_cancancan/rule'
3
- require 'verbose_cancancan/controller_resource'
4
1
  require 'verbose_cancancan/railtie'
5
2
 
6
3
  module VerboseCancancan
@@ -8,9 +5,9 @@ module VerboseCancancan
8
5
  class << self
9
6
 
10
7
  def logger
11
- logger = Logger.new(STDOUT)
8
+ logger = Rails.logger
12
9
 
13
- logger.formatter = proc { |_, _, _, msg| "#{msg}\n\n" }
10
+ # logger.formatter = proc { |_, _, _, msg| "#{msg}\n\n" }
14
11
 
15
12
  logger
16
13
  end
@@ -23,13 +20,16 @@ module VerboseCancancan
23
20
  bk_trace.clean(locations)
24
21
  end
25
22
 
23
+ # TODO: Refactor Coloring approch.
26
24
  def colourize(msg, color)
27
25
  colors = {
28
- clear: "\e[0m",
29
- red: "\e[1;31m",
30
- green: "\e[1;92m",
31
- yellow: "\e[33m",
32
- cyan: "\e[36m"
26
+ clear: "\e[0m",
27
+ red: "\e[1;31m",
28
+ green: "\e[1;92m",
29
+ magenta: "\e[1;95m",
30
+ yellow: "\e[1;93m",
31
+ orange: "\e[1;38;5;214m",
32
+ cyan: "\e[1;96m"
33
33
  }
34
34
 
35
35
  "#{colors[color]}#{msg}#{colors[:clear]}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verbose_cancancan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zain Iftikhar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-11 00:00:00.000000000 Z
11
+ date: 2025-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancancan
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '6.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '6.0'
27
55
  description: 'This gem displays the location where a rule was defined and prints the
28
56
  locations where load_and_authorize_resource and authorize! hooks are defined.
29
57