Skip to content

Instantly share code, notes, and snippets.

@coop
Created October 30, 2013 11:00
Show Gist options
  • Save coop/7230773 to your computer and use it in GitHub Desktop.
Save coop/7230773 to your computer and use it in GitHub Desktop.
# This is a pretty standard controller action in our codebase. Maybe it might
# get rolled up into a service object but it's unlikely. This code is smelly
# because it's making decisions based on the return value of an object it
# doesn't own.
class DonationsController < ApplicationController
def create
mailer.notify_supporter donation
if charity.active?
mailer.receipt donation
mailer.notify_charity donation
else
mailer.payment_advice donation
mailer.initiate_kyc charity
end
redirect_to somewhere_url
end
end
# Delegate the responsibility of which emails to send to charity. In this
# situation Charity essentially acts as a service object.
class DonationsController < ApplicationController
def create
charity.send_post_donation_notifications donation
redirect_to somewhere_url
end
end
# Now it's charities decision what emails to send depending on its state. What
# is awesome about this code is that charity can define more states with
# different emails (or any type of notification) and the controller + tests do
# not change!
class Charity < ActiveRecord::Base
def send_post_donation_notifications donation, mail = PostDonationMailer.delay
mail.notify_supporter donation
if charity.active?
mail.receipt donation
mail.notify_charity donation
else
mail.payment_advice donation
mail.initiate_kyc self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment