Send reminder emails and update reminder sent state

This commit is contained in:
Erik Michaels-Ober 2011-06-25 02:38:00 -07:00
parent 698e42c025
commit 65a80d2d9e
9 changed files with 70 additions and 15 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,2 +0,0 @@
class ReminderMailer < ActionMailer::Base
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,4 @@
require 'test_helper'
class HydrantMailerTest < ActionMailer::TestCase
end

View File

@ -1,4 +0,0 @@
require 'test_helper'
class ReminderMailerTest < ActionMailer::TestCase
end