Skip to content

Instantly share code, notes, and snippets.

@mikekreeki
Created June 27, 2014 14:22
Show Gist options
  • Save mikekreeki/8ea354849903a5b2f77e to your computer and use it in GitHub Desktop.
Save mikekreeki/8ea354849903a5b2f77e to your computer and use it in GitHub Desktop.
How to use ActiveRecord::Callbacks with clear separation of responsibilities
class EmailListener
def user_created(user)
UserMailer.welcome_email(user).deliver
end
end
class User < ActiveRecord::Base
include Wisper::Publisher
##############################
# CALLBACKS
##############################
after_create :notify_created
##############################
# PRIVATE METHODS
##############################
private
def notify_created
publish(:user_created, self)
end
end
class UsersController < ApplicationController
def create
@user = User.new(user_params)
@user.subscribe EmailListener.new
if @user.save
# ..
else
# ..
end
end
private
def user_params
# ..
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment