diff --git a/Gemfile b/Gemfile index 7711cf9..240c59a 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,7 @@ gem 'simple_form' gem 'capybara' gem 'rails-i18n' gem 'puma' +gem 'geocoder' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index a4a73c6..4d0bef2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -66,6 +66,7 @@ GEM diff-lcs (1.2.5) erubis (2.7.0) execjs (2.6.0) + geocoder (1.2.11) globalid (0.3.6) activesupport (>= 4.1.0) i18n (0.7.0) @@ -217,6 +218,7 @@ DEPENDENCIES byebug capybara coffee-rails (~> 4.1.0) + geocoder jbuilder (~> 2.0) jquery-rails mongoid (~> 5.0.0) diff --git a/app/models/supporter.rb b/app/models/supporter.rb index 5a51e3f..0c257d8 100644 --- a/app/models/supporter.rb +++ b/app/models/supporter.rb @@ -1,6 +1,10 @@ class Supporter include Mongoid::Document include Mongoid::Timestamps + include Geocoder::Model::Mongoid + + geocoded_by :address + after_validation :geocode # auto-fetch coordinates field :first_name, type: String field :last_name, type: String @@ -10,6 +14,7 @@ class Supporter field :email, type: String field :support, type: String # Indicates the type of support field :age_category, type: String + field :coordinates, type: Array validates :first_name, presence: true validates :last_name, presence: true @@ -19,4 +24,8 @@ class Supporter validates :email, presence: true, uniqueness: true validates :support, presence: true validates :age_category, presence: true + + def address + "#{street}, #{zip}, #{city}, Switzerland" + end end diff --git a/spec/features/support_spec.rb b/spec/features/support_spec.rb index b387006..f70089c 100644 --- a/spec/features/support_spec.rb +++ b/spec/features/support_spec.rb @@ -9,7 +9,7 @@ feature 'Support announcement' do fill_in 'Nachname', with: 'Blocher' fill_in 'Strasse/Nr.', with: 'Wängirain 53' fill_in 'Postleitzahl', with: '8704' - fill_in 'Ort', with: 'Hinterm Mond' + fill_in 'Ort', with: 'Herrliberg' fill_in 'E-Mail', with: 'blocher@blocher.ch' choose 'Ich unterschreibe eine Volksinitiative für die Freigabe von Cannabis und helfe aktiv Unterschriften in meiner Umgebung zu sammeln.' @@ -17,6 +17,8 @@ feature 'Support announcement' do choose 'Ich bin über 64 Jahre alt' expect { click_button 'Unterstützung zusichern' }.to change { Supporter.count }.by(1) + + expect(Supporter.find_by(email: 'blocher@blocher.ch').coordinates).to eq [8.618589100000001, 47.2926304] end scenario 'A bot tries to enter data' do