Skip to content

Instantly share code, notes, and snippets.

@samnang
Created September 15, 2013 03:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samnang/6567768 to your computer and use it in GitHub Desktop.
Save samnang/6567768 to your computer and use it in GitHub Desktop.
Decorator vs Form Object vs Service Object?
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
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
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