Reputation: 4083
I'm currently working on a largish Ruby on Rails project. It's old enough and big enough that it's not clear if all views are actually in use.
Is there any script/plugin out there that can generate a list of unused view files?
Upvotes: 16
Views: 3838
Reputation: 3632
The Evil-Martians have a script for RSpec, that will tell you after a test run, which Views are not called during test:
https://github.com/evilmartians/terraforming-rails/tree/master/tools/templates_tracker
Drop in spec/support and run with TT=1
wget https://raw.githubusercontent.com/evilmartians/terraforming-rails/refs/heads/master/tools/templates_tracker/templates_tracker_rspec.rb \
-O spec/support/templates_tracker.rb
TT=1 rspec
Upvotes: 0
Reputation: 23989
Just install and run the discover-unused-partials gem:
gem install discover-unused-partials
discover-unused-partials rails_root_directory
Upvotes: 5
Reputation: 432
I wrote a script to find unused partials/views. I assumed, though, that "unused" means that a view-file is present for which no controller-method is defined (any more). The script does not check whether the view is called because there is no link from the default-route to it. This would have been far more complex.
Place the following script in the application's script folder:
#!/usr/bin/env ruby
require 'config/environment'
(Dir['app/controllers/*.rb'] - ['app/controllers/application.rb']).each do |c|
require c
base = File.basename(c, '.rb')
views = Hash.new
Dir["app/views/#{base.split('_')[0]}/*"].each do |v|
views.store(File.basename(v).split('.')[0], v)
end
unused_views = views.keys - Object.const_get(base.camelcase).public_instance_methods - ApplicationController.public_instance_methods
puts "Unused views for #{base.camelcase}:" if unused_views.size > 0
unused_views.each { |v| puts views[v] }
end
It is kinda hackish and unfinished, but it does the job - at least for me.
Execute it like this (you only need to change the execute-bit the first time with chmod):
chmod +x script/script_name
./script/script_name
Enjoy!
Upvotes: 7
Reputation: 6581
Take a look at the following script on GitHub http://github.com/vinibaggio/discover-unused-partials
Upvotes: 8
Reputation: 71
Iterate through your partials, grep (or awk) the project for the name of the file. Adjust your search regex to look for "render :partial" at beginning of line for generic partials (eg, "_form").
Upvotes: 3