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.

This project is archived and is in readonly mode.

ActiveModel::Observer and ActiveRecord...

#4152

I'm using mongomapper and would like to implement some observers for my application and models.

config.active_record.observers = :my_observer

relies on having: require "active_record/railtie"

which requires a database.yml file, which requires I have at least one valid adapter, which requires I need to add something like "gem 'sqlite3-ruby' to my Gemfile.

Reported by Mike (at coverallcrew) · March 11th, 2010 @ 12:29 AM

State: invalid
Milestone: 3.0.2
Assigned to: Yehuda Katz (wycats) Yehuda Katz (wycats)
Importance: none

Activity

  1. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from new to invalid
    • Assigned user set to Yehuda Katz (wycats)
    • Milestone cleared.

    If you want to use ActiveModel observers outside of ActiveRecord, you will need to include the ActiveModel observer model as such:

    class Post
      include MongoMapper::Document
      include ActiveModel::Observing
    
      def create(*)
        notify_observers(:before_create, self)
        value = super
        notify_observers(after_create, self)
        value
      end
    end
    

    If you wish, you can create a module that you can mix in to any MongoMapper model with observing powers:

    module MongoMapper
      module Observing
        # This will make sure that ActiveModel::Observing is included onto any model
        # that MongoMapper::Observing is included onto
        extend ActiveSupport::Concern
        include ActiveModel::Observing
    
        def create(*)
          notify_observers(:before_create, self)
          value = super
          notify_observers(after_create, self)
          value
        end
    
      end
    end
    
    class Post
      include MongoMapper::Document
      include MongoMapper::Observer
    end
    

    That said, the MongoMapper folks should probably provide something like this out of the box.

    March 27th, 2010 @ 09:24 AM

  2. Mike (at coverallcrew)
    Mike (at coverallcrew)

    Hi Yehuda,

    This still needs ActiveRecord requirements in config/application.rb (when calling config.active_record.observers). Perhaps I'm missing something for Rails to know how to get ActiveModel classes observable.

    Cheers.

    March 27th, 2010 @ 05:31 PM

  3. Mike (at coverallcrew)
    Mike (at coverallcrew)

    Can we reopen this to take a look. This still seems like an issue.

    July 23rd, 2010 @ 08:33 PM

  4. Jeremy Kemper
  5. bingbing
  6. bingbing