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