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 authored and tbrisker committed Jan 31, 2018
1 parent 5667847 commit 114a089
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Added

Changed

- None
- Reduced db calls in #revisions method
[#402](https://github.com/collectiveidea/audited/pull/402)
[#403](https://github.com/collectiveidea/audited/pull/403)

Fixed

Expand Down
12 changes: 7 additions & 5 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ 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))
previous_attributes.merge!(audit.new_attributes)
revision_with(previous_attributes.merge!(version: audit.version))
end
end

Expand Down

0 comments on commit 114a089

Please sign in to comment.