Skip to content

Commit

Permalink
Update iterators in auditor to check first whether the audits associa…
Browse files Browse the repository at this point in the history
…tion is loaded
  • Loading branch information
wkirby committed Jun 13, 2024
1 parent 706cbff commit 0e1b3b4
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ def with_auditing(&block)
# end
#
def revisions(from_version = 1)
return [] unless audits.from_version(from_version).exists?
return [] unless has_version?(from_version)

all_audits = audits.select([:audited_changes, :version, :action]).to_a
all_audits = audits.pluck(:audited_changes, :version, :action).to_a
targeted_audits = all_audits.select { |audit| audit.version >= from_version }

previous_attributes = reconstruct_attributes(all_audits - targeted_audits)
Expand All @@ -156,6 +156,10 @@ def revisions(from_version = 1)
end
end

def has_version?(version)
audits.loaded? ? audits.any? { |audit| audit.version >= from_version } : audits.from_version(from_version).exists?
end

# Get a specific revision specified by the version number, or +:previous+
# Returns nil for versions greater than revisions count
def revision(version)
Expand All @@ -166,8 +170,8 @@ def revision(version)

# Find the oldest revision recorded prior to the date/time provided.
def revision_at(date_or_time)
audits = self.audits.up_until(date_or_time)
revision_with Audited.audit_class.reconstruct_attributes(audits) unless audits.empty?
targeted_audits = audits.loaded? ? audits.filter { |audit| audit.created_at <= date_or_time } : audits.up_until(date_or_time)
revision_with Audited.audit_class.reconstruct_attributes(targeted_audits) unless targeted_audits.empty?
end

# List of attributes that are audited.
Expand Down Expand Up @@ -323,11 +327,12 @@ def audits_to(version = nil)
version = if audit_version
audit_version - 1
else
previous = audits.descending.offset(1).first
previous = audits.loaded? ? audits.sort_by { |audit| -audit.version }.second : audits.descending.offset(1).first
previous ? previous.version : 1
end
end
audits.to_version(version)

audits.loaded? ? audits.filter { |audit| audit.version <= version } : audits.to_version(version)
end

def audit_create
Expand Down Expand Up @@ -386,7 +391,7 @@ def comment_required_state?
def combine_audits_if_needed
max_audits = audited_options[:max_audits]
if max_audits && (extra_count = audits.count - max_audits) > 0
audits_to_combine = audits.limit(extra_count + 1)
audits_to_combine = audits.loaded? ? audits.take(extra_count + 1) : audits.limit(extra_count + 1)
combine_audits(audits_to_combine)
end
end
Expand Down

0 comments on commit 0e1b3b4

Please sign in to comment.