Rails stats only includes code from some specific folders in app

Another WTF I have is about rails stats. We try to follow Rails structure as much as possible, but we have some extra folders in app (like clients, services and others, based on the project), and the big surprise is that rails stats doesn’t include them :man_facepalming:

It’d be great if it was smart enough to include such folders and not only a subset of them

2 Likes

Hi! You can customize it to include your folders. My app is named baqio for instance. I created a rake task baqio.rake like that:

task stats: 'baqio:stats'

namespace :baqio do
  task :stats do
    require 'rails/code_statistics'
    ::STATS_DIRECTORIES << ['Builders', 'app/builders']
    ::STATS_DIRECTORIES << ['Decorators', 'app/decorators']
    ::STATS_DIRECTORIES << ['Facades', 'app/facades']
    ::STATS_DIRECTORIES << ['Forms', 'app/forms']
    ::STATS_DIRECTORIES << ['Policies', 'app/policies']
    ::STATS_DIRECTORIES << ['Pdfs', 'app/pdfs']
    ::STATS_DIRECTORIES << ['Services', 'app/services']
    ::STATS_DIRECTORIES << ['Uploaders', 'app/uploaders']
  end
end

You can them do rake stats and it will works :slight_smile:

1 Like

This is definitely nice solution. But I agree with @eric15 that it should be default behaviour of this command. I see absolutely no sane reason to treat default directories different then the additional custom ones while they are clearly integral part of the app itself.

Let’s watch language like this. Rails is a big tent, and no one knows everything – so just because you don’t see a reason doesn’t mean there isn’t one.

Something like “To me, it seems like anyone adding custom directories to Rails would always want them included in the stats” might be better.

One thing I noticed when rephrasing that: “adding custom directories to Rails” isn’t something that Rails treats as a first-class thing. We can add things to the autoload paths, but that’s subtly different. It might be that the reason that custom directories aren’t in Rails stats has to do with the fact that “custom directories” aren’t really a Rails-defined concept. So there would be no obvious place to automatically add things to the stats.

Point taken on the language. I phrased my opinion a bit too aggressive there. What I meant was that I myself consider treating all files in /app directory the same in context of stats as most logical approach.

While I understand that part of the directories structure is dictated by a convention we all embrace (and love). But it does not mean that this convention cannot be expanded locally. For example code that do not fall simply into standard MVC pattern, like service objects, can be put into additional directory. And I support the idea of including those directories in stats listing automatically by default.