From 567614f92cc6fdf51314fe2fd2a3d51860ec4a04 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Mon, 15 Jan 2018 23:19:20 +0200 Subject: [PATCH] Optimize reconstructing attributes in #revisions --- lib/audited/auditor.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/audited/auditor.rb b/lib/audited/auditor.rb index db4732c9..400d77dd 100644 --- a/lib/audited/auditor.rb +++ b/lib/audited/auditor.rb @@ -101,14 +101,17 @@ def without_auditing(&block) # end # def revisions(from_version = 1) - return [] if audits.from_version(from_version).empty? + return [] unless audits.from_version(from_version).exists? - loaded_audits = audits.select([:audited_changes, :version]).to_a - targeted_audits = loaded_audits.select { |audit| audit.version >= from_version } + all_audits = audits.select([:audited_changes, :version]).to_a + targeted_audits = all_audits.select { |audit| audit.version >= from_version } + + previous_attributes = reconstruct_attributes(all_audits - targeted_audits) targeted_audits.map do |audit| - ancestors = loaded_audits.select { |a| a.version <= audit.version } - revision_with(reconstruct_attributes(ancestors).merge(version: audit.version)) + revision_attributes = previous_attributes.merge(audit.new_attributes) + previous_attributes = revision_attributes + revision_with(revision_attributes.merge!(version: audit.version)) end end