guidelines for where to put classes in Rails apps that don't fit anywhere

前端 未结 5 1687
忘掉有多难
忘掉有多难 2020-12-24 01:28

I\'m wondering if there are any best practices about where to put non-standard Ruby files in Rails apps, those that don\'t fit in any of the default directories (contr

5条回答
  •  难免孤独
    2020-12-24 01:48

    You touch on a number of different use cases, and I think that this part is the closest to the "right" answer:

    I've got quite a lot of these now, some of them are added to lib which ends up as a pile of random classes and modules, some sneak into app/models. I'd like to organize this somehow, but I don't know where to start.

    That's pretty much right on in my book. The one thing you don't mention is extracting various pieces into separate gems. Classes that talk to external services are excellent candidates for extraction, as are strategy classes if they're sufficiently general. These can be private, since running your own gem server isn't hard, and you can then obviously reuse them across ROR apps.

    Last and most concretely, resque jobs I stuff into lib/jobs.

    My rule of thumb is if it's a model of some kind, it goes into app/models. If not, it probably belongs in lib or some appropriately named subdirectory thereof, e.g. lib/jobs, lib/extensions, lib/external, or the like.

提交回复
热议问题