-
-
Save krisleech/3791c763c3038529550e to your computer and use it in GitHub Desktop.
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 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