Last active
July 24, 2024 21:29
-
-
Save bonniesimon/9f87ab5279c30bac9744b5f33056a1c6 to your computer and use it in GitHub Desktop.
Using view_helper methods directly in a view_component file throws an error. We have to use helper.method_name to access the view helper method. So first we need to find helper methods that are used in the view_component.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to get all function names from all helpers and check their presence in a specific file | |
def get_all_helper_functions | |
all_helper_functions = [] | |
Dir[Rails.root.join("app", "helpers", "**", "*_helper.rb")].each do |helper_file| | |
relative_path = Pathname.new(helper_file).relative_path_from(Rails.root.join("app", "helpers")).to_s | |
helper_module_name = relative_path.chomp(".rb").camelize | |
begin | |
helper_module = helper_module_name.constantize | |
methods = helper_module.instance_methods(false) | |
methods.each do |method| | |
all_helper_functions << "#{helper_module_name}##{method}" | |
end | |
rescue NameError => e | |
puts "Warning: Couldn't load #{helper_module_name}: #{e.message}" | |
end | |
end | |
all_helper_functions | |
end | |
def check_file_for_helper_functions(file_path, helper_functions) | |
found_functions = [] | |
File.open(file_path, "r") do |file| | |
file_content = file.read | |
helper_functions.each do |func| | |
module_name, method_name = func.split("#") | |
if file_content.include?(method_name) | |
found_functions << func | |
end | |
end | |
end | |
found_functions | |
end | |
all_helper_functions = get_all_helper_functions | |
file_name = "app/components/incidents/sidebar_component.html.erb" | |
file_path = Rails.root.join(file_name) | |
unless File.exist?(file_path) | |
puts "Error: File does not exist: #{file_path}" | |
exit | |
end | |
found_functions = check_file_for_helper_functions(file_path, all_helper_functions) | |
if found_functions.empty? | |
puts "No helper functions found in the file: #{file_name}" | |
else | |
puts "Helper functions found in the file #{file_name}:" | |
found_functions.each { |func| puts "- #{func}" } | |
puts "\nTotal helper functions found: #{found_functions.size}" | |
end | |
# We can run this by using rails runner check_presense_of_helper_methods.rb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment