Created
May 31, 2013 22:43
-
-
Save JKring/5688427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## On BuyerEmailer | |
# Now we don't have to call it in every method... | |
sendgrid_category 'buyer' | |
# This is the hash that connects preference levels to emails | |
@@preference_levels = { | |
claimed: "important" | |
} | |
# This is a class method, meaning you call it like: | |
# BuyerEmailer.check_and_send(:claimed, @business) | |
def self.check_and_send email, *opts | |
# This looks at the class variable and determines the preference level | |
level = @@preference_levels[email] | |
# This assumes business is the first argument to the method. | |
# A lot of these methods will have to be refactored to include business as the first arg | |
business = opts.first | |
# Then it calls the method on itself | |
# using send http://ruby-doc.org/core-2.0/Object.html#method-i-send | |
# ONLY IF the business isn't unsubscribed from that level. | |
send(email, *opts).deliver if business.check_prefs level | |
end | |
# On Business | |
# You'll have to refactor check_prefs to take in an optional argument of the given level | |
def check_prefs level=nil | |
if email_preferences | |
return false if email_preferences["ignore_all"] == true | |
return false if level && email_preferences["ignore_#{ level }"] == true | |
end | |
setup_unsubscribe_token | |
return true | |
end | |
def setup_unsubscribe_token | |
unless unsubscribe_token | |
self.unsubscribe_token = SecureRandom.hex(16) | |
self.save | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment