Skip to content

Commit

Permalink
remove after_save callback in site, add a reset method do that, and i…
Browse files Browse the repository at this point in the history
…nvoke it in controller
  • Loading branch information
siuying committed Feb 10, 2012
1 parent 0bee2ae commit 6022ccd
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
6 changes: 0 additions & 6 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,4 @@ Vagrant::Config.run do |config|

# config.vm.boot_mode = :gui
config.ssh.max_tries = 30

# customzie VM
config.vm.customize [
"modifyvm", :id,
"--memory", "384"
]
end
1 change: 1 addition & 0 deletions app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def create
# PUT /sites/1.json
def update
@site = Site.find(params[:id])
@site.reset if params[:site][:url] != @site.url

respond_to do |format|
if @site.update_attributes(params[:site])
Expand Down
16 changes: 6 additions & 10 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@ class Site

validates_presence_of :url
validates_format_of :url, :with => %r{^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$}

before_save :update_status


def ok?
status == STATUS_OK
end

def failed?
status == STATUS_FAILED
end

protected
def update_status
if url_changed?
self.status = STATUS_UNKNOWN
self.status_changed_at = Time.now
end

# Reset status and status change date
def reset
self.status = STATUS_UNKNOWN
self.status_changed_at = Time.now
end
end
24 changes: 11 additions & 13 deletions spec/jobs/site_fetcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
require 'spec_helper'

describe SiteFetcher do
context "Fetcher should send email" do
context "Fetcher Email Notifications" do
before(:each) do
@site = Site.create!(:url => "http://google.com",
:status => Site::STATUS_OK,
:status_changed_at => 3.minutes.ago)
@response = double(:response)
SitesMailer = double(:sites_mailer).as_null_object
SiteFetcher.stub(:get_url).and_return(@response)
end

it "should not send email when status is ok and no change" do
it "should not send when status is ok and no change" do
@site = FactoryGirl.create(:ok_site)
@response.stub(:success?).and_return(true)
SitesMailer.should_not_receive(:notify_error).should_not_receive(:notify_resolved)

SiteFetcher.perform(@site.id)
end

it "should send email when status is change from ok to fail" do
@site.update_attribute :status, Site::STATUS_OK
it "should send when status is change from ok to fail" do
@site = FactoryGirl.create(:ok_site)
@response.stub(:success? => false, :timed_out? => false, :code => 404, :status_message => "Not found")
SitesMailer.should_receive(:notify_error).and_return(stub(:mailer, :deliver => true))
SitesMailer.should_not_receive(:notify_resolved)

SiteFetcher.perform(@site.id)
end

it "should send email when status is change from fail to ok" do
@site.update_attribute :status, Site::STATUS_FAILED
it "should send when status is change from fail to ok" do
@site = FactoryGirl.create(:failed_site)
@response.stub(:success?).and_return(true)
SitesMailer.should_receive(:notify_resolved).and_return(stub(:mailer, :deliver => true))
SitesMailer.should_not_receive(:notify_error)

SiteFetcher.perform(@site.id)
end

it "should not send email when status is change from unknown to ok" do
@site.update_attribute :status, Site::STATUS_UNKNOWN
it "should not email when status is change from unknown to ok" do
@site = FactoryGirl.create(:site)
@response.stub(:success?).and_return(true)
SitesMailer.should_not_receive(:notify_resolved).and_return(stub(:mailer, :deliver => true))
SitesMailer.should_not_receive(:notify_error)

SiteFetcher.perform(@site.id)
end

it "should send email when status is change from unknown to fail" do
@site.update_attribute :status, Site::STATUS_UNKNOWN
it "should send when status is change from unknown to fail" do
@site = FactoryGirl.create(:site)
@response.stub(:success? => false, :timed_out? => false, :code => 404, :status_message => "Not found")
SitesMailer.should_receive(:notify_error).and_return(stub(:mailer, :deliver => true))
SitesMailer.should_not_receive(:notify_resolved)
Expand Down
9 changes: 9 additions & 0 deletions spec/models/site_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'

describe Site do
context "Site has status" do
it "should not ok nor fail when created" do
site = Site.create
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Expand Down

0 comments on commit 6022ccd

Please sign in to comment.