-
Notifications
You must be signed in to change notification settings - Fork 151
ActiveRecord::Observer
Kris Leech edited this page Dec 8, 2016
·
3 revisions
This shows how to mimick the behaviour of ActiveRecord::Observer
callbacks. The observer is a simple PORO (Plain Old Ruby Object).
The observer, models/bid_observer.rb
:
class BidObserver
def after_create(bid)
# ...
end
end
The model, models/bid.rb
:
class Bid < ActiveRecord::Base
include Wisper::Publisher
validates :amount, :presence => true
after_create do
broadcast(:after_create, self)
end
end
The controller, controllers/bids_controller.rb
:
class BidsController < ApplicationController
def new
@bid = Bid.new
end
def create
@bid = Bid.new(:amount => 40)
@bid.subscribe(BidObserver.new)
@bid.save
end
end
Need to ask a question? Please ask on StackOverflow tagging the question with wisper.
Found a bug? Please report it on the Issue tracker.