Skip to content

Commit

Permalink
subscribe user to mailchimp after signup
Browse files Browse the repository at this point in the history
  • Loading branch information
hstove committed Dec 31, 2013
1 parent 0174f78 commit 4961da3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/models/user.rb
Expand Up @@ -34,6 +34,8 @@ def enrolled_courses
if Rails.env.production? && !(how_to = Course.find_by_id(117)).blank? if Rails.env.production? && !(how_to = Course.find_by_id(117)).blank?
Rails.configuration.queue << Afterparty::BasicJob.new(self, :enroll, how_to) Rails.configuration.queue << Afterparty::BasicJob.new(self, :enroll, how_to)
end end
job = Afterparty::BasicJob.new(self, :subscribe_to_mailchimp)
Rails.configuration.queue << job
end end


validates_presence_of :username, :email validates_presence_of :username, :email
Expand Down Expand Up @@ -167,4 +169,16 @@ def generate_token(column)
self[column] = SecureRandom.urlsafe_base64 self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column]) end while User.exists?(column => self[column])
end end

def subscribe_to_mailchimp testing=false
return true if (Rails.env.test? && !testing)
list_id = ENV['MAILCHIMP_ULUDUM_LIST_ID']

response = Rails.configuration.mailchimp.lists.subscribe({
id: list_id,
email: {email: email},
double_optin: false,
})
response
end
end end
14 changes: 14 additions & 0 deletions spec/models/user_spec.rb
Expand Up @@ -173,4 +173,18 @@ def new_user(attributes = {})
last_email.to.should include (user.email) last_email.to.should include (user.email)
end end
end end

describe "#subscribe_to_mailchimp" do
let(:user) { create(:user) }
it "calls mailchimp correctly" do
opts = {
email: {email: user.email},
id: ENV['MAILCHIMP_ULUDUM_LIST_ID'],
double_optin: false,
}
clazz = Rails.configuration.mailchimp.lists.class
clazz.any_instance.should_receive(:subscribe).with(opts).once
user.send(:subscribe_to_mailchimp, true)
end
end
end end

0 comments on commit 4961da3

Please sign in to comment.