Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Commit

Permalink
Move Mailchimp account creation to a User model callback instead of o…
Browse files Browse the repository at this point in the history
…n the controller
  • Loading branch information
jkongie committed Apr 19, 2012
1 parent e3fbb0e commit 4bf8380
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/controllers/spree/users_controller_decorator.rb
@@ -1,6 +1,5 @@
Spree::UsersController.class_eval do

after_filter :create_in_mailchimp, :only => [:create]
after_filter :update_in_mailchimp, :only => [:update]
# destroy.after :remove_from_mailchimp # can use r_c?

Expand Down Expand Up @@ -42,6 +41,9 @@ def mc_list_id
private

def create_in_mailchimp
logger.info "========================="
logger.info "Mail list subscriber? #{@user.is_mail_list_subscriber}"
logger.info "========================="
return unless @user.is_mail_list_subscriber

Spree::User.benchmark "Adding mailchimp subscriber (list id=#{mc_list_id})" do
Expand All @@ -59,6 +61,7 @@ def create_in_mailchimp
rescue Hominid::APIError => e
# TODO alert someone there is a problem with mailchimp
logger.warn "MailChimp::Sync: Failed to create contact in mailchimp: #{e.message}"

end

def remove_from_mailchimp
Expand Down
84 changes: 84 additions & 0 deletions app/models/user_decorator.rb
@@ -1,5 +1,89 @@
Spree::User.class_eval do

before_create :mailchimp_add_to_mailing_list
#before_update :mailchimp_update_mailing_list, :if => :is_mail_list_subscriber_changed?

attr_accessible :is_mail_list_subscriber

private

def mailchimp_add_to_mailing_list
if self.is_mail_list_subscriber?
begin
hominid.list_subscribe(mailchimp_list_id, self.email, mailchimp_merge_vars, 'html', *mailchimp_subscription_opts)
logger.debug "Fetching new mailchimp subscriber info"

assign_mailchimp_subscriber_id
rescue Hominid::APIError => e
logger.warn "SpreeMailChimp: Failed to create contact in Mailchimp: #{e.message}"
end
end
end

# Retrieves and stores the Mailchimp member id
#
# Returns the Mailchimp ID
def assign_mailchimp_subscriber_id
begin
response = hominid.list_member_info(mailchimp_list_id, [self.email]).with_indifferent_access

if response[:success] == 1
member = response[:data][0]

self.mailchimp_subscriber_id = member[:id]
end
rescue Hominid::APIError => e
logger.warn "SpreeMailChimp: Failed to retrieve and store Mailchimp ID: #{e.message}"
end
end

# Creates an instance of the Hominid::API
#
# Returns Hominid::API
def hominid
@hominid ||= Hominid::API.new(Spree::Config.get(:mailchimp_api_key))
end

# Gets the Mailchimp list ID that is stored in Spree::Config
#
# Returns a Mailchimp list ID String
def mailchimp_list_id
@mailchimp_list_id ||= Spree::Config.get(:mailchimp_list_id)
end

# Generates the subsubcription options for the application
#
# The option values are returned as an Array in the following order:
#
# double_optin - Flag to control whether a double opt-in confirmation
# message is sent, defaults to true. Abusing this may
# cause your account to be suspended. (default: false)
# update_existing - Flag to control whether existing subscribers should be
# updated instead of throwing an error. (default: true)
# ( MailChimp's default is false )
# replace_interests - Flag to determine whether we replace the interest
# groups with the groups provided or we add the provided
# groups to the member's interest groups. (default: true)
# send_welcome - If the double_optin is false and this is true, a welcome
# email is sent out. If double_optin is true, this has no
# effect. (default: false)
#
# Returns an Array of subscription options
#
# TODO: Add configuration options for 'update_existing' and 'replace_interests'
# TODO: Remove configuration options for :mailchimp_send_notify
def mailchimp_subscription_opts
[Spree::Config.get(:mailchimp_double_opt_in), true, true, Spree::Config.get(:mailchimp_send_welcome)]
end

def mailchimp_merge_vars
merge_vars = {}
if mailchimp_merge_user_attribs = Spree::Config.get(:mailchimp_merge_vars)
mailchimp_merge_user_attribs.split(',').each do |method|
merge_vars[method.upcase] = self.send(method.downcase) if @user.respond_to? method.downcase
end
end
merge_vars
end

end

0 comments on commit 4bf8380

Please sign in to comment.