diff --git a/Gemfile b/Gemfile index b82ba44..748ba79 100755 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,9 @@ gem 'sdoc', '~> 0.4.0', group: :doc # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +gem 'sidekiq' +gem 'gibbon' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' @@ -45,3 +48,6 @@ group :development do gem 'spring' end +group :test do + gem 'webmock' +end diff --git a/Gemfile.lock b/Gemfile.lock index ca28e0e..5c39b0f 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -35,6 +35,8 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + addressable (2.5.2) + public_suffix (>= 2.0.2, < 4.0) angular_rails_csrf (1.0.4) rails (>= 3, < 5) angularjs-rails (1.4.8) @@ -51,14 +53,23 @@ GEM execjs coffee-script-source (1.10.0) concurrent-ruby (1.0.5) + connection_pool (2.2.2) + crack (0.4.3) + safe_yaml (~> 1.0.0) crass (1.0.4) debug_inspector (0.0.2) diff-lcs (1.3) erubis (2.7.0) execjs (2.6.0) + faraday (0.15.2) + multipart-post (>= 1.2, < 3) ffi (1.9.23) + gibbon (3.2.0) + faraday (>= 0.9.1) + multi_json (>= 1.11.0) globalid (0.4.1) activesupport (>= 4.2.0) + hashdiff (0.3.7) i18n (0.9.5) concurrent-ruby (~> 1.0) jbuilder (2.4.0) @@ -74,9 +85,13 @@ GEM mini_portile2 (2.3.0) minitest (5.11.3) multi_json (1.11.2) + multipart-post (2.0.0) nokogiri (1.8.2) mini_portile2 (~> 2.3.0) + public_suffix (3.0.2) rack (1.6.9) + rack-protection (2.0.3) + rack rack-test (0.6.3) rack (>= 1.0) rails (4.2.10) @@ -109,6 +124,7 @@ GEM ffi (>= 0.5.0, < 2) rdoc (4.2.1) json (~> 1.4) + redis (4.0.1) rspec-core (3.7.1) rspec-support (~> 3.7.0) rspec-expectations (3.7.0) @@ -126,6 +142,7 @@ GEM rspec-mocks (~> 3.7.0) rspec-support (~> 3.7.0) rspec-support (3.7.1) + safe_yaml (1.0.4) sass (3.5.5) sass-listen (~> 4.0.0) sass-listen (4.0.0) @@ -140,6 +157,11 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + sidekiq (5.1.3) + concurrent-ruby (~> 1.0) + connection_pool (~> 2.2, >= 2.2.0) + rack-protection (>= 1.5.0) + redis (>= 3.3.5, < 5) spring (1.6.2) sprockets (3.7.1) concurrent-ruby (~> 1.0) @@ -162,6 +184,10 @@ GEM binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) + webmock (3.4.2) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff PLATFORMS ruby @@ -171,15 +197,18 @@ DEPENDENCIES angularjs-rails byebug coffee-rails (~> 4.1.0) + gibbon jbuilder (~> 2.0) rails (= 4.2.10) rspec-rails sass-rails (~> 5.0) sdoc (~> 0.4.0) + sidekiq spring sqlite3 uglifier (>= 1.3.0) web-console (~> 2.0) + webmock BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/app/controllers/entries_controller.rb b/app/controllers/entries_controller.rb index 14d6cb1..62be2d5 100755 --- a/app/controllers/entries_controller.rb +++ b/app/controllers/entries_controller.rb @@ -5,6 +5,7 @@ def create @entry = Entry.new(entry_params) if @entry.save + MailingListSubscriberJob.perform_later @entry.id render json: {success: true} else render json: {success: false, errors: @entry.errors} @@ -14,6 +15,6 @@ def create private # Never trust parameters from the scary internet, only allow the white list through. def entry_params - params.require(:entry).permit(:competition_id, :name, :email) + params.require(:entry).permit(:competition_id, :given_name, :family_name, :email) end end diff --git a/app/jobs/mailing_list_subscriber_job.rb b/app/jobs/mailing_list_subscriber_job.rb new file mode 100644 index 0000000..a0cd205 --- /dev/null +++ b/app/jobs/mailing_list_subscriber_job.rb @@ -0,0 +1,12 @@ +class MailingListSubscriberJob < ActiveJob::Base + queue_as :default + + def perform(entry_id) + entry = Entry.find(entry_id) + + MailingList + .find(entry.competition.mailing_list_id) + .add_subscriber(given_name: entry.given_name, family_name: entry.family_name, + email: entry.email) + end +end diff --git a/app/models/entry.rb b/app/models/entry.rb index 9f690f4..3e91009 100755 --- a/app/models/entry.rb +++ b/app/models/entry.rb @@ -8,7 +8,7 @@ class Entry < ActiveRecord::Base validates_presence_of :competition, inverse_of: :entries validates_presence_of :email validates_format_of :email, :with => EMAIL_REGEX, allow_blank: true, allow_nil: true - validates_presence_of :name, if: :requires_name + validates_presence_of :given_name, :family_name, if: :requires_name validates_uniqueness_of :email, scope: :competition, message: "has already entered this competition" private diff --git a/app/views/competitions/entrant_page.erb b/app/views/competitions/entrant_page.erb index 262c316..41691b1 100755 --- a/app/views/competitions/entrant_page.erb +++ b/app/views/competitions/entrant_page.erb @@ -6,7 +6,7 @@