Skip to content

Commit

Permalink
Leverage ARel, minor fixes for Rails 3.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethkalmer committed Feb 2, 2011
1 parent 2c3ffa3 commit bb38256
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
7 changes: 5 additions & 2 deletions lib/acts_as_audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Audit < ActiveRecord::Base
cattr_accessor :audited_class_names
self.audited_class_names = Set.new

# Order by ver
default_scope order(:version)
scope :descending, reorder("version DESC")

class << self

# Returns the list of classes that are being audited
Expand Down Expand Up @@ -94,8 +98,7 @@ def revision

# Return all audits older than the current one.
def ancestors
self.class.find(:all, :order => 'version',
:conditions => ['auditable_id = ? and auditable_type = ? and version <= ?',
self.class.where(['auditable_id = ? and auditable_type = ? and version <= ?',
auditable_id, auditable_type, version])
end

Expand Down
16 changes: 9 additions & 7 deletions lib/acts_as_audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def acts_as_audited(options = {})
attr_accessible :audit_comment
end

has_many :audits, :as => :auditable, :order => "#{Audit.quoted_table_name}.version"
has_many :audits, :as => :auditable
attr_protected :audit_ids if options[:protect]
Audit.audited_class_names << self.to_s

Expand Down Expand Up @@ -116,7 +116,7 @@ def without_auditing(&block)
# end
#
def revisions(from_version = 1)
audits = self.audits.find(:all, :conditions => ['version >= ?', from_version])
audits = self.audits.where(['version >= ?', from_version])
return [] if audits.empty?
revision = self.audits.find_by_version(from_version).revision
Audit.reconstruct_attributes(audits) {|attrs| revision.revision_with(attrs) }
Expand All @@ -129,7 +129,7 @@ def revision(version)

# Find the oldest revision recorded prior to the date/time provided.
def revision_at(date_or_time)
audits = self.audits.find(:all, :conditions => ["created_at <= ?", date_or_time])
audits = self.audits.where("created_at <= ?", date_or_time)
revision_with Audit.reconstruct_attributes(audits) unless audits.empty?
end

Expand All @@ -143,7 +143,10 @@ def audited_attributes
def revision_with(attributes)
self.dup.tap do |revision|
revision.send :instance_variable_set, '@attributes', self.attributes_before_type_cast
revision.send :instance_variable_set, '@new_record', false unless self.destroyed?
revision.send :instance_variable_set, '@persisted', !self.destroyed?
revision.send :instance_variable_set, '@readonly', false
revision.send :instance_variable_set, '@destroyed', false
revision.send :instance_variable_set, '@marked_for_destruction', false
Audit.assign_revision_attributes(revision, attributes)

# Remove any association proxies so that they will be recreated
Expand Down Expand Up @@ -174,12 +177,11 @@ def audits_to(version = nil)
version = if self.version
self.version - 1
else
previous = audits.find(:first, :offset => 1,
:order => "#{Audit.quoted_table_name}.version DESC")
previous = audits.descending.offset(1).first
previous ? previous.version : 1
end
end
audits.find(:all, :conditions => ['version <= ?', version])
audits.where(['version <= ?', version])
end

def audit_create
Expand Down
2 changes: 1 addition & 1 deletion spec/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
user.audits(true).first.version.should be(1)
user.audits(true).last.version.should be(2)
user.destroy
user.audits(true).last.version.should be(3)
Audit.where(:auditable_type => 'User', :auditable_id => user.id).last.version.should be(3)
end

describe "reconstruct_attributes" do
Expand Down

0 comments on commit bb38256

Please sign in to comment.