Created
November 27, 2012 02:06
-
-
Save bhserna/4151944 to your computer and use it in GitHub Desktop.
Factory method using convention and ruby
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 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 |
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
Notifications::NewComment.create(comment) | |
Notifications::NewIdea.create(idea) |
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
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