Extend rails:stats task

I find rails stats task useful when I join new projects. However, when I’d like to dig into more details, Rails doesn’t help me.

Say rails stats task tells me that controllers’ LOC is bigger than models. But how can I know which controller causes this? In fact, it’s not easy. Using ls -l tells us the size of each file, but that includes comments so it’s not always useful.

Here I suggest rails stats:controllers task, which tells something described above. It prints out stats information for given directories. This has some variations such as rails stats:models.

I’d love to work on this feature if people like it. What do you think?



Overall, I’m all for a nice way to show more useful stats. Your proposal starts to get into interesting territory of being a a code statistics analyzer. There are a handful of Rubygems that are starting to take some steps on this front.

One thought, is that it might be worth considering pitching this as a feature to Fastruby’s rails_stats gem, which is already doing some extending for us.

Hi, I didn’t know about rails_stats gem, thank you!

I just tried it out, but I couldn’t find a way to get detailed information about each part of the app (e.g. controllers) which I want to know. It’s great that it prints out custom directories, that should be supported by Rails by default.

Would be happy to see such sub tasks :+1:

1 Like

It’s not what you asked, but if you are now keen on rails_stats it may help you level up there, and I’ve never seen anything like this elsewhere.

I use a command to update a stats.md file with new stats for each release. The command converts the output of the standard rake task to markdown (GFM, with tables). This way the changes are tracked over time inside the repo. There is probably a more elegant way to do it, but once I had it working I stopped fiddling. Works on bash and zsh.

# Run with (change to match your local path):
#   bundle exec rake "stats[/Users/pboling/projects/myrailsapp]" | sed 's/\+/\|/g' | tee >(sed 's/^[^|]*//g' | sed '/^$/d' | sed -n -e '2,$p' > stats-table.md) | grep "Code LOC:" > stats-loc.md && cat stats-table.md stats-loc.md > stats.md && rm stats-table.md stats-loc.md
# NOTE: the brackets and actual path following the rake command are not optional!
# NOTE: The quotes are required if you run zsh instead of bash
require "rails_stats"

I think it’s a great idea. You might take a look at the interface of cloc or other LOC-counting tools for inspiration - I use cloc --by-file quite a bit, for example.