re: I'm Sandi Metz, Ask Me Anything! [Finished] VIEW POST

FULL DISCUSSION
 

Hey Sandi, thanks so much for doing this AMA!

I'm not too sure if this is related to object-oriented design, but I was wondering about this code snippet I have in my PublishingService object:

class PublishingService
  def publish_book
    true if finalize_book &&
        author.update(book_count: (author.book_count + 1)) &&
        send_announcement_email
  end

  # other methods
end

Is this generally an acceptable pattern? When I created this, I assumed it was a standard since Rails' ActiveRecord actions like update return true if it went through as opposed to some final object.

 

I'm not Sandi Metz but there are a few things that captured my attention about that piece of code.

You're using a service/business/whatever object and that is good ™ (instead of pushing all that code down in the model)

I would pass the book (and the author, unless you infer it from the book object) as an argument to publish book, this makes your code easier to test and mock.

Something like:

def publish_book(book, author)
   finalize_book(book) && author.increase_book_count! && send_announcement_email
end
code of conduct - report abuse