partial_inspector 1.0.0 → 2.0.0

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: 4da9f1dbf7e24e31a2ff0861e6a953077a40c3b8ddf8c32943b25b0dea006bac
4
- data.tar.gz: be3e444cc43d7b41bb33806c426adeff4f155dce8d38273eac0a8241b8f52f84
3
+ metadata.gz: b01271288a1127081cd52384cec35879907aa6bf778d9c16c30760e37d5df700
4
+ data.tar.gz: 503dea0d0f53e11ddcab09d73f6eda69e6cb65dd52bbcbd9007c3d24e6ac016c
5
5
  SHA512:
6
- metadata.gz: e6fea5f0e342ae64204796a40d9e210b3fbc9d61634d837be9f3adc0065d29dbb6c7f3ba70ebc175da071e321f80d453381ab2f17dfe73beec19e40e2e36e634
7
- data.tar.gz: aab718cd76499900275cf18c41eba1ee46d6b5bf4d47648998d6b5bd73606265888bc22a8113805beedf32a2f7fc1dee58793422ea37f06d4256125c7e1a7584
6
+ metadata.gz: c9ac5c9677eb732664821aa681e1fae7d6a7e35817371fd3f6b4a69e585e26fd5feaf4ecc9376c5f66211399c988c38379016fb571bdec1a84b2cd6404ae771f
7
+ data.tar.gz: d48cd66554591f92771c0fb9f1bdb26ec808ff66fc2ff6b20f17ae341c2cded972e2f6262017779ff9f6c8321b66f01bc5fadf4783300a73bcd515033b9ef532
data/README.md CHANGED
@@ -34,6 +34,10 @@ gem install partial_inspector
34
34
  ```
35
35
  PartialInspector.scanner.scan_unused_partials
36
36
  ```
37
+ 2. Example Output
38
+
39
+ <img width="606" height="263" alt="image" src="https://github.com/user-attachments/assets/f7aef983-be65-4167-9703-810f29631a95" />
40
+
37
41
 
38
42
 
39
43
 
@@ -0,0 +1,70 @@
1
+ require_relative 'utils'
2
+
3
+ module PartialInspector
4
+ module PartialTree
5
+ include PartialInspector::Utils
6
+
7
+ private
8
+ def data_to_build_tree(partial_path)
9
+ router(partial_path)
10
+ end
11
+
12
+ def router(path)
13
+ base_data = []
14
+ partial_path = path.split('/')[0]=='app' ? path.split('/')[2..-1].join('/') : path
15
+ files = find_base_files_rendering_partial(partial_path)
16
+ return [[path]] if files.empty?
17
+ files.each do |file|
18
+ if rendered_by_partial(file)
19
+ child_traces = router(file)
20
+ child_traces.each do |trace|
21
+ base_data << [path] + trace
22
+ end
23
+ else
24
+ base_data << [path, file]
25
+ end
26
+ end
27
+ base_data
28
+ end
29
+
30
+ def find_base_files_rendering_partial(partial_path)
31
+ return [] if partial_path.nil? || partial_path == ''
32
+ base_files = []
33
+
34
+ files = Dir.glob("app/**/*.{rb,html.erb,js.erb,turbo_stream.erb}")
35
+ files.each do |file|
36
+ file_content = File.readlines(file)
37
+ file_content.each_with_index do |line, index|
38
+ if (line.include?('render') || line.include?('partial:')) && check_partial_exists(line, partial_path)
39
+ base_files << file
40
+ end
41
+ end
42
+ end
43
+
44
+ partial_dir = build_partial_base_dir_path(partial_path)
45
+ partial_name = extract_partial_name(partial_path)
46
+
47
+ base_files + find_base_files_rendering_partial_against_partial_name(partial_dir, partial_name)
48
+ end
49
+
50
+
51
+ def find_base_files_rendering_partial_against_partial_name(partial_dir, partial_name)
52
+ base_files = []
53
+
54
+ Dir.glob("app/views/#{partial_dir}*.*.erb").each do |file|
55
+ file_content = File.readlines(file)
56
+ file_content.each_with_index do |line, index|
57
+ if line.include?('render') && check_partial_exists(line, partial_name)
58
+ base_files << file
59
+ end
60
+ end
61
+ end
62
+
63
+ base_files
64
+ end
65
+
66
+ def rendered_by_partial(file)
67
+ file.split('/').last[0] == '_'
68
+ end
69
+ end
70
+ end
@@ -1,10 +1,12 @@
1
1
  require_relative "used_partials"
2
2
  require_relative "unused_partials"
3
+ require_relative "partial_tree"
3
4
 
4
5
  module PartialInspector
5
6
  class Reporter
6
7
  include PartialInspector::UsedPartials
7
8
  include PartialInspector::UnusedPartials
9
+ include PartialInspector::PartialTree
8
10
 
9
11
  def report_files_rendering_partial(partial_path)
10
12
  files = base_scanner(partial_path)
@@ -46,5 +48,20 @@ module PartialInspector
46
48
  end
47
49
  return
48
50
  end
51
+
52
+ def report_partial_tree(partial_path)
53
+ paths = data_to_build_tree(partial_path)
54
+ paths.each_with_index do |path, index|
55
+ puts "\e[35mTREE #{index+1}\e[0m"
56
+ space = ""
57
+ path[1..-1].each do |sub_path|
58
+ puts "#{space}-> #{sub_path}"
59
+ space = space + "\t"
60
+ end
61
+ space=""
62
+ puts "\n"
63
+ end
64
+ return
65
+ end
49
66
  end
50
67
  end
@@ -13,6 +13,11 @@ module PartialInspector
13
13
  return
14
14
  end
15
15
 
16
+ def inspect_partial_tree(partial_path)
17
+ puts reporter.report_partial_tree(partial_path)
18
+ return
19
+ end
20
+
16
21
  private
17
22
  def reporter
18
23
  PartialInspector::Reporter.new
@@ -29,32 +29,6 @@ module PartialInspector
29
29
  lines + check_partial_against_name(partial_dir, partial_name)
30
30
  end
31
31
 
32
- def check_partial_exists(line, partial)
33
- partial_path = ""
34
- line_content = line.split(' ')
35
- line_content.each_with_index do |value, index|
36
- if line.include?("render partial:") && value == "render"
37
- partial_path = line_content[index + 2]
38
- break
39
- elsif value == "render"
40
- partial_path = line_content[index + 1]
41
- break
42
- elsif value == "partial:"
43
- partial_path = line_content[index + 1]
44
- break
45
- end
46
- end
47
- if partial_path[0] == "\""
48
- partial_path = partial_path[1..-2]
49
- partial_path = partial_path[0..-2] if partial_path[-1] == '"'
50
- elsif partial_path[0] == "'"
51
- partial_path = partial_path[1..-2]
52
- partial_path = partial_path[0..-2] if partial_path[-1] == "'"
53
- end
54
-
55
- partial_path == partial
56
- end
57
-
58
32
  def build_path(path_components)
59
33
  path = ''
60
34
  path_components.each do |path_component|
@@ -24,5 +24,31 @@ module PartialInspector
24
24
 
25
25
  path
26
26
  end
27
+
28
+ def check_partial_exists(line, partial)
29
+ partial_path = ""
30
+ line_content = line.split(' ')
31
+ line_content.each_with_index do |value, index|
32
+ if line.include?("render partial:") && value == "render"
33
+ partial_path = line_content[index + 2]
34
+ break
35
+ elsif value == "render"
36
+ partial_path = line_content[index + 1]
37
+ break
38
+ elsif value == "partial:"
39
+ partial_path = line_content[index + 1]
40
+ break
41
+ end
42
+ end
43
+ if partial_path[0] == "\""
44
+ partial_path = partial_path[1..-2]
45
+ partial_path = partial_path[0..-2] if partial_path[-1] == '"'
46
+ elsif partial_path[0] == "'"
47
+ partial_path = partial_path[1..-2]
48
+ partial_path = partial_path[0..-2] if partial_path[-1] == "'"
49
+ end
50
+
51
+ partial_path == partial
52
+ end
27
53
  end
28
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PartialInspector
4
- VERSION = "1.0.0"
4
+ VERSION = "2.0.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: partial_inspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SalihaShahid
@@ -18,6 +18,7 @@ files:
18
18
  - README.md
19
19
  - Rakefile
20
20
  - lib/partial_inspector.rb
21
+ - lib/partial_inspector/partial_tree.rb
21
22
  - lib/partial_inspector/reporter.rb
22
23
  - lib/partial_inspector/scanner.rb
23
24
  - lib/partial_inspector/unused_partials.rb