Created
September 29, 2011 20:24
Example Rollcall plugin for Corral
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 'rest_client' | |
require 'uri' | |
require 'account' | |
# this is basically a monkey patch of Rollcall's Account class... | |
# it's one of those things you would never see in Java, because you can't really | |
# modify a class during runtime | |
Account.class_eval do | |
# here we tell the Account class to execute different methods before | |
# creating/updating/destroying a record | |
before_create :create_account_in_corral, :if => proc{ !login.blank? && !password.blank? } | |
before_update :update_account_in_corral, :if => proc{ !login.blank? && !password.blank? } | |
before_destroy :delete_account_in_corral | |
# this gets called prior to the record actually being committed to the database during | |
# the "validation" step... one of the callbacks we refer to above might have encountered | |
# an error and stored it under the @corral_error member variable. We check for it and | |
# add it to the Account's list of errors. This will prevent the account from being saved, | |
# and the user or web service client will see an error message instead. | |
validate do | |
self.errors[:base] << @corral_error if @corral_error | |
end | |
def create_account_in_corral | |
# do the stuff you need to do to create the account in Corral. | |
# if there's an error, put it (the string error message) in @corral_error | |
end | |
def update_account_in_corral | |
end | |
def destroy_account_in_corral | |
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
# this is the file that makes the whole thing into a Rails plugin (a "Railtie") | |
# one of the things we do here is define the kinds of config options you can | |
# set for the plugin | |
module RollcallCorral | |
class Railtie < Rails::Railtie | |
config.corral = ActiveSupport::OrderedOptions.new | |
initializer 'corral.initialize' do |app| | |
# check if the option was given | |
if config.corral.some_config_option | |
# hold on to it as a constant (there are better ways to do it, but this is simpler | |
RollcallCorall::SOME_CONFIG_OPTION = config.corral.some_config_option | |
end | |
# load the account_callbacks.rb monkeypatch file when the plugin is initialized | |
require 'account_callbacks' | |
end | |
end | |
end | |
# the config options are set either in Rollcall's config/environments/*.rb files, or in | |
# config/application.rb (if you want the same config for all environments |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment