Skip to content
Browse files

Use Time.zone instead of DateTime where possible. Simplify usage of r…

…and in specs.
  • Loading branch information...
1 parent f877cec commit 25ef156941b918ccd2e002b4f75e8d4fff65c917 @porcupie porcupie committed Nov 28, 2012
View
2 app/controllers/dashboard_controller.rb
@@ -11,6 +11,6 @@ def index
@call_lists.compact!
@call_lists.sort!{|x,y| x.name <=> y.name}
- @oncall_assignments = current_user.oncall_assignments.where("ends_at > ?", DateTime.now).order('ends_at')
+ @oncall_assignments = current_user.oncall_assignments.where("ends_at > ?", Time.zone.now).order('ends_at')
end
end
View
6 app/models/call_list.rb
@@ -78,7 +78,7 @@ def in_business_hours?(now = Time.now)
end
def current_oncalls
- [] | self.oncall_assignments.where("starts_at < ? AND ends_at > ?", DateTime.now, DateTime.now) | self.oncall_assignments.where("starts_at is NULL AND ends_at is NULL")
+ [] | self.oncall_assignments.where("starts_at < ? AND ends_at > ?", Time.zone.now, Time.zone.now) | self.oncall_assignments.where("starts_at is NULL AND ends_at is NULL")
end
def contact_types
@@ -145,7 +145,7 @@ def gen_oncall_assignments(start_date = nil, w = AppConfig.oncall_assignments_ge
end
# Given a date, find the last person oncall from that date
- def last_oncall(date = DateTime.now)
+ def last_oncall(date = Time.zone.now)
# Explicitly sort it ourselves rather than using activerecord order method since there can be some
# default scope laying around that messes up the sorting
last_oncall_assignment = oncall_assignments.where('starts_at <= ?', date).sort{|x,y| x.ends_at <=> y.ends_at}.last
@@ -157,7 +157,7 @@ def last_oncall(date = DateTime.now)
end
# Given a date, find all users who are oncall
- def whos_oncall(date = DateTime.now)
+ def whos_oncall(date = Time.zone.now)
oncall_assignments.where("starts_at <= ? AND ends_at >= ?", date, date) | oncall_assignments.where("starts_at is NULL AND ends_at is NULL")
end
View
2 app/models/oncall_assignment.rb
@@ -1,5 +1,5 @@
class OncallAssignment < CallEscalation
- set_table_name 'oncall_assignments'
+ self.table_name = 'oncall_assignments'
validate :check_oncall_datetime
after_save :add_call_list_membership
View
2 lib/ringring/calendar/oncall_updater.rb
@@ -14,7 +14,7 @@ def self.update_oncalls(call_list)
return FETCH_CALENDAR_ERR
end
- new_oncalls = ConfluenceIcal::find_oncall(call_list_calendar.url, DateTime.now)
+ new_oncalls = ConfluenceIcal::find_oncall(call_list_calendar.url, Time.zone.now)
if new_oncalls.nil? or new_oncalls.empty?
return FETCH_CALENDAR_ERR
View
50 spec/models/call_list_spec.rb
@@ -3,7 +3,7 @@
describe CallList do
describe "default factory creation" do
it "can be created" do
- call_list = CallList.make(:name => 'testlist1', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make(:name => 'testlist1', :twilio_list_id => (rand(2132)).to_i)
#call_list.valid?
#puts call_list.errors.full_messages.inspect
call_list.should be_valid
@@ -22,7 +22,7 @@
end
it "cannot have same name as existing call list" do
- CallList.make!(:name => "calllist1", :twilio_list_id => (rand * 2132).to_i)
+ CallList.make!(:name => "calllist1", :twilio_list_id => (rand(2132)).to_i)
call_list = CallList.make(:name => "calllist1")
call_list.should_not be_valid
call_list.should have(1).errors_on(:name)
@@ -39,48 +39,48 @@
@user1 = User.make!(:email => 'auser1@example.com', :password => 'password')
@user2 = User.make!(:email => 'auser2@example.com', :password => 'password')
@user3 = User.make!(:email => 'auser3@example.com', :password => 'password')
- @call_list = CallList.make!(:name => 'testlist1', :twilio_list_id => (rand * 2132).to_i)
+ @call_list = CallList.make!(:name => 'testlist1', :twilio_list_id => (rand(2132)).to_i)
end
it "returns nil if there is no oncall assignments in the past" do
# assignment that is in the future
oncall_assignment = OncallAssignment.make!(:user_id => @user3.id,
:call_list_id => @call_list.id,
- :starts_at => DateTime.now + 1,
- :ends_at => DateTime.now + 3
+ :starts_at => Time.zone.now + 1,
+ :ends_at => Time.zone.now + 3
)
- @call_list.last_oncall(DateTime.now).should equal(nil)
+ @call_list.last_oncall(Time.zone.now).should equal(nil)
end
it "returns last oncall correctly" do
oncall_assignment = OncallAssignment.make!(:user_id => @user2.id,
:call_list_id => @call_list.id,
- :starts_at => DateTime.now - 7,
- :ends_at => DateTime.now - 2
+ :starts_at => Time.zone.now - 7,
+ :ends_at => Time.zone.now - 2
)
- @call_list.last_oncall(DateTime.now).should eq(@user2)
+ @call_list.last_oncall(Time.zone.now).should eq(@user2)
oncall_assignment = OncallAssignment.make!(:user_id => @user3.id,
:call_list_id => @call_list.id,
- :starts_at => DateTime.now - 7,
- :ends_at => DateTime.now + 3)
+ :starts_at => Time.zone.now - 7,
+ :ends_at => Time.zone.now + 3)
oncall_assignment = OncallAssignment.make!(:user_id => @user1.id,
:call_list_id => @call_list.id,
- :starts_at => DateTime.now - 7,
- :ends_at => DateTime.now + 2
+ :starts_at => Time.zone.now - 7,
+ :ends_at => Time.zone.now + 2
)
- @call_list.last_oncall(DateTime.now).should eq(@user3)
+ @call_list.last_oncall(Time.zone.now).should eq(@user3)
end
it "returns last oncall correctly when passing in datetime in future" do
# assignment that is in the future
oncall_assignment = OncallAssignment.make!(:user_id => @user3.id,
:call_list_id => @call_list.id,
- :starts_at => DateTime.now + 1,
- :ends_at => DateTime.now + 3
+ :starts_at => Time.zone.now + 1,
+ :ends_at => Time.zone.now + 3
)
- @call_list.last_oncall(DateTime.now + 2).should eq(@user3)
+ @call_list.last_oncall(Time.zone.now + 2).should eq(@user3)
end
end
@@ -90,7 +90,7 @@
@user2 = User.make!(:email => 'auser2@example.com', :password => 'password')
@user3 = User.make!(:email => 'auser3@example.com', :password => 'password')
@users = [@user1, @user2, @user3]
- @call_list = CallList.make!(:name => 'testlist1', :twilio_list_id => (rand * 2132).to_i)
+ @call_list = CallList.make!(:name => 'testlist1', :twilio_list_id => (rand(2132)).to_i)
@users.each do |user|
CallListMembership.create(:call_list_id => @call_list.id,
@@ -107,7 +107,7 @@
@call_list.oncall_assignments_gen = oncall_assignments_gen
@call_list.save
- lambda {@call_list.gen_oncall_assignments}.should_not raise_error
+ lambda { @call_list.gen_oncall_assignments }.should_not raise_error
@call_list.oncall_assignments.size.should be == AppConfig.oncall_assignments_gen['from_now'] + 1
@call_list.oncall_assignments_gen.last_gen.should be > AppConfig.oncall_assignments_gen['from_now'].weeks.from_now
@call_list.oncall_assignments.last.starts_at.should_not be > AppConfig.oncall_assignments_gen['from_now'].weeks.from_now
@@ -125,7 +125,7 @@
oncall_assignments_gen = OncallAssignmentsGen.make!
@call_list.oncall_assignments_gen = oncall_assignments_gen
@call_list.save
- lambda {@call_list.gen_oncall_assignments(DateTime.now, 2)}.should_not raise_error
+ lambda {@call_list.gen_oncall_assignments(Time.zone.now, 2)}.should_not raise_error
@call_list.oncall_assignments.size.should be == 3
@call_list.oncall_assignments.last.user.should eq(@user3)
@@ -148,7 +148,7 @@
end
it "has correct oncall_candidates default ordering based on position" do
- call_list = CallList.make!(:name => 'testlist2', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:name => 'testlist2', :twilio_list_id => (rand(2132)).to_i)
@users.each_with_index do |user, index|
CallListMembership.create(:call_list_id => call_list.id,
:user_id => user.id,
@@ -161,19 +161,19 @@
describe "verify business hours" do
it "is not in business hour if no business hours are defined" do
- call_list = CallList.make!(:name => 'testlist1', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:name => 'testlist1', :twilio_list_id => (rand(2132)).to_i)
call_list.in_business_hours?.should be_false
end
it "correctly determines if in business hour for UTC time" do
- call_list = CallList.make!(:business_time_zone => 'UTC', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:business_time_zone => 'UTC', :twilio_list_id => (rand(2132)).to_i)
(0..6).each do |wday|
call_list.business_hours.create(:wday => wday, :start_time => Time.parse("8:00 AM +0000"),
:end_time => Time.parse("8:00 PM +0000"))
end
call_list.in_business_hours?(Time.parse("9:00 AM +0000")).should be_true
end
it "correctly determines if in business hour for non UTC time" do
- call_list = CallList.make!(:business_time_zone => 'Arizona', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:business_time_zone => 'Arizona', :twilio_list_id => (rand(2132)).to_i)
(0..6).each do |wday|
call_list.business_hours.create(:wday => wday, :start_time => Time.parse("8:00 AM -0700"),
:end_time => Time.parse("8:00 PM -0700"))
@@ -185,7 +185,7 @@
describe "oncall assignment" do
it "automatically add oncall user to membership list" do
user1 = User.make!(:email => 'auser1@example.com', :password => 'password')
- call_list = CallList.make!(:name => 'testlist1', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:name => 'testlist1', :twilio_list_id => (rand(2132)).to_i)
oncall_assignment = OncallAssignment.make!(:user_id => user1.id,
:call_list_id => call_list.id)
call_list.members.should include(user1)
View
4 spec/models/user_spec.rb
@@ -4,15 +4,15 @@
describe User do
describe 'ability' do
it "can manage their own call list" do
- call_list = CallList.make!(:name => 'my call list', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:name => 'my call list', :twilio_list_id => (rand(2132)).to_i)
user = User.make!
call_list.owners << user
ability = Ability.new(user)
ability.should be_able_to(:manage, call_list)
end
it "cannot manage someone else's call list" do
- call_list = CallList.make!(:name => 'not my call list', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:name => 'not my call list', :twilio_list_id => (rand(2132)).to_i)
user = User.make!
ability = Ability.new(user)
ability.should_not be_able_to(:manage, call_list)
View
10 spec/requests/call_lists_spec.rb
@@ -11,8 +11,8 @@
describe "GET /call_lists" do
it "displays call lists" do
- CallList.make!(:name => "calllist1", :twilio_list_id => (rand * 2132).to_i)
- CallList.make!(:name => "calllist2", :twilio_list_id => (rand * 2132).to_i)
+ CallList.make!(:name => "calllist1", :twilio_list_id => (rand(2132)).to_i)
+ CallList.make!(:name => "calllist2", :twilio_list_id => (rand(2132)).to_i)
visit call_lists_path
page.should have_content("calllist1")
page.should have_content("calllist2")
@@ -23,7 +23,7 @@
it "creates call list" do
visit new_call_list_path
fill_in "call_list[name]", :with => 'calllist3'
- fill_in "call_list[twilio_list_id]", :with => (rand * 2132).to_i
+ fill_in "call_list[twilio_list_id]", :with => (rand(2132)).to_i
click_button "Create Call list"
page.should have_content("calllist3")
end
@@ -33,7 +33,7 @@
@admin_user.save
visit new_call_list_path
fill_in "call_list[name]", :with => 'calllist3'
- fill_in "call_list[twilio_list_id]", :with => (rand * 2132).to_i
+ fill_in "call_list[twilio_list_id]", :with => (rand(2132)).to_i
fill_in "call_list[oncall_assignments_gen_attributes][cycle_time]", :with => "1:23 AM"
click_button "Create Call list"
call_list_id = File.basename(current_path)
@@ -51,7 +51,7 @@
@admin_user.save
visit new_call_list_path
fill_in "call_list[name]", :with => 'calllist3'
- fill_in "call_list[twilio_list_id]", :with => (rand * 2132).to_i
+ fill_in "call_list[twilio_list_id]", :with => (rand(2132)).to_i
fill_in "call_list[business_hours_attributes][0][start_time]", :with => "9:15 AM"
fill_in "call_list[business_hours_attributes][0][end_time]", :with => "7:32 PM"
select @admin_user.time_zone, :from => "call_list[business_time_zone]"
View
12 spec/requests/dashboard_spec.rb
@@ -25,10 +25,10 @@
it "displays call lists that the user belongs to" do
user = User.create(:email => 'admin@example.com', :password => 'mypassword')
- call_list = CallList.make!(:name => 'my special call list', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:name => 'my special call list', :twilio_list_id => (rand(2132)).to_i)
call_list.owners << user
- CallList.make!(:name => 'someone else call list', :twilio_list_id => (rand * 2132).to_i)
+ CallList.make!(:name => 'someone else call list', :twilio_list_id => (rand(2132)).to_i)
visit "/users/sign_in"
fill_in "user[email]", :with => 'admin@example.com'
@@ -41,7 +41,7 @@
it "displays the user's current oncall assignments if there are any" do
user = User.make!(:email => 'admin@example.com', :password => 'password')
- call_list = CallList.make!(:name => 'my special call list', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:name => 'my special call list', :twilio_list_id => (rand(2132)).to_i)
call_list.owners << user
oncall_assignment = OncallAssignment.make!(:user_id => user.id,
@@ -56,13 +56,13 @@
it "should not display old oncall assignments" do
user = User.make!(:email => 'admin@example.com', :password => 'password')
- call_list = CallList.make!(:name => 'my special call list', :twilio_list_id => (rand * 2132).to_i)
+ call_list = CallList.make!(:name => 'my special call list', :twilio_list_id => (rand(2132)).to_i)
call_list.owners << user
oncall_assignment = OncallAssignment.make!(:user_id => user.id,
:call_list_id => call_list.id,
- :starts_at => DateTime.now - 10,
- :ends_at => DateTime.now - 5
+ :starts_at => Time.zone.now - 10,
+ :ends_at => Time.zone.now - 5
)
visit "/users/sign_in"
View
6 spec/support/blueprints.rb
@@ -26,16 +26,16 @@
end
OncallAssignment.blueprint do
- starts_at { DateTime.now - 7 }
- ends_at { DateTime.now + 7 }
+ starts_at { Time.zone.now - 7 }
+ ends_at { Time.zone.now + 7 }
timezone_offset { 0 }
assigned_by { 'test' }
end
OncallAssignmentsGen.blueprint do
enable { true }
cycle_day { 1 }
- cycle_time { DateTime.parse("8:00") }
+ cycle_time { Time.zone.parse("8:00") }
timezone_offset { 0 }
end

0 comments on commit 25ef156

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