Re:Where the logic hides in rails apps
by programminggeek on Sunday December 23, 2012 @11:36PM
(#4961861)
In the Obvious architecture, it would be something more like..
class CreateContact
def initialize contact_jack, email_jack
@contact_jack = contact_jack
@email_jack = email_jack
end
def do input
# validate input
contact = Contact.new
contact.populate input
contact_jack.save contact.to_hash
email = ContactEmail.new
email.populate contact.to_hash
email_jack.send email.to_hash
contact.to_hash
end
end
With that structure you can call CreateContact with: action = CreateContact.new
result = action.do ContactJack.new, EmailJack.new
In that structure you can totally test the action and logic without hitting the db or the mail system at all. Your ContactJack and EmailJack can be easily swapped out for various pluggable data stores. Fileystem, MySQL, Mongo, Postgres, Cassandra, could be swapped out for the ContactJack. EmailJack could send through standard mail servers, SendGrid, Amazon AWS, Mandrill, etc.Obvious is on github, you can read more at http://obvious.retromocha.com
I agree with your comments on the class name, it should probably be ContactCreator. But I think having a method named create_contact_and_send_email is kind of code-smelly. I'd most likely do something like:
create_contact!(:send_email => false)
Re:Where the logic hides in rails apps
by bradwestness on Sunday December 23, 2012 @07:21PM
(#4961258)
Yeah "CreatesContract" is a weird name. ContractCreator, or ContractRepository (so it can do more than just create objects) makes more sense to me.
Plus he should probably just break the send_email method into it's own method, which he can then call depending on if create_contact returns successfully.
A simpler, more contrived example, would be to create a new function create_and_send_email! and place the creation logic and email sending logic in there. And now, when the developer uses the service object, she has the ability to choose between just creating the contact, or creating it and sending an email.