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:
classPublishingServicedefpublish_booktrueiffinalize_book&&author.update(book_count: (author.book_count+1))&&send_announcement_emailend# other methodsend
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.
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:
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: