Skip to content

Commit

Permalink
Merge pull request #25 from auxesis/feature/email-alerts
Browse files Browse the repository at this point in the history
Email alerts, with human readable addresses
  • Loading branch information
auxesis committed Oct 15, 2016
2 parents d50c020 + 2d339be commit 5784cc6
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 38 deletions.
11 changes: 11 additions & 0 deletions db/migrations/012_add_address_column_to_alerts.rb
@@ -0,0 +1,11 @@
class AddAddressColumnToAlerts< Sequel::Migration
def up
alter_table(:alerts) do
add_column :address, String
end
end

def down
end
end

4 changes: 0 additions & 4 deletions lib/gotgastro/models/alert.rb
Expand Up @@ -10,8 +10,4 @@ def location=(value)
self.lat = lat
self.lng = lng
end

def address
"123 Straight Street, Greenway, 2001"
end
end
8 changes: 8 additions & 0 deletions lib/gotgastro/services/alert_signup_service.rb
Expand Up @@ -9,20 +9,27 @@ class AlertSignupService

attribute :email, String
attribute :location, String
attribute :address, String
attribute :distance, Float
attribute :alert, Alert
attribute :mail, Mail

validates :email, presence: { :message => 'Bummer! We need an email address to create an alert for you.' }
validates :location, presence: {}
validates :distance, presence: {}
validates :address, presence: { :message => "Sorry! We need an address to create an alert." }

def initialize(opts)
@host = opts[:host] || 'https://gotgastroagain.com'
@alert = opts[:alert] if opts[:alert]
super(opts)
end

def address
return @alert.address if @alert
super
end

def distance
return @alert.distance if @alert
super
Expand Down Expand Up @@ -68,6 +75,7 @@ def persist!
:email => email,
:location => location,
:distance => distance,
:address => address,
:confirmation_id => Digest::MD5.new.hexdigest("#{rand(Time.now.to_i).to_s}-#{email}")
}
@alert = Alert.create(attrs)
Expand Down
3 changes: 2 additions & 1 deletion lib/gotgastro/workers/email_alerts.rb
Expand Up @@ -9,7 +9,8 @@ class EmailAlerts
include Sidekiq::Worker

def perform
Alert.where{confirmed_at !~ nil}.where(:unsubscribed_at => nil).each do |alert|
alerts = Alert.where{confirmed_at !~ nil}.where(:unsubscribed_at => nil)
alerts.each do |alert|
businesses = Business.find_near(alert.location, :within => alert.distance)
conditions = { Sequel.qualify(:offences, :created_at) => Time.now.beginning_of_day..Time.now.end_of_day }
offences = Offence.join(businesses, :id => :business_id).where{conditions}.all
Expand Down
1 change: 1 addition & 0 deletions lib/public/css/main.css
Expand Up @@ -23,6 +23,7 @@ button.geolocation {
}
}

/* search results page */
div.row.landing p {
margin-bottom: 15px;
}
Expand Down
24 changes: 19 additions & 5 deletions lib/public/js/index.js
Expand Up @@ -12,8 +12,13 @@ function initAutocomplete() {

function searchFromAddress() {
var place = autocomplete.getPlace();
var coords = { lat: place.geometry.location.lat(), lng: place.geometry.location.lng() },
query = jQuery.param(coords);
var location = {
lat: place.geometry.location.lat(),
lng: place.geometry.location.lng(),
address: place.formatted_address
},
query = jQuery.param(location);

window.location = window.location + 'search?' + query;
}

Expand Down Expand Up @@ -48,9 +53,18 @@ $(document).ready(function() {

function showPosition(position) {
if (position.coords) {
var coords = { lat: position.coords.latitude, lng: position.coords.longitude },
query = jQuery.param(coords);
window.location = window.location + 'search?' + query;
console.log('position', position)
var location = { lat: position.coords.latitude, lng: position.coords.longitude },
geocoder = new google.maps.Geocoder;

geocoder.geocode({'location': location}, function(results, status) {
if (status === 'OK') {
location.address = results[0].formatted_address
}
var query = jQuery.param(location);
window.location = window.location + 'search?' + query;
})

} else {
var error = "<i class='fa fa-bomb fa-fw'></i> Couldn't find your location.";
$('button.geolocation').html(error);
Expand Down
11 changes: 5 additions & 6 deletions lib/views/alert.haml
Expand Up @@ -7,18 +7,17 @@
Now check your email
%p
You should shortly receive an email from&nbsp;
%code>= @alert.mail.from
%code>= @alert.mail.from.first
\.
%p
%strong Click on the link in the email
to confirm and start receiving alerts about food safety problems in
your area.
to confirm and start receiving alerts about food safety problems within
%em&= "#{@alert.distance}km"
of
%em&= @alert.address
%p
If you don't get an email in the next few minutes, try checking your spam filter.

%pre
= params.inspect

%div.row.nav
%div.col-md-12
%p
Expand Down
4 changes: 2 additions & 2 deletions lib/views/alert_confirmation.haml
Expand Up @@ -9,9 +9,9 @@
%p
You will now receive email alerts for any planning applications
we find within
= "#{@alert.distance}km"
%em&= "#{@alert.distance}km"
of
= @alert.location
%em&= @alert.address
%p
You can change the size of the area covered by the alert now or
at a later time. Every email alert will have this link.
Expand Down
37 changes: 20 additions & 17 deletions lib/views/search.haml
Expand Up @@ -8,27 +8,30 @@
- map_url = "#{img_src}&markers=#{markers}&key=#{gmaps_api_key}"

%div.container
%h1 Food safety problems near me
%h1
Food safety problems near:
%small.address&= params[:address]
%div.row
%div.col-md-12
%p.map
%img{:src => map_url}/
- if params[:alert]
%div.row.email-alert
%div.col-md-12
%div.well.well-lg
%form{:action => link_to('/alert'), :method => 'post'}
%div.form-group
%p
Get alerts for new food problems near here straight in your email. It's free!
%div.form-group
%p
%input.form-control.input-lg{:name => 'alert[email]', :placeholder => 'me@example.org', :type => 'email'}
%input{:type => 'hidden', :name => "alert[location]", :value => "#{@location.lat},#{@location.lng}"}
%input{:type => 'hidden', :name => "alert[distance]", :value => "5"}
%button{:class => 'btn btn-success btn-lg btn-block', :type => 'submit'}
%i.fa.fa-fw.fa-envelope
Create alert
- if params[:address]
%div.row.email-alert
%div.col-md-12
%div.well.well-lg
%form{:action => link_to('/alert'), :method => 'post'}
%div.form-group
%p
Get alerts for new food problems near here straight in your email. It's free!
%div.form-group
%p
%input.form-control.input-lg{:name => 'alert[email]', :placeholder => 'me@example.org', :type => 'email'}
%input{:type => 'hidden', :name => "alert[location]", :value => "#{@location.lat},#{@location.lng}"}
%input{:type => 'hidden', :name => "alert[distance]", :value => "5"}
%input{:type => 'hidden', :name => "alert[address]", :value => params[:address]}
%button{:class => 'btn btn-success btn-lg btn-block', :type => 'submit'}
%i.fa.fa-fw.fa-envelope
Create alert

%div.row.results
- if @businesses.size > 0
Expand Down
6 changes: 3 additions & 3 deletions spec/alerts_spec.rb
Expand Up @@ -9,7 +9,7 @@
let(:subscribed_user) {
within_25km && within_150km

visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&alert=hello"
visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&address=foobar"
fill_in 'alert[email]', :with => 'subscribed@example.org'
click_on 'Create alert'
GotGastro::Workers::EmailWorker.drain
Expand All @@ -29,7 +29,7 @@
let(:unconfirmed_user) {
within_25km && within_150km

visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&alert=hello"
visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&address=foobar"
fill_in 'alert[email]', :with => 'unconfirmed@example.org'
click_on 'Create alert'
GotGastro::Workers::EmailWorker.drain
Expand All @@ -41,7 +41,7 @@
let(:unsubscribed_user) {
within_25km && within_150km

visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&alert=hello"
visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&address=foobar"
fill_in 'alert[email]', :with => 'unsubscribed@example.org'
click_on 'Create alert'
GotGastro::Workers::EmailWorker.drain
Expand Down

0 comments on commit 5784cc6

Please sign in to comment.