Created
January 23, 2013 17:58
-
-
Save litch/4611053 to your computer and use it in GitHub Desktop.
Refactor me challenge!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EquipmentItem | |
attr_accessor :task_items, equipment_hauls | |
def current_location | |
last_task = task_items.sort_by{|a| a.end_time }.last | |
last_haul = equipment_hauls.last | |
if last_task && last_haul | |
if last_task.end_time > last_haul.performed_at | |
last_task.timesheet.job | |
else | |
last_haul.to_job | |
end | |
elsif last_task | |
last_task.timesheet.job | |
elsif last_haul | |
last_haul.to_job | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe EquipmentItem do | |
describe 'current_location' do | |
let(:item) { build :equipment_item } | |
it "can locate itself based on last task" do | |
job = mock_model(Job) | |
timesheet = mock_model(Timesheet, job: job) | |
task_item = build :task_item, timesheet: timesheet | |
item.task_items << task_item | |
item.current_location.should == job | |
end | |
it "locates itself based on an equipment haul" do | |
from_job = mock_model(Job) | |
to_job = mock_model(Job) | |
item.equipment_hauls << build(:equipment_haul, equipment_item: item, from_job: from_job, to_job: to_job) | |
item.current_location.should == to_job | |
end | |
it "decides whether the equipment_haul or last_task is the correct one to report" do | |
task_job = mock_model(Job) | |
timesheet = mock_model(Timesheet, job: task_job) | |
task_item = build :task_item, timesheet: timesheet | |
item.task_items << task_item | |
from_job = mock_model(Job) | |
to_job = mock_model(Job) | |
item.equipment_hauls << build(:equipment_haul, equipment_item: item, from_job: from_job, to_job: to_job, performed_at: Time.zone.now-5.minutes) | |
item.current_location.should == to_job | |
task_item.end_time = Time.zone.now | |
item.current_location.should == task_job | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment