How to find all unused code in Ruby on Rails - ruby-on-rails

I've inherited a Rails 2.3 app, which lacks a solid test suite. There is a fair amount of testing, but unfortunately, many of the tests test against old, unused models, controllers, and views.
Does anyone have a solid idea of how I might go about testing which models, controllers, views, helpers, etc. are completely unused, as well as look into the ones that ARE used and see which functions specifically are not used?

You can look at this answer, and perhaps some of the other answers listed: https://stackoverflow.com/a/9788511/485864
I would probably end up logging the methods that you have, and run your code through the paths and anything not listed in the log, may be examined to see if it is indeed not used.

RCov or SimpleCov won't do this what you want?

You can try to use RubyMine, a Rails IDE, to search for unused code. Try searching for method names and stuff like that. It's been a while since I used it so I dunno if it will have a highlight on unused methods.
Also, you can try some bash commands(grep/ack/find) to search for the code snippets.

Related

Find all undefined variables in Ruby file?

I am coming across lot of production issues related to undefined variables. How can I FIND all undefined variables in Ruby file? Is there any gem/script which can scan through code files and detect possible issues.. This way I can test code before production deployment or accepting Pull Requests.
This can't really be done. There are too many ways a variable could be defined. There's also an equal number of ways a variable that should be defined could be set to nil for whatever reason.
You can run Ruby with warnings enabled (-w on the command-line), which will complain vigorously about these things. It will only complain about variables along the execution path of the code, so if there are sections you don't exercise, you won't get warnings.
This is why having a test suite that exhaustively tests branches is essential to shaking out bugs like this. If you have an if in your code, you need two tests for it. If you have two if clauses, you may need three or four. Anything with a case will need all branches tested. This can be a lot of work for a project that's got a lot of business logic in it.
Since Ruby isn't compiled per-se, it's not really able to detect these sorts of issues before the code is run. What's in your file and what actually gets executed can be worlds apart depending on the impact of other parts of code. This is not true in more conservative languages like C or Rust.
Test Unit is built into rails so write some unit tests. Then run with rake test.
Try the rspec and simplecov gems.
rspec allows you to write unit tests that find undefined variables.
simplecov measures code coverage to make sure your tests cover all variables.

is there a complete list of rspec api

newbie. I would like a full list of the rspec api - particularly the built in matchers. It appears this web site: http://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/built-in-matchers appears to be done base on version.
the problem I have is, lets say I need a have(n).items matcher. I would have no idea this matcher existed by looking at version 3.4. I would potentially have to view all ~20 versions to discover it. Why is it done this way? Doesn't this seem like an awkward way to publish an API? is there a full list somewhere? :)
That matcher was removed in RSpec 3:
http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/
To find which matchers are available, check your Gemfile.lock to see which version of RSpec is in use, then refer to the corresponding documentation.

RSpec "retest broken tests from last time" flag

Is there any way to retest previously broken tests?
So, say, I run rspec and several tests in different files and directories fail.
I fix something and now I have to manually specify all files and folders I want to retest or run tests for whole project again(It takes considerable amount of time for big projets).
What I was looking for is something like a flag
rspec --prev-failed-only
I realize that such flag would require considerable amount of additional actions from rspec, like storing results of previous tests and so on. But I think it would be super convenient to me.
Is there any such(or similar) tool/gem?
rspec-rerun gem does what you want : https://github.com/dblock/rspec-rerun
https://github.com/rspec/rspec-core/issues/456 has a good discussion on the topic of making rspec itself be able to rerun failed tests.
On the Giant Robots podcast, Sam Phippen of the core team mentioned this feature is due to be added to RSpec soon.
In case anyone else finds this, a month after the question was asked (open source <3 ) rspec 3.3 introduced the --only-failures option, also the delightfully handy --next-failure (-n) option. rspec --help for more info.

Plugin vs Engine in Rails 3, shipped as a gem

In the documentation for Rails::Plugin (for Rails 3), I'm reading the following:
"... you actually cannot declare a Rails::Engine inside your Plugin, otherwise it would cause the same files to be loaded twice. This means that if you want to ship an Engine as gem it cannot be used as plugin and vice-versa."
Can anyone be more specific about exactly what files get loaded twice? I have declared a plugin/gem as Engine, and it works fine also being put inside vendor/plugins (and I'd like to keep it this way), in spite of the statement above. I simply want some clarity on why (and whether) this is not a good thing to do.
Thank you!
I don't have an exact answer myself, but this subject is covered in this resource which I have found helpful:
https://gist.github.com/e139fa787aa882c0aa9c

tips and tricks for using vim with ruby/ruby on rails

I'm one of those developers who isn't using TextMate with any of his Ruby/Ruby on Rails work. My particular loyalty in this arena lies with vim. What are your favorite tips/tricks for using vim with Ruby and/or Ruby on Rails to make you as efficient as possible when working?
Most important
Get a copy of rails.vim it is awesome on millions of levels. Read the doc. There are way too many tips, :Rview customer, :RSmodel foo, :Rinvert, gf, :Rextract, :Rake and the list goes on and on. You will probably want NERDTree as well for easy navigation (which you can access using :Rtree)
Second most important
Follow tpope on twitter (the author of fugative, rails.vim, haml.vim, vividchalk theme, cucumber.vim and so on), he seems to be posting new related to Rails vim plugins quite regularly (be it syntax highlighting or git integration).
You might want to checkout my ruby/rails specific vimfiles.
Its a useful starting point and has many useful Ruby/Rails plugins bundled and configured.
The one thing that really sucks about Textmate is that it doesn't run on Linux. My vim/gvim config is the same on Mac, Windows and Linux. Same fonts, same themes, same plugins and same customizations.
I mostly use Textmate for snippets and quick evaluations for posting here.
I wrote an in depth guide on using Textmate features (especially Rails related features) in VIM. It's very relevant to this question.
http://www.jackkinsella.ie/2011/09/05/textmate-to-vim.html
I don't use vim, instead, I'm like those millions of developers using Textmate. Nevertheless, a colleague does use vim/gvim.
By looking at him work, one of the things I wish I could do in Textmate is the ease of working on multiple files at the same time. Basically, you can easily manipulate multiple windows, which is quite handy.

Resources