This project is archived and is in readonly mode.

#6221 ✓wontfix
Ravil Bayramgalin

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

Reported by Ravil Bayramgalin | December 25th, 2010 @ 03:26 PM | in 3.1

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 - (includes documentation and tests)

Hope to get a feedback :)

Comments and changes to this ticket

  • Santiago Pastorino

    Santiago Pastorino January 9th, 2011 @ 09:13 PM

    • 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'


    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 ;).

  • Santiago Pastorino

    Santiago Pastorino January 15th, 2011 @ 01:58 AM

    • 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 :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

  • Ravil Bayramgalin

    Ravil Bayramgalin January 15th, 2011 @ 11:12 AM

    So here are the new patches: - [PATCH 1/3] Add observer_path method to load observers matching given pattern - [PATCH 2/3] Replace ActiveRecord references in ActiveModel::Observing comments - [PATCH 3/3] Update application.rb template to reflect observer changes

  • José Valim

    José Valim January 21st, 2011 @ 10:16 AM

    • State changed from “open” to “wontfix”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>