Skip to content

Instantly share code, notes, and snippets.

@krisleech
Forked from nicholasjhenry/gist:3024550
Last active April 12, 2017 10:34
Show Gist options
  • Save krisleech/3791c763c3038529550e to your computer and use it in GitHub Desktop.
Save krisleech/3791c763c3038529550e to your computer and use it in GitHub Desktop.
class AddComment
attr_reader :store
def initializer(store)
@store = store
return self
end
def call(post_id, comment_attrs)
post = store.find(post_id)
comment = post.add_comment(comment_attrs)
if comment.save
publish(:success, post, comment)
else
publish(:fail, post, comment)
end
end
end
class CommentController
def create
service = MyApp::AddComment.new(Post)
service.on(:success) { |post, comment| redirect_to post }
service.on(:fail) { |post, comment| @post, @comment = post, comment; render :new }
service.call(post_id: 1, comment: { body: "cool!"})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment