Re:Rails – The Missing Parts
by troels on Monday March 03, 2014 @02:04PM
(#7335654)
One thing I can't figure out is whether to place my service objects under `/app/models` or under `/lib`. There doesn't seem to be a clear consensus on this? I tend more towards the former, because autoload works better during development. Also, I consider them part of my domain model. What do you do?
[ Parent
| on HN ]
There's nothing stopping you from creating extra folders under /app to contain various types of service objects. Personally I have:
/app/factories - for classes which create active record objects
/app/services - for classes which do something I generally don't care about the return value of. Often perfect for background jobs.
/app/decorators - most of these are Draper classes
/app/forms - form objects
and a few others from various other gems. /app/interactors
It's a trivial thing to add new folders like this to autoloading: # add to application.rb
config.autoload_paths += %W(#{config.root}/app/interactors)As of Rails 3.0, I believe app/anything/ is autoloaded:
* https://github.com/rails/rails/blob/3-0-stable/railties/test...
Re:Rails – The Missing Parts
by tomblomfield on Monday March 03, 2014 @02:31PM
(#7335845)
Anything that is application-specific, we put in /app
Anything that's generic and re-usable, we put in /lib. This code is normally a prime candidate for gems - only very simple code stays in /lib for very long.
I use /app/interactors or /use_cases (per some Uncle Bob talk about architecture). I don't consider them models in the 'Data Model' sense so I don't put them in /app/models
I have app/services and just add that in application.rb to the load path.