From 65a80d2d9e698d82fca4b8321c6b9d5d36de8d8b Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Sat, 25 Jun 2011 02:38:00 -0700 Subject: [PATCH] Send reminder emails and update reminder sent state --- app/controllers/addresses_controller.rb | 2 +- app/controllers/reminders_controller.rb | 7 +-- app/mailers/hydrant_mailer.rb | 13 ++++++ app/mailers/reminder_mailer.rb | 2 - app/models/address.rb | 2 +- app/models/hydrant.rb | 44 ++++++++++++++++++- .../hydrant_mailer/reminder_email.text.erb | 7 +++ test/functional/hydrant_mailer_test.rb | 4 ++ test/functional/reminder_mailer_test.rb | 4 -- 9 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 app/mailers/hydrant_mailer.rb delete mode 100644 app/mailers/reminder_mailer.rb create mode 100644 app/views/hydrant_mailer/reminder_email.text.erb create mode 100644 test/functional/hydrant_mailer_test.rb delete mode 100644 test/functional/reminder_mailer_test.rb diff --git a/app/controllers/addresses_controller.rb b/app/controllers/addresses_controller.rb index 4e21e8e..af4adec 100644 --- a/app/controllers/addresses_controller.rb +++ b/app/controllers/addresses_controller.rb @@ -2,7 +2,7 @@ class AddressesController < ApplicationController respond_to :json def show - @address = Address.find_lat_lng("#{params[:address]}, #{params[:city_state]}") + @address = Address.geocode("#{params[:address]}, #{params[:city_state]}") unless @address.blank? respond_with @address else diff --git a/app/controllers/reminders_controller.rb b/app/controllers/reminders_controller.rb index fd2d851..aec755a 100644 --- a/app/controllers/reminders_controller.rb +++ b/app/controllers/reminders_controller.rb @@ -2,16 +2,13 @@ class RemindersController < ApplicationController respond_to :json def create - puts params[:reminder].inspect @reminder = Reminder.new(params[:reminder]) - if @reminder.save + HydrantMailer.reminder_email(@reminder.hydrant).deliver + @reminder.update_attribute(:sent, true) render(:json => @reminder) else render(:json => {"errors" => @reminder.errors}, :status => 500) end - # ReminderMailer.send_reminder - # @reminder.sent = true - # @reminder.save! end end diff --git a/app/mailers/hydrant_mailer.rb b/app/mailers/hydrant_mailer.rb new file mode 100644 index 0000000..db19e5b --- /dev/null +++ b/app/mailers/hydrant_mailer.rb @@ -0,0 +1,13 @@ +class HydrantMailer < ActionMailer::Base + default :from => "hello@#{default_url_options[:host]}" + + def reminder_email(hydrant) + @hydrant = hydrant + @user = hydrant.user + mail( + :to => hydrant.user.email, + :from => "reminder@#{default_url_options[:host]}", + :subject => ["Remember to Shovel", hydrant.name].compact.join(' '), + ) + end +end diff --git a/app/mailers/reminder_mailer.rb b/app/mailers/reminder_mailer.rb deleted file mode 100644 index 8cf94e6..0000000 --- a/app/mailers/reminder_mailer.rb +++ /dev/null @@ -1,2 +0,0 @@ -class ReminderMailer < ActionMailer::Base -end diff --git a/app/models/address.rb b/app/models/address.rb index 577d12f..582d364 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -1,7 +1,7 @@ class Address include Geokit::Geocoders - def self.find_lat_lng(address) + def self.geocode(address) MultiGeocoder.geocode(address).ll.split(',').map{|s| s.to_f} end end diff --git a/app/models/hydrant.rb b/app/models/hydrant.rb index ec6d717..51ba04a 100644 --- a/app/models/hydrant.rb +++ b/app/models/hydrant.rb @@ -1,4 +1,5 @@ class Hydrant < ActiveRecord::Base + include Geokit::Geocoders validates_presence_of :lat, :lng belongs_to :user has_many :reminders @@ -13,8 +14,47 @@ class Hydrant < ActiveRecord::Base Hydrant.find_by_sql([query, lat.to_f, lng.to_f, lat.to_f, limit.to_i]) end - def adopted? - !user.nil? + def reverse_geocode + @reverse_geocode ||= MultiGeocoder.reverse_geocode([lat, lng]) end + def street_number + reverse_geocode.street_number + end + + def street_name + reverse_geocode.street_name + end + + def street_address + reverse_geocode.street_address + end + + def city + reverse_geocode.city + end + + def state + reverse_geocode.state + end + + def zip + reverse_geocode.zip + end + + def country_code + reverse_geocode.country_code + end + + def country + reverse_geocode.country + end + + def full_address + reverse_geocode.full_address + end + + def adopted? + !user_id.nil? + end end diff --git a/app/views/hydrant_mailer/reminder_email.text.erb b/app/views/hydrant_mailer/reminder_email.text.erb new file mode 100644 index 0000000..60f566a --- /dev/null +++ b/app/views/hydrant_mailer/reminder_email.text.erb @@ -0,0 +1,7 @@ +<%= @hydrant.user.name.split.first %>, + +Did you remember to shovel out <%= @hydrant.name %>, the fire hydrant you adopted at <%= @hydrant.street_address %>? If not, please shovel your hydrant as soon as possible! + +If you've already shoveled out <%= @hydrant.name %>, thank you for serving your city! + +Mayor Tom Menino diff --git a/test/functional/hydrant_mailer_test.rb b/test/functional/hydrant_mailer_test.rb new file mode 100644 index 0000000..74bf567 --- /dev/null +++ b/test/functional/hydrant_mailer_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class HydrantMailerTest < ActionMailer::TestCase +end diff --git a/test/functional/reminder_mailer_test.rb b/test/functional/reminder_mailer_test.rb deleted file mode 100644 index 4dfbb98..0000000 --- a/test/functional/reminder_mailer_test.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'test_helper' - -class ReminderMailerTest < ActionMailer::TestCase -end