Skip to content

Instantly share code, notes, and snippets.

@bhserna
Created November 27, 2012 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhserna/4151944 to your computer and use it in GitHub Desktop.
Save bhserna/4151944 to your computer and use it in GitHub Desktop.
Factory method using convention and ruby
class ApplicationController < ActionController::Base
# other stuff ..
def notify(*args)
Notifications::Notifier.new.notify(*args)
end
end
class CommentsController < ApplicationController
# other stuff
def create
@comment = task.new_comment(params[:comment])
if @comment.save
notify :new_comment, @comment
# render comment
else
# render errors
end
end
end
Notifications::NewComment.create(comment)
Notifications::NewIdea.create(idea)
module Notifications
class Notifier
def notify kind, *args
notification_class(kind).create(*args)
end
private
def notification_class(kind)
"Notifications::#{kind.to_s.camelize}".constantize
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment