Skip to content

Commit bc66e38

Browse files
author
Austin Schneider
committed
revert alert record
1 parent a8e7d10 commit bc66e38

File tree

7 files changed

+30
-53
lines changed

7 files changed

+30
-53
lines changed

app/controllers/alerts_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def create
1111

1212
# TODO: test
1313
def alert
14-
@alert ||= current_user.build_alert_from_params(params)
14+
@alert ||= current_user.build_alert(params)
1515
end
1616

1717
end

lib/pinger/alert.rb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
require 'alert/record'
1+
class Alert < ActiveRecord::Base
22

3-
class Alert
3+
belongs_to :location
4+
belongs_to :email_callback
45

5-
class << self
6-
def build(attrs)
7-
Record.new attrs
8-
end
6+
validates :times,
7+
presence: true,
8+
numericality: {
9+
greater_than: 0,
10+
if: lambda { times.present? }
11+
}
12+
validates :code_is_not, presence: true
13+
validates :email_callback_id, presence: true
914

10-
delegate :joins, :scoped, :unscoped, :table_name,
11-
:to => Record
15+
def conditions_met?
16+
pings = location.pings.order { performed_at.desc }.limit times
17+
pings.size == times && pings.all? { |p| p.response_status_code != code_is_not }
18+
end
19+
20+
def deliver!
21+
Mailer.notification(self).deliver
22+
end
23+
24+
def to_presenter(view)
25+
Presenter.new self, view
1226
end
1327

1428
end
1529

30+
require 'alert/mailer'
1631
require 'alert/presenter'

lib/pinger/alert/record.rb

Lines changed: 0 additions & 38 deletions
This file was deleted.

lib/pinger/user.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def save_email_callback(email_callback)
88
email_callback.save
99
end
1010

11-
def build_alert_from_params(params = {})
11+
def build_alert(params = {})
1212
attrs = params[:alert] || {}
1313
attrs[:location_id] = params[:location_id] if params[:location_id].present?
14-
Alert.build attrs
14+
Alert.new attrs
1515
end
1616

1717
end

lib/test_support/factories.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
2828
end
2929

30-
factory :alert, :class => 'Alert::Record' do
30+
factory :alert do
3131
email_callback
3232
times { rand(10) + 1 }
3333
code_is_not { (rand(300) + 200).to_s }

spec/lib/pinger/alert_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22
require 'mail'
33

4-
describe Alert::Record do
4+
describe Alert do
55

66
describe "instance methods" do
77
subject do

spec/lib/pinger/user_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858
before do
5959
@alert = mock(Alert)
6060
@params = params
61-
Alert.stub(:build) { |args| @alert if args == attrs }
61+
Alert.stub(:new) { |args| @alert if args == attrs }
6262
end
6363

64-
it { subject.build_alert_from_params(@params).should == @alert }
64+
it { subject.build_alert(@params).should == @alert }
6565
end
6666
end
6767
end

0 commit comments

Comments
 (0)