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`.
This commit is contained in:
Jared Beck 2016-12-04 17:02:06 -05:00
parent 480d6c18b5
commit 8e6efc6052
3 changed files with 39 additions and 52 deletions

View File

@ -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

View File

@ -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

View File

@ -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