Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

This project is archived and is in readonly mode.

[PATCH] Add option to load default observers from app/models/*_observer.rb

#6221

Currently, when using observers, we need to manually list all of them in environment configuration

# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer

But we already have a default convention for naming observers - running "rails generate observer user" creates app/models/user_observer.rb. So why don't we add an option to automatically load observers named per convention.


My patch adds support for adding :default argument to list of observers which has the same effect as listing all observers from app/models/*_observer.rb

config.active_record.observers = :default

If you use non-conventional observers you can mix :default argument with them:

config.active_record.observers = :default, :garbage_collector

Patch - https://gist.github.com/754906 (includes documentation and tests)

Hope to get a feedback :)

Reported by Ravil Bayramgalin · December 25th, 2010 @ 03:26 PM

State: wontfix
Milestone: 3.1
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Santiago Pastorino
    Santiago Pastorino
    • Milestone set to 3.x
    • State changed from new to open
    • Importance changed from to Low

    Can you change that to allow

    config.active_record.observers = 'app/models'

    and

    config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    so when you set observers as a string you call instantiate_observer of every *_observer.rb file

    Also remember that you're coding in Active Model so references to Active Record are not welcome ;).

    January 9th, 2011 @ 09:13 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • Milestone changed from 3.x to 3.1

    For the record we agreed on that ...

    config.active_record.observer_path 'app/models/*_observer.rb' # default in railties
    config.active_record.observer :cacher, :garbage_collector # in app, add two observers on top of defaults
    config.active_record.observers = :cacher, :garbage_collector # in app, old style assignment explicitly sets these observers, clobbering defaults

    old API would retain compatibility by wiping out defaults

    January 15th, 2011 @ 01:58 AM

  3. Ravil Bayramgalin
    Ravil Bayramgalin

    So here are the new patches:

    https://gist.github.com/780840 - [PATCH 1/3] Add observer_path method to load observers matching given pattern
    https://gist.github.com/780842 - [PATCH 2/3] Replace ActiveRecord references in ActiveModel::Observing comments
    https://gist.github.com/780843 - [PATCH 3/3] Update application.rb template to reflect observer changes

    January 15th, 2011 @ 11:12 AM

  4. José Valim
    José Valim
    • State changed from open to wontfix

    January 21st, 2011 @ 10:16 AM