Only run tests for ActiveRecord::Enum when it is defined

This commit is contained in:
Ben Atkins 2014-06-17 11:40:17 -04:00
parent 348b71e082
commit d9cf951dae
5 changed files with 27 additions and 16 deletions

View File

@ -1,6 +1,7 @@
## 3.0.3 (Unreleased)
- [#373](https://github.com/airblade/paper_trail/pull/373) - Fix default sort order for the `versions` association in Rails `4.1`
- [#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 Rails `4.1`.
- [#372](https://github.com/airblade/paper_trail/pull/372) - Use [Arel](https://github.com/rails/arel) for SQL construction.
- [#347](https://github.com/airblade/paper_trail/pull/347) - Autoload `ActiveRecord` models in via a `Rails::Engine` when
the gem is used with `Rails`.

View File

@ -345,10 +345,10 @@ module PaperTrail
enums = previous.respond_to?(:defined_enums) ? previous.defined_enums : {}
previous.tap do |prev|
prev.id = id # `dup` clears the `id` so we add that back
changed_attributes.select { |k,v| self.class.column_names.include?(k) }.each { |attr, before|
before = enums[attr][before] unless enums[attr].nil?
changed_attributes.select { |k,v| self.class.column_names.include?(k) }.each do |attr, before|
before = enums[attr][before] if enums[attr]
prev[attr] = before
}
end
end
end

View File

@ -0,0 +1,17 @@
require 'spec_helper'
# This model is in the test suite soley for the purpose of testing ActiveRecord::Enum,
# which is available in ActiveRecord4+ only
describe PostWithStatus do
if defined?(ActiveRecord::Enum)
with_versioning do
let(:post) { PostWithStatus.create!(:status => 'draft') }
it "should stash the enum value properly in versions" do
post.published!
post.archived!
post.previous_version.published?.should == true
end
end
end
end

View File

@ -1,4 +1,8 @@
class PostWithStatus < ActiveRecord::Base
has_paper_trail
enum status: { draft: 0, published: 1, archived: 2 }
# ActiveRecord::Enum is only supported in AR4.1+
if defined?(ActiveRecord::Enum)
enum status: { draft: 0, published: 1, archived: 2 }
end
end

View File

@ -1376,17 +1376,6 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
end
end
context 'A record with enum fields' do
should "keep the integer in versions" do
post = PostWithStatus.create!(status: "draft")
assert post.draft?
post.published!
post.archived!
assert post.previous_version.published?
end
end
private
# Updates `model`'s last version so it looks like the version was