Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if audits is loaded before executing database queries #715

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.loaded? ? audits.to_a : audits.select([: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 >= version } : audits.from_version(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.select { |audit| audit.created_at <= date_or_time }.sort_by(&:version) : 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.select { |audit| audit.version <= version }.sort_by(&:version) : audits.to_version(version)
end

def audit_create
Expand Down Expand Up @@ -387,7 +392,7 @@ def combine_audits_if_needed
max_audits = evaluate_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.sort_by(&:version).take(extra_count + 1) : audits.limit(extra_count + 1)
combine_audits(audits_to_combine)
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,20 @@ def stub_global_max_audits(max_audits)
end
end

describe "has_version" do
let(:user) { create_versions(2) }

it "should return true when a version exists" do
expect(user.has_version?(1)).to eq(true)
expect(user.has_version?(2)).to eq(true)
end

it "should return false when a version does not exist" do
expect(user.has_version?(8)).to eq(false)
expect(user.has_version?(1337)).to eq(false)
end
end

describe "own_and_associated_audits" do
it "should return audits for self and associated audits" do
owner = Models::ActiveRecord::Owner.create!
Expand Down