Skip to content

Commit

Permalink
Optimize reconstructing attributes in #revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jan 15, 2018
1 parent 70ad55c commit 567614f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 567614f

Please sign in to comment.