Created
September 15, 2013 03:21
-
-
Save samnang/6567768 to your computer and use it in GitHub Desktop.
Decorator vs Form Object vs Service Object?
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 FacebookCommentNotifer | |
def initialize(comment) | |
@comment = comment | |
end | |
def save | |
@comment.save && post_to_wall | |
end | |
private | |
def post_to_wall | |
Facebook.post(title: @comment.title, user: @comment.author) | |
end | |
end | |
class CommentsController < ApplicatinController | |
def create | |
@comment = Comment.new(params[:comment]) | |
if FacebookCommentNotifer.new(@comment).save | |
redirect_to blog_path, notice: "Your comment was posted." | |
else | |
render "new" | |
end | |
end | |
end |
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 CommentForm | |
include ActiveModel::Model | |
attr_accessor :title, :author | |
# validations | |
def submit(params) | |
return false unless valid? | |
comment = create_comment(params) | |
post_to_wall(comment) | |
true | |
end | |
private | |
def create_comment(params) | |
Comment.create(params) | |
end | |
def post_to_wall(comment) | |
Facebook.post(title: comment.title, user: comment.author) | |
end | |
end | |
class CommentsController < ApplicatinController | |
def create | |
@comment_form = CommentForm.new | |
if @comment_form.submit(params[:comment]) | |
redirect_to blog_path, notice: "Your comment was posted." | |
else | |
render "new" | |
end | |
end | |
end |
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 CommentNotifier | |
def self.create(params) | |
comment = Comment.new(params) | |
if comment.save | |
Facebook.post(title: comment.title, user: comment.author) | |
end | |
comment | |
end | |
end | |
class CommentsController < ApplicatinController | |
def create | |
@comment = CommentNotifier.create(params[:comment]) | |
if @comment.persisted? | |
redirect_to blog_path, notice: "Your comment was posted." | |
else | |
render "new" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment