diff --git a/lib/audited/auditor.rb b/lib/audited/auditor.rb index c9c867ae..b726c981 100644 --- a/lib/audited/auditor.rb +++ b/lib/audited/auditor.rb @@ -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) @@ -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) @@ -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. @@ -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 @@ -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 diff --git a/spec/audited/auditor_spec.rb b/spec/audited/auditor_spec.rb index cff4044b..cefacd2c 100644 --- a/spec/audited/auditor_spec.rb +++ b/spec/audited/auditor_spec.rb @@ -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!