From a23144fc042efa5e0d9297fa1fd7d51ffc041a1b Mon Sep 17 00:00:00 2001 From: Emmanuel Gomez Date: Sat, 5 Feb 2011 23:44:56 -0800 Subject: [PATCH] One more Version scope for PaperTrail::Model. --- lib/paper_trail/has_paper_trail.rb | 2 +- lib/paper_trail/version.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index 5b9b9fb4..73dfd639 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -71,7 +71,7 @@ module PaperTrail def version_at(timestamp) # Because a version stores how its object looked *before* the change, # we need to look for the first version created *after* the timestamp. - version = versions.first :conditions => ['created_at > ?', timestamp], :order => 'created_at ASC, id ASC' + version = versions.after(timestamp).first version ? version.reify : self end diff --git a/lib/paper_trail/version.rb b/lib/paper_trail/version.rb index af5fa684..dad9b389 100644 --- a/lib/paper_trail/version.rb +++ b/lib/paper_trail/version.rb @@ -16,6 +16,12 @@ class Version < ActiveRecord::Base :order => "id DESC", } } + named_scope :after, lambda { |timestamp| { + :conditions => ['created_at > ?', timestamp], + # TODO: is this :order necessary, considering its presence on the has_many :versions association? + :order => 'created_at ASC, id ASC' + } } + # Restore the item from this version. # # This will automatically restore all :has_one associations as they were "at the time",