From d9cf951daebb8f2c5c80b3844a3bf6434be46538 Mon Sep 17 00:00:00 2001 From: Ben Atkins Date: Tue, 17 Jun 2014 11:40:17 -0400 Subject: [PATCH] Only run tests for ActiveRecord::Enum when it is defined --- CHANGELOG.md | 3 ++- lib/paper_trail/has_paper_trail.rb | 6 +++--- spec/models/post_with_status_spec.rb | 17 +++++++++++++++++ test/dummy/app/models/post_with_status.rb | 6 +++++- test/unit/model_test.rb | 11 ----------- 5 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 spec/models/post_with_status_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index e8968b0f..b6a95411 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index 5d418836..abcb779b 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -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 diff --git a/spec/models/post_with_status_spec.rb b/spec/models/post_with_status_spec.rb new file mode 100644 index 00000000..b47e77ab --- /dev/null +++ b/spec/models/post_with_status_spec.rb @@ -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 diff --git a/test/dummy/app/models/post_with_status.rb b/test/dummy/app/models/post_with_status.rb index e24a18cc..2149f7c3 100644 --- a/test/dummy/app/models/post_with_status.rb +++ b/test/dummy/app/models/post_with_status.rb @@ -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 diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb index 4634498e..25dd6a74 100644 --- a/test/unit/model_test.rb +++ b/test/unit/model_test.rb @@ -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