From 8e6efc6052099e177392749b804d0ac31ea7fd58 Mon Sep 17 00:00:00 2001 From: Jared Beck Date: Sun, 4 Dec 2016 17:02:06 -0500 Subject: [PATCH] Simplify test suite after dropping rails 3 support Also, the only version of rails 4 that we test against is 4.2, so we can drop the conditional around `ActiveRecord::Enum`. --- spec/models/post_with_status_spec.rb | 72 +++++++++++------------ test/dummy/app/models/post_with_status.rb | 7 +-- test/dummy/app/models/widget.rb | 12 +--- 3 files changed, 39 insertions(+), 52 deletions(-) diff --git a/spec/models/post_with_status_spec.rb b/spec/models/post_with_status_spec.rb index 982ffe1d..3255373f 100644 --- a/spec/models/post_with_status_spec.rb +++ b/spec/models/post_with_status_spec.rb @@ -1,49 +1,45 @@ require "rails_helper" -# This model tests ActiveRecord::Enum, which was added in AR 4.1 -# http://edgeguides.rubyonrails.org/4_1_release_notes.html#active-record-enums describe PostWithStatus, type: :model do - if defined?(ActiveRecord::Enum) - with_versioning do - let(:post) { described_class.create!(status: "draft") } + with_versioning do + let(:post) { described_class.create!(status: "draft") } - it "should stash the enum value properly in versions" do + it "should stash the enum value properly in versions" do + post.published! + post.archived! + expect(post.paper_trail.previous_version.published?).to be true + end + + it "can read enums in version records written by PT 4" do + post = described_class.create(status: "draft") + post.published! + version = post.versions.last + # Simulate behavior PT 4, which used to save the string version of + # enums to `object_changes` + version.update(object_changes: "---\nid:\n- \n- 1\nstatus:\n- draft\n- published\n") + assert_equal %w(draft published), version.changeset["status"] + end + + context "storing enum object_changes" do + subject { post.versions.last } + + it "should stash the enum value properly in versions object_changes" do post.published! post.archived! - expect(post.paper_trail.previous_version.published?).to be true + expect(subject.changeset["status"]).to eql %w(published archived) end + end - it "can read enums in version records written by PT 4" do - post = described_class.create(status: "draft") - post.published! - version = post.versions.last - # Simulate behavior PT 4, which used to save the string version of - # enums to `object_changes` - version.update(object_changes: "---\nid:\n- \n- 1\nstatus:\n- draft\n- published\n") - assert_equal %w(draft published), version.changeset["status"] - end - - context "storing enum object_changes" do - subject { post.versions.last } - - it "should stash the enum value properly in versions object_changes" do - post.published! - post.archived! - expect(subject.changeset["status"]).to eql %w(published archived) - end - end - - describe "#touch_with_version" do - it "preserves the enum value (and all other attributes)" do - post = described_class.create(status: :draft) - expect(post.versions.count).to eq(1) - expect(post.status).to eq("draft") - Timecop.travel 1.second.since # because MySQL lacks fractional seconds precision - post.paper_trail.touch_with_version - expect(post.versions.count).to eq(2) - expect(post.versions.last[:object]).to include("status: 0") - expect(post.paper_trail.previous_version.status).to eq("draft") - end + describe "#touch_with_version" do + it "preserves the enum value (and all other attributes)" do + post = described_class.create(status: :draft) + expect(post.versions.count).to eq(1) + expect(post.status).to eq("draft") + Timecop.travel 1.second.since # because MySQL lacks fractional seconds precision + post.paper_trail.touch_with_version + expect(post.versions.count).to eq(2) + expect(post.versions.last[:object]).to include("status: 0") + expect(post.paper_trail.previous_version.status).to eq("draft") end end end diff --git a/test/dummy/app/models/post_with_status.rb b/test/dummy/app/models/post_with_status.rb index 2149f7c3..fc6d9a48 100644 --- a/test/dummy/app/models/post_with_status.rb +++ b/test/dummy/app/models/post_with_status.rb @@ -1,8 +1,7 @@ +# This model tests ActiveRecord::Enum, which was added in AR 4.1 +# http://edgeguides.rubyonrails.org/4_1_release_notes.html#active-record-enums class PostWithStatus < ActiveRecord::Base has_paper_trail - # ActiveRecord::Enum is only supported in AR4.1+ - if defined?(ActiveRecord::Enum) - enum status: { draft: 0, published: 1, archived: 2 } - end + enum status: { draft: 0, published: 1, archived: 2 } end diff --git a/test/dummy/app/models/widget.rb b/test/dummy/app/models/widget.rb index 064a5f63..774982ab 100644 --- a/test/dummy/app/models/widget.rb +++ b/test/dummy/app/models/widget.rb @@ -1,16 +1,8 @@ class Widget < ActiveRecord::Base + EXCLUDED_NAME = "Biglet".freeze has_paper_trail has_one :wotsit + has_many :fluxors, -> { order(:name) } has_many :whatchamajiggers, as: :owner - - EXCLUDED_NAME = "Biglet".freeze - validates :name, exclusion: { in: [EXCLUDED_NAME] } - - # `has_many` syntax for specifying order uses a lambda in Rails 4 - if ::ActiveRecord::VERSION::MAJOR >= 4 - has_many :fluxors, -> { order(:name) } - else - has_many :fluxors, order: :name - end end