Observers in Sub-directories in Edge Rails

Shanti Braford
Tue, 10 Oct 2006 12:02:05 -0000

Hello,

I haven''t had the opportunity to play with Edge Rails recently, but
upon testing out a new app in it, found that having observers in
sub-directories doesn''t work out of the box.

i.e. our app/models folder looks something like:
models/
  - attachment.rb
  - beta_code.rb
  billing/
   - biller.rb
   - coupon.rb
  observers/
   - conversation_observer.rb
   - feed_observer.rb

Putting them in subdirectories was done just to keep things in the
app/models folder somewhat manageable & organized.

Just counted our models, we''re now up to 47!  (and they said Rails was
only for toy scaffolding apps... =))

So, for example the billing/biller.rb model definition looks like:
class Biller
  ...
end

Not, "Billing::Biller"...

A few questions:

1) Any advice on models directory organization going forward (i.e. for
Edge Rails & Rails 2.0)?  (i.e. is our setup going to be OK, or what is
recommended when you have this many models)

2) Is it OK to keep Observers in a subdirectory within app/models?

So this line in environment.rb:
ConversationObserver.instance

Was causing this error:

/ ... long path ...
/activesupport/lib/active_support/dependencies.rb:251:in
`load_missing_constant'': uninitialized constant CannedReplyObserver
(NameError)

Not so much worried about that.  Should I open a ticket for this or is
it just expected behavior?

Adding this to environment.rb did the trick anyway:
Dir["#{RAILS_ROOT}/app/models/observers/*"].each do |path|
  filename = path.split(''/'').last.sub(''.rb'',
'''')
  require "app/models/observers/#{filename}"
end

Thanks!

Shanti