Skip to content

Instantly share code, notes, and snippets.

@LindseyB
Created May 6, 2024 00:58
Show Gist options
  • Save LindseyB/d659fee213ca515a9356b05db6a4b914 to your computer and use it in GitHub Desktop.
Save LindseyB/d659fee213ca515a9356b05db6a4b914 to your computer and use it in GitHub Desktop.
Finds specifically unused views in a rails project (skips partials in favor of the unused partials gem)
#!/usr/bin/env ruby
require './config/environment'
(Dir.glob("./app/views/**/*.html.erb")).each do |v|
next if v.include?('components') || v.include?('layouts') || v.include?('mailer') || v.include?('errors') || v.include?('static_pages') || v.include?('partials')
match = /app\/views\/(.*)\/(.+)\.html\.erb/.match(v)
klass = "#{match[1].camelize}Controller".constantize rescue nil
action = match[2]
# skip partials because we can use the `discover-unused-partials` gem
next if action[0] == "_"
if klass.nil?
puts "Unused view: #{v}"
next
end
unless klass.new.public_methods.include?(action.to_sym)
puts "Unused view: #{v}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment