-
-
Save whereisciao/820720 to your computer and use it in GitHub Desktop.
Mongoid Observer using ActiveModel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "#{RAILS_ROOT}/lib/mongoid/lib/observing.rb" | |
# Initializer for Observers | |
User.observers = :user_observer | |
User.instantiate_observers | |
# The preferred syntax, similar to AR's setup | |
# Mongoid.configure do |config| | |
# config.observers = :user_observer | |
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Mongoid | |
module Observing | |
CALLBACKS = [ | |
:before_create, :before_destroy, :before_save, :before_update, | |
:before_validation, :after_create, :after_destroy, :after_save, | |
:after_update, :after_validation | |
] | |
def self.included(base) | |
base.module_eval { include ActiveModel::Observing } | |
CALLBACKS.each do |callback| | |
callback_method = :"notify_observers_#{callback}" | |
base.module_eval <<-RUBY, __FILE__, __LINE__+1 | |
#{callback}(#{callback_method.inspect}) | |
def #{callback_method}(&block) | |
notify_observers(#{callback.inspect}, &block) | |
end | |
private #{callback_method.inspect} | |
RUBY | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'carrierwave/orm/mongoid' | |
class User | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
include Mongoid::Observing | |
field :name | |
field :username | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserObserver < ActiveModel::Observer | |
def after_validation(user) | |
puts "User Observer After Validation" | |
end | |
end |
Got it to work with cgriego's tips:
- upgrade mongoid gem to master
- create an initializer or equivalent to load the observers: Mongoid.observers << :user_observer
- generate the observer: rails g observer user
- implement observer class "user_observer" and make it inherit from Mongoid::Observer (the generator will set this up for you)
My observer callbacks were called twice. Before and after the original model validation.
Fixed it by removing the explicit callback in the module eval, which causes the observer callback only to trigger after the orignal model callback.
No luck using the core support for observers officially added in Mongoid by @cgriego?
@angelim, works well with the Mongoid::Observer in master, however our production env isn't upgraded to Mongoid >= 2.0.0 yet.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you upgrade to Mongoid master then you'll get core support for observers. Add your observers to Mongoid.observers and make them inherit from Mongoid::Observer.