Skip to content
Browse files

allowing alerts to be sent at anytime during the day not just on firs…

…t report
  • Loading branch information...
1 parent 8a7e20c commit 399894c4cfbc0352d1187abc01f515287d75cda5 @timmyc committed Jan 17, 2012
View
8 app/models/alert.rb
@@ -3,8 +3,12 @@ class Alert < ActiveRecord::Base
belongs_to :area
belongs_to :shredder
belongs_to :subscription
+ before_create :set_defaults
after_create :deliver
+ scope :for_subscription_date, lambda{|date,subscription_id| where("date_sent = ? and subscription_id = ?", date, subscription_id)}
+ scope :for_area_date, lambda{|date,area_id| where("date_sent = ? and area_id = ?", date, area_id)}
+
def deliver
Delayed::Job.enqueue Dispatch.new(self.id)
end
@@ -14,4 +18,8 @@ def send_message
self.subscription.send_message(self)
self.update_attribute('sent',true)
end
+
+ def set_defaults
+ self.date_sent = Time.now.to_date
+ end
end
View
2 app/models/shredder.rb
@@ -18,7 +18,7 @@ class Shredder < ActiveRecord::Base
validates_uniqueness_of :mobile, :scope => :area_id
scope :notices_for, lambda{|inches,area_id| where("area_id = ? and inches <= ?",area_id,inches)}
GREETS = ['SICKBURD','Gaper','Powderpuff','Powstar','Child of Ullr','Brah','Bro','Gnarsef','Brosef','Sickter','Shredder','Gaper Gapper','Shredhead','Freshie Fiend','Snow Bunny','Snow Angel','Conehead']
- DEFAULT_MESSAGE = 'FRESHIEZ! {{area}} is reporting {{new_snow}}" in the last 12 hours. Base Temp: {{base_temp}}. Reported At: {{report_time}}'
+ DEFAULT_MESSAGE = 'FRESHIEZ! {{area}} is reporting {{new_snow}}" of new snow. Base Temp: {{base_temp}}. Reported At: {{report_time}}'
def send_confirmation
Twilio.connect(Cone::Application.config.twilio_sid, Cone::Application.config.twilio_auth)
View
10 app/models/snow_report.rb
@@ -11,11 +11,11 @@ def shout
end
def send_notifications
- if self.first_report
- subscriptions = Subscription.for_inches_area(self.snowfall_twelve,self.area_id)
- subscriptions.each do |s|
- self.alerts.create(:shredder_id => s.shredder_id, :area_id => self.area_id, :subscription_id => s.id)
- end
+ subscriptions = Subscription.for_inches_area(self.snowfall_twelve,self.area_id)
+ alerts_sent = Alert.for_area_date(self.report_time.to_date, self.area_id).collect{|a| a.subscription_id }
+ subscriptions.each do |s|
+ next if alerts_sent.include?(s.id)
+ self.alerts.create(:shredder_id => s.shredder_id, :area_id => self.area_id, :subscription_id => s.id)
end
end
View
17 db/migrate/20120116001059_alertdate.rb
@@ -0,0 +1,17 @@
+class Alertdate < ActiveRecord::Migration
+ def up
+ add_column :alerts, :date_sent, :date
+ add_column :subscriptions, :hour, :int
+ add_index :alerts, :date_sent
+ add_index :subscriptions, :hour
+ Alert.all.each do |alert|
+ next unless alert.snow_report
+ alert.update_attribute('date_sent', alert.snow_report.report_time.to_date)
+ end
+ end
+
+ def down
+ remove_column :alerts, :date_sent
+ remove_column :subscriptions, :hour
+ end
+end
View
8 db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120106051011) do
+ActiveRecord::Schema.define(:version => 20120116001059) do
create_table "alerts", :force => true do |t|
t.integer "shredder_id"
@@ -20,8 +20,11 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "subscription_id"
+ t.date "date_sent"
end
+ add_index "alerts", ["date_sent"], :name => "index_alerts_on_date_sent"
+
create_table "areas", :force => true do |t|
t.string "name"
t.string "key"
@@ -110,6 +113,9 @@
t.datetime "updated_at"
t.string "intro"
t.string "gender"
+ t.integer "hour"
end
+ add_index "subscriptions", ["hour"], :name => "index_subscriptions_on_hour"
+
end
View
1 spec/factories/alert.rb
@@ -3,6 +3,7 @@
factory :alert do
association :snow_report
association :shredder
+ association :subscription
end
end
View
2 spec/factories/subscriptions.rb
@@ -4,7 +4,7 @@
factory :subscription do
shredder_id ""
area_id ""
- inches ""
+ inches "4"
type ""
message 'FRESHIEZ! Bachy is reporting {{new_snow}}" in the last 12 hours. Base Temp: {{base_temp}}. Reported At: {{report_time}}'
end
View
22 spec/models/alert_spec.rb
@@ -12,6 +12,28 @@
@alert.save
end
+ context 'date sent' do
+ before do
+ @alert = Factory.build(:alert)
+ end
+
+ it "should have the column" do
+ @alert.should respond_to(:date_sent)
+ end
+
+ it "should populate the value on create" do
+ @alert.save
+ @alert.date_sent.should eql(Time.now.to_date)
+ end
+
+ it "should return the proper records for for_subscription_date" do
+ Alert.for_subscription_date(Time.now.to_date,@alert.subscription_id).size.should eql(0)
+ @alert.save
+ Alert.for_subscription_date(Time.now.to_date,@alert.subscription_id).size.should eql(1)
+ end
+
+ end
+
context 'alert types' do
before do
@area = Factory.create(:area)
View
28 spec/models/snow_report_spec.rb
@@ -10,6 +10,8 @@
@shredder = Factory.create(:shredder, :area => @area)
@shredder.mobile_confirm
@snow_report = Factory.build(:snow_report, :area => @area, :report_time => Time.now)
+ @shredder2 = Factory.create(:shredder, :area => @area, :inches => 8)
+ @shredder2.mobile_confirm
end
context "scopes" do
@@ -68,6 +70,14 @@
@snow_report.save
}.to change(@snow_report.alerts, :size).by(1)
end
+ it "should not create alerts after create if alerts already sent for subscription and snowfall is >= 1" do
+ @snow_report = Factory.build(:snow_report, :area => @area, :report_time => Time.now, :snowfall_twelve => 2)
+ @snow_report.save
+ @snow_report2 = Factory.build(:snow_report, :area => @area, :report_time => Time.now + 2.minutes, :snowfall_twelve => 2)
+ expect {
+ @snow_report2.save
+ }.to change(@snow_report2.alerts, :size).by(0)
+ end
it "should only send alerts to shredders that want to be notified" do
@shredder2 = Factory.create(:shredder, :area => @area, :inches => 10, :email => 'jah@brah.com', :mobile => '4513501234')
@shredder2.mobile_confirm
@@ -76,5 +86,23 @@
@snow_report.save
}.to change(@snow_report.alerts, :size).by(1)
end
+ it "should send an alert for shredders when threshold is reached" do
+ @snow_report = Factory.build(:snow_report, :area => @area, :report_time => Time.now.to_date, :snowfall_twelve => 2)
+ expect {
+ @snow_report.save
+ }.to change(@snow_report.alerts, :size).by(1)
+ @snow_report2 = Factory.build(:snow_report, :area => @area, :report_time => Time.now + 2.minutes, :snowfall_twelve => 5)
+ expect {
+ @snow_report2.save
+ }.to change(@snow_report2.alerts, :size).by(0)
+ @snow_report3 = Factory.build(:snow_report, :area => @area, :report_time => Time.now + 2.minutes, :snowfall_twelve => 8)
+ expect {
+ @snow_report3.save
+ }.to change(@snow_report3.alerts, :size).by(1)
+ @snow_report4 = Factory.build(:snow_report, :area => @area, :report_time => Time.now + 20.minutes, :snowfall_twelve => 8)
+ expect {
+ @snow_report4.save
+ }.to change(@snow_report4.alerts, :size).by(0)
+ end
end
end

0 comments on commit 399894c

Please sign in to comment.
Something went wrong with that request. Please try again.