Story#115762063 - all contacts are using the same versions logic

This commit is contained in:
Vladimir Krylov 2016-03-22 15:17:36 +02:00
parent 779a47c80f
commit 028aa31232
2 changed files with 24 additions and 7 deletions

View file

@ -55,4 +55,22 @@ module Versions
domains.each(&:touch_with_version)
end
end
module ClassMethods
def all_versions_for(ids, time)
ver_klass = paper_trail_version_class
from_history = ver_klass.where(item_id: ids).
order(:item_id).
preceding(time + 1, true).
select("distinct on (item_id) #{ver_klass.table_name}.*").
map do |ver|
o = new(ver.object)
ver.object_changes.to_h.each { |k, v| o[k]=v[-1] }
o
end
not_in_history = where(id: (ids - from_history.map(&:id)))
from_history + not_in_history
end
end
end