-
-
Save mattwynne/3088890 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 CommentsController < ApplicationController | |
def create | |
app.create_comment(params) | |
end | |
private | |
def app | |
MyRailsApp.new | |
end | |
end | |
# this is a facade over the domain which wires up the right adapters and the right domain objects | |
class MyRailsApp | |
def create_comment(params) | |
comment = Comment.new(params) | |
comment_processor.new_comment(comment) | |
end | |
private | |
def comment_processor | |
listener = FanoutAnnouncer.new(twitter_poster, facebook_poster, comment_persister) | |
CommentProcessor.new(listener) | |
end | |
def twitter_poster | |
TwitterPoster.new | |
end | |
def facebook_poster | |
FacebookPoster.new | |
end | |
def comment_persister | |
SQLCommentPersister.new | |
end | |
end | |
# example class | |
class CommentProcessor < Struct.new(:listener) | |
def new_comment(comment) | |
new_comment = comment.with_language(language_for(comment.body)) | |
.with_spam(is_spam(comment.body)) | |
listener.announce('new_comment', new_comment) | |
end | |
end | |
# immutable value object for carrying data about a comment | |
class Comment | |
include Virtus::ValueObject | |
attribute :body, String | |
def with_language | |
# ... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a small detail, but was interested if you would pass in the entire params hash into the facade or filter on the comment attributes. The example never specifies the comment attributes; i.e. params[:comment].