Skip to content

Instantly share code, notes, and snippets.

@brushbox
Forked from justinko/Plea.markdown
Created July 16, 2012 04:31
Show Gist options
  • Save brushbox/3120546 to your computer and use it in GitHub Desktop.
Save brushbox/3120546 to your computer and use it in GitHub Desktop.
Am I doing it wrong?

Dear Rubyists,

I just lost a contract because of my code in a Rails project.

The specific code in question is related to a "posting a comment" feature. Here are the details:

In this project, "posting a comment" does not simply entail inserting a row into the database. It involves a procedure to yes, insert a row, but also detect its language, check for spam, send emails, and "share" it to Twitter and Facebook. I believe this algorithm should be encapsulated. I do not believe it belongs in a controller or a model. I do not believe Active Record callbacks should be used.

The "senior developer", whom is the stake holder's right hand man, said this:

All that's being accomplished is moving a few statements from the controller to the a new class and adding even more code to support the new construct. We don't have the problems that this would solve like comments being posted from multiple controllers.

What do you guys think? Am I doing it wrong?

Please leave a comment, good or bad. My motivation to continue programming is at an all time low.

Thank you.

# app/use_cases/post_comment.rb
# Called from the "create" action in a controller
class PostComment
def initialize(language_detector = LanguageDetector.new,
spam_checker = SpamChecker.new,
comment_mailer = CommentMailer.new,
post_to_twitter = PostToTwitter.new,
post_to_facebook = PostToFacebook.new)
@language_detector = language_detector
@spam_checked = spam_checker
@comment_mailer = comment_mailer
@post_to_twitter = post_to_twitter
@post_to_facebook = post_to_facebook
end
def post(user, entry, attributes)
comment = user.comments.new
comment.assign_attributes(attributes.merge(entry: entry))
comment.save!
@language_detector.set_language(comment)
@spam_checker.check_spam(comment)
@comment_mailer.send_mail(comment)
@post_to_twitter.post(user, comment) if comment.share_on_twitter?
@post_to_facebook.action(user, comment, :comment) if comment.share_on_facebook?
comment
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment