This project is archived and is in readonly mode.

#4152 ✓invalid
Mike (at coverallcrew)

ActiveModel::Observer and ActiveRecord...

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

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.

Comments and changes to this ticket

  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) March 27th, 2010 @ 09:24 AM

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

  • Mike (at coverallcrew)

    Mike (at coverallcrew) March 27th, 2010 @ 05:31 PM

    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.

  • Mike (at coverallcrew)

    Mike (at coverallcrew) July 23rd, 2010 @ 08:33 PM

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

  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
  • bingbing
  • bingbing

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages