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 Contact < ActiveRecord::Base | |
... | |
def after_create | |
if Hook.hooks_exist?('new_contact', self) | |
Resque.enqueue(Hook, self.class.name, self.id) | |
# To trigger directly without Resque: Hook.trigger('new_contact', self) | |
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
class Hook < ActiveRecord::Base | |
attr_accessible :event, :account_id, :subscription_url, :target_url | |
validates_presence_of :event, :account_id, :subscription_url, :target_url | |
# Looks for an appropriate REST hook that matches the record, and triggers the hook if one exists. | |
def self.trigger(event, record) | |
hooks = Hook.find(:all, :conditions => { | |
:event => event, | |
:account_id => record.account_id, | |
}) |