partial_inspector 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ec65cfb72a02b4e0b240170e1bb32a10ac0e08fe42d3861cf71c5f43562d6b19
4
+ data.tar.gz: 81fedb93a828b9048a5f9446524a95dd99b964ea38cfb17af10fd2d9c2f94619
5
+ SHA512:
6
+ metadata.gz: 448eb53a7316e2db1e5cd5475a3d1998e4f59408490b04e2d0de0ed3a32265dd65c3db62d396fea2c6b55df45ddf4cf019db7f4c46b2c884c0543ee757af39e8
7
+ data.tar.gz: b3da77f5daf4e6a71eb79042d3d1cf02f331df79a6a3cec248107f05ebad019fb5b637e5a1676c04b2c0f40efb54e912b99777a86dfbc6b0b7b95b1f5c3e1713
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # Partial Inspector
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,50 @@
1
+ require_relative "utils"
2
+
3
+ module PartialInspector
4
+ class Reporter
5
+ include PartialInspector::Utils
6
+
7
+ def report_files_rendering_partial(partial_path)
8
+ files = base_scanner(partial_path)
9
+ return "\e[41m\e[34mINVALID PATH\e[0m\e[0m" if files.empty?
10
+
11
+ search_results_count = files.size
12
+ grouped_files = combine_unique_files(files)
13
+
14
+ puts "\n\e[36mSEARCH SUMMARY\e[0m"
15
+ puts "\e[35mTOTAL SEARCH RESULTS\e[0m: \e[32m#{search_results_count}\e[0m"
16
+ puts "\e[35mTOTAL FILES\e[0m: \e[32m#{grouped_files.keys.size}\e[0m"
17
+
18
+ puts "\n\e[36mDETAILS\e[0m"
19
+ grouped_files.each do |key, value|
20
+ puts "\e[35mFILE NAME\e[0m: \e[34m#{key.to_s}\e[0m"
21
+ file_contents = value
22
+ file_contents.each do |file_content|
23
+ puts "\e[35mLINE #{file_content[:line_number]}\e[0m: #{file_content[:line_content]} "
24
+ end
25
+ puts "RENDERED \e[32m#{file_contents.size} TIME(S)\e[0m\n\n"
26
+ end
27
+ return
28
+ end
29
+
30
+ private
31
+ def combine_unique_files(files)
32
+ results = {}
33
+ files.each do |file|
34
+ same_files = files.filter { |f| f[:file] == file[:file] }
35
+
36
+ same_files.each do |same_file|
37
+ files.delete(same_file)
38
+ results[same_file[:file].to_sym] = [] if results[same_file[:file].to_sym] == nil
39
+ results[same_file[:file].to_sym] << { line_number: same_file[:line_number], line_content: file[:line_content] }
40
+ end
41
+ end
42
+
43
+ files.each do |file|
44
+ results[file[:file].to_sym] = [{ line_number: file[:line_number], line_content: file[:line_content] }]
45
+ end
46
+
47
+ results
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,16 @@
1
+ require_relative "reporter"
2
+
3
+ module PartialInspector
4
+ class Scanner
5
+
6
+ def inspect_files_rendering_partial(partial_path)
7
+ puts reporter.report_files_rendering_partial(partial_path)
8
+ return
9
+ end
10
+
11
+ private
12
+ def reporter
13
+ PartialInspector::Reporter.new
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,125 @@
1
+ module PartialInspector
2
+ module Utils
3
+ private
4
+ def base_scanner(partial_path)
5
+ return [] if partial_path.nil? || partial_path == ''
6
+ lines = []
7
+
8
+ files = Dir.glob("app/**/*.{rb,html.erb,js.erb,turbo_stream.erb}")
9
+ files.each do |file|
10
+ file_content = File.readlines(file)
11
+ file_content.each_with_index do |line, index|
12
+ if (line.include?('render') || line.include?('partial:')) && check_partial_exists(line, partial_path)
13
+ lines << {
14
+ file: file,
15
+ line_number: index + 1,
16
+ line_content: highlight_partial_form_content(partial_path, line.strip)
17
+ }
18
+ end
19
+ end
20
+ end
21
+
22
+ partial_dir = build_partial_base_dir_path(partial_path)
23
+ partial_name = extract_partial_name(partial_path)
24
+
25
+ lines + check_partial_against_name(partial_dir, partial_name)
26
+ end
27
+
28
+ def extract_partial_file_name(partial_path)
29
+ file_name = partial_path.split('/').last
30
+ file_name
31
+ end
32
+
33
+ def extract_partial_name(partial_path)
34
+ partial_name = partial_path.split('/').last
35
+ partial_name = partial_name.split('.').first
36
+ partial_name = partial_name[1..-1] if partial_name[0] == '_'
37
+ partial_name
38
+ end
39
+
40
+ def check_partial_exists(line, partial)
41
+ partial_path = ""
42
+ line_content = line.split(' ')
43
+ line_content.each_with_index do |value, index|
44
+ if line.include?("render partial:") && value == "render"
45
+ partial_path = line_content[index + 2]
46
+ break
47
+ elsif value == "render"
48
+ partial_path = line_content[index + 1]
49
+ break
50
+ elsif value == "partial:"
51
+ partial_path = line_content[index + 1]
52
+ break
53
+ end
54
+ end
55
+ if partial_path[0] == "\""
56
+ partial_path = partial_path[1..-2]
57
+ partial_path = partial_path[0..-2] if partial_path[-1] == '"'
58
+ elsif partial_path[0] == "'"
59
+ partial_path = partial_path[1..-2]
60
+ partial_path = partial_path[0..-2] if partial_path[-1] == "'"
61
+ end
62
+
63
+ partial_path == partial
64
+ end
65
+
66
+ def build_path(path_components)
67
+ path = ''
68
+ path_components.each do |path_component|
69
+ path = path+"#{path_component}/"
70
+ end
71
+
72
+ path[0..-2]
73
+ end
74
+
75
+ def build_partial_base_dir_path(partial_path)
76
+ path_components = partial_path.split('/')
77
+ path = ''
78
+ path_components = path_components[0..-2]
79
+ path_components.each do |path_component|
80
+ path = path + "#{path_component}/"
81
+ end
82
+
83
+ path
84
+ end
85
+
86
+ def check_partial_against_name(partial_dir, partial_name)
87
+ lines = []
88
+
89
+ Dir.glob("app/views/#{partial_dir}*.*.erb").each do |file|
90
+ file_content = File.readlines(file)
91
+ file_content.each_with_index do |line, index|
92
+ if line.include?('render') && check_partial_exists(line, partial_name)
93
+ lines << {
94
+ file: file,
95
+ line_number: index + 1,
96
+ line_content: highlight_partial_form_content(partial_name, line.strip)
97
+ }
98
+ end
99
+ end
100
+ end
101
+
102
+ lines
103
+ end
104
+
105
+ def highlight_partial_form_content(partial_path, content)
106
+ if content.include?("'#{partial_path}'")
107
+ content_components = content.split("'#{partial_path}'")
108
+ result = if content_components.size < 2
109
+ content_components[0]+"\e[44m\e[32m'#{partial_path}'\e[0m\e[0m"
110
+ else
111
+ content_components[0]+"\e[44m\e[32m'#{partial_path}'\e[0m\e[0m"+content_components[1]
112
+ end
113
+ return result
114
+ elsif content.include?("\"#{partial_path}\"")
115
+ content_components = content.split("\"#{partial_path}\"")
116
+ result = if content_components.size < 2
117
+ content_components[0]+"\e[44m\e[32m\"#{partial_path}\"\e[0m\e[0m"
118
+ else
119
+ content_components[0]+"\e[44m\e[32m\"#{partial_path}\"\e[0m\e[0m"+content_components[1]
120
+ end
121
+ return result
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PartialInspector
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "partial_inspector/version"
4
+ require_relative "partial_inspector/scanner"
5
+
6
+ module PartialInspector
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+
10
+ def self.scanner()
11
+ PartialInspector::Scanner.new
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ module PartialInspector
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: partial_inspector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - SalihaShahid
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-07-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - salihashahid1102@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - Rakefile
22
+ - lib/partial_inspector.rb
23
+ - lib/partial_inspector/reporter.rb
24
+ - lib/partial_inspector/scanner.rb
25
+ - lib/partial_inspector/utils.rb
26
+ - lib/partial_inspector/version.rb
27
+ - sig/partial_inspector.rbs
28
+ homepage:
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.2.3
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Partial Inspector
51
+ test_files: []