diff --git a/CHANGELOG.md b/CHANGELOG.md index c6364806..344bd9bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - [#383](https://github.com/airblade/paper_trail/pull/383) - Make gem compatible with `ActiveRecord::Enum` (available in `ActiveRecord` 4.1+). - [#373](https://github.com/airblade/paper_trail/pull/373) - Fix default sort order for the `versions` association in `ActiveRecord` 4.1. - [#372](https://github.com/airblade/paper_trail/pull/372) - Use [Arel](https://github.com/rails/arel) for SQL construction. + - [#365](https://github.com/airblade/paper_trail/issues/365) - `VersionConcern#version_at` should return `nil` when receiving a timestamp + that occured after the object was destroyed. - [#347](https://github.com/airblade/paper_trail/pull/347) - Autoload `ActiveRecord` models in via a `Rails::Engine` when the gem is used with `Rails`. - Expand `PaperTrail::VERSION` into a module, mimicking the form used by Rails to give it some additional modularity & versatility. diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index a5a7671a..29f29ef5 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -182,7 +182,8 @@ module PaperTrail # Because a version stores how its object looked *before* the change, # we need to look for the first version created *after* the timestamp. v = send(self.class.versions_association_name).subsequent(timestamp, true).first - v ? v.reify(reify_options) : self + return v.reify(reify_options) if v + self unless self.destroyed? end # Returns the objects (not Versions) as they were between the given times. diff --git a/spec/models/widget_spec.rb b/spec/models/widget_spec.rb index f8ac3162..a7a7fe7c 100644 --- a/spec/models/widget_spec.rb +++ b/spec/models/widget_spec.rb @@ -105,6 +105,21 @@ describe Widget do end end + describe :version_at do + it { should respond_to(:version_at) } + + context "Timestamp argument is AFTER object has been destroyed" do + before do + widget.update_attribute(:name, 'foobar') + widget.destroy + end + + it "should return `nil`" do + widget.version_at(Time.now).should be_nil + end + end + end + describe :whodunnit do it { should respond_to(:whodunnit) }