Giving observers their own directory

Given that mailers now have their own directory by default in app/mailers, should the same thinking be applied for putting observers in app/observers or app/models/observers?

—P

“app/models/observers” would be fine by me, but I don’t think observers are ever that many that they warrant having their own directory. they’re mostly here to cleanup our models or to extract concerns tied to lifecycles of certain models which they themselves shouldn’t be concerned with.

Given that mailers now have their own directory by default in app/mailers, should the same thinking be applied for putting observers in app/observers or app/models/observers?

"app/models/observers" would be fine by me, but I don't think observers are ever that many that they warrant having their own directory. they're mostly

Hmmm, perhaps, although I only ever have one or two mailers in my apps...

Buy it could be said that observers belong to the model layer. For me app/models is where the model layer lives generally speaking, non-persistent models and all. So they are OK there in my view and do not quite see this in the generated app layout.

Of course if you prefer to put them in their own directory it is easy to add app/observers to autoload_paths in config/application.rb.

Buy it could be said that observers belong to the model layer. For me app/models is where the model layer lives generally speaking, non-persistent models and all. So they are OK there in my view and do not quite see this in the generated app layout.

I agree that they belong in the model layer, but they're not models, hence the app/models/observers suggestion.

Of course if you prefer to put them in their own directory it is easy to add app/observers to autoload_paths in config/application.rb.

I agree that it's easy, but my main point is raising the issue as to whether they should have their own folder by default and it seems to me that it's a similar situation.

What stands out for me it the pattern that's now forming:

app/controllers -> *_controller.rb app/views -> *.html.erb app/helpers -> *_helper.rb

Mixing modelname.rb and modelname_observer.rb in the same directory to me seems both cluttered and inconsistent with how the other directories are organised.

—P

For what it's worth, I've had this block in my 2.3.x environment.rb's for a while and have been happy with the organizational system it yields.

  %w( sweepers observers mailers ).each do |path|     config.load_paths.unshift File.join(Rails.root, 'app', path)   end   config.action_mailer.template_root = File.join Rails.root, 'app', 'mailer_views'

  observer_files = Dir[File.join(Rails.root, 'app', 'observers', '*.rb')]   config.active_record.observers = observer_files.map {|f| File.basename f, '.rb'}

Mailers, sweepers, mailer views, and observers are conceptually distinct from models, http-facing views, and controllers. Observers aren't models/observers any more than helpers are views/helpers.

–Jacob

For the archives, this is now autoload_paths.

Just to chime in: I always place my observers in their own folder (usually app/ observers), so I'd +1 a specific folder for them.

I chose app/models/observers for mine But soon enough I got rid of them all Rafactoring their functionality away.

I feel like observers sound like a good idea But generally end up better implemented in a different way.

So giving them their own folder.. I don't mind.