Skip to content

Instantly share code, notes, and snippets.

@joecannatti
Created December 18, 2012 15:55
Show Gist options
  • Save joecannatti/4329206 to your computer and use it in GitHub Desktop.
Save joecannatti/4329206 to your computer and use it in GitHub Desktop.
class Task
attr_accessor :name
attr_accessor :assigned_to_user_id
attr_accessor :due_on
def save
Storer.store(self)
end
end
class EmailInfo
def initialize(obj)
@obj = obj
@type = obj.class
end
def user_id
@obj.send(id_fields[@type])
end
def title
mapped_data[@type][:title]
end
def body
mapped_data[@type][:body]
end
def id_fields
{
Task => :assigned_to_user_id,
SomeThingElseMaybeUser => :id,
MaybeEvent => :user_id
}
end
def email_data
{
Task => { :title => "New Task", :body => "You have been assigned a new task" },
SomeThingElseMaybeUser => { :title => "Thanks for registering", :body => "Have fun" },
MaybeEvent => { :title => "New Event", :body => "Enjoy!" }
}
end
end
class Storer
def self.store(obj)
obj.save!
email_info = TaskEmailInfo.new(obj)
email.send(email_info.user_id, email_info.title, email_info.body)
end
end
@kberridge
Copy link

Suppose you wanted to save a task, but didn't want it to send an email? How would you suppress the email?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment