/
time_entries.rb
47 lines (39 loc) · 1.48 KB
/
time_entries.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class MiteSynchronizer::TimeEntries < MiteSynchronizer::Base
def synchronize(is_initial_sync)
if (!is_initial_sync)
destroy_old_local_records if local_record_ids_to_destroy.any?
changed_remote_records.each do |remote_record|
update_local_record(remote_record)
end
end
end
protected
def destroy_old_local_records
TimeEntry.connected_to_mite.destroy_all(:mite_time_entry_id => local_record_ids_to_destroy)
end
def update_local_record(remote_record)
local_record = find_local_record(remote_record)
local_record.update_attributes({
:mite_project_id => remote_record.project_id,
:mite_service_id => remote_record.service_id,
:mite_time_entry_updated_on => remote_record.updated_at.localtime,
:hours => (remote_record.minutes.to_f / 60).to_f,
:comments => remote_record.note
})
end
def find_local_record(remote_record)
local_records.detect do |local_record|
local_record.mite_time_entry_id == remote_record.id
end
end
def local_record_ids_to_destroy
@local_record_ids_to_destroy ||= local_records.map(&:mite_time_entry_id) - remote_records.map(&:id)
end
def remote_records
return [] unless local_records.any?
@remote_records ||= Mite::TimeEntry.find(:all, :params => {:ids => local_records.map(&:mite_time_entry_id).join(",")})
end
def local_records
@local_records ||= TimeEntry.connected_to_mite.find(:all, :conditions => {:user_id => User.current.id})
end
end