From 42e20b6a3cadaed55c1899338f9596b35e859eca Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Tue, 15 Nov 2016 10:05:53 +0100 Subject: [PATCH] Automat mail chimp subscription --- Gemfile | 2 ++ Gemfile.lock | 5 +++++ app/controllers/supporters_controller.rb | 17 +++++++++++++++++ app/models/supporter.rb | 2 +- spec/features/support_spec.rb | 3 ++- 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index f0851c1..01421f7 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,7 @@ gem 'puma' gem 'geocoder' gem 'rails_12factor' + gem 'rails_admin' gem 'mongoid_paranoia' @@ -53,6 +54,7 @@ group :development, :test do gem 'rspec-rails', '~> 3.0' gem 'pry-rails' + gem 'dotenv-rails' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 4f17fde..d554a83 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -66,6 +66,10 @@ GEM concurrent-ruby (1.0.2) debug_inspector (0.0.2) diff-lcs (1.2.5) + dotenv (2.1.1) + dotenv-rails (2.1.1) + dotenv (= 2.1.1) + railties (>= 4.0, < 5.1) erubis (2.7.0) excon (0.49.0) execjs (2.7.0) @@ -266,6 +270,7 @@ DEPENDENCIES byebug capybara coffee-rails (~> 4.1.0) + dotenv-rails geocoder jbuilder (~> 2.0) jquery-rails diff --git a/app/controllers/supporters_controller.rb b/app/controllers/supporters_controller.rb index a98f818..a87eb59 100644 --- a/app/controllers/supporters_controller.rb +++ b/app/controllers/supporters_controller.rb @@ -9,6 +9,7 @@ class SupportersController < ApplicationController @supporter = Supporter.new supporter_form_params.merge(language: I18n.locale) if !input_to_fast? && @supporter.save # SupporterMailer.welcome_email(@supporter).deliver_now + mailchimp_registration redirect_to thanks_path else flash.now[:danger] = t '.timeout' if input_to_fast? @@ -21,4 +22,20 @@ class SupportersController < ApplicationController def supporter_form_params params.require(:supporter).permit(:first_name, :last_name, :street, :zip, :email, :support, :li_membership, :age_category, :city, :comments) end + + def mailchimp_registration + mailchimp = Mailchimp::API.new(ENV['MAILCHIMP_API']) + mailchimp.lists.subscribe(ENV['CH420_LIST_ID'], { 'email' => @supporter.email }, merge_vars, 'html', false) + end + + def merge_vars + { 'EMAIL' => @supporter.email, + 'FNAME' => @supporter.first_name, + 'LNAME' => @supporter.last_name, + 'SUPPORT' => @supporter.support, + 'MEMBERSHIP' => @supporter.li_membership.to_s, + 'AGE' => @supporter.age_category, + 'LANG' => @supporter.language, + 'ZIP' => @supporter.zip } + end end diff --git a/app/models/supporter.rb b/app/models/supporter.rb index 7241408..ca9a2ff 100644 --- a/app/models/supporter.rb +++ b/app/models/supporter.rb @@ -4,7 +4,7 @@ class Supporter include Mongoid::Paranoia include Geocoder::Model::Mongoid - COUNTER_START = 10003 + COUNTER_START = 10_003 geocoded_by :address after_validation :geocode # auto-fetch coordinates diff --git a/spec/features/support_spec.rb b/spec/features/support_spec.rb index c906ae8..c1bee31 100644 --- a/spec/features/support_spec.rb +++ b/spec/features/support_spec.rb @@ -21,9 +21,10 @@ feature 'Support announcement' do sleep 5 # Do a real sleep to have a real integration test expect(ActionMailer::Base.deliveries.count).to eq 0 + expect_any_instance_of(SupportersController).to receive(:mailchimp_registration).and_return(true) + expect { click_button 'Unterstützung zusichern' }.to change { Supporter.count }.by(1) # expect(ActionMailer::Base.deliveries.count).to eq 1 - supporter = Supporter.find_by(email: 'blocher@blocher.ch') expect(supporter.coordinates).to eq [8.618589100000001, 47.2926304] expect(supporter.comments).to eq 'Test text'