diff --git a/.rubocop.yml b/.rubocop.yml index 7bb5fa0f9..04aff53f1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -95,6 +95,10 @@ Naming/PredicateName: Naming/UncommunicativeMethodParamName: Enabled: false +# This cop does not seem to work in rubocop-rspec 1.28.0 +RSpec/DescribeClass: + Enabled: false + # Yes, ideally examples would be short. Is it possible to pick a limit and say, # "no example will ever be longer than this"? Hard to say. Sometimes they're # quite long. diff --git a/spec/dummy_app/app/models/management.rb b/spec/dummy_app/app/models/management.rb new file mode 100644 index 000000000..3e983040c --- /dev/null +++ b/spec/dummy_app/app/models/management.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# Note that there is no `type` column for this subclassed model, so changes to +# Management objects should result in Versions which have an item_type of +# Customer. +class Management < Customer +end diff --git a/spec/models/family/celebrity_family_spec.rb b/spec/models/family/celebrity_family_spec.rb index 2d12c9b7a..a2fd905f6 100644 --- a/spec/models/family/celebrity_family_spec.rb +++ b/spec/models/family/celebrity_family_spec.rb @@ -4,6 +4,17 @@ module Family RSpec.describe CelebrityFamily, type: :model, versioning: true do + describe "#joins" do + it "works on an STI model" do + described_class.create! + result = described_class. + joins(:versions). + select("families.*, max(versions.event) as event"). + first + expect(result.event).to eq("create") + end + end + describe "#create" do it "creates version with item_subtype == class.name, not base_class" do carter = described_class.create( diff --git a/spec/models/management_spec.rb b/spec/models/management_spec.rb new file mode 100644 index 000000000..bf3cb9277 --- /dev/null +++ b/spec/models/management_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "spec_helper" + +::RSpec.describe(::Management, type: :model, versioning: true) do + it "utilises the base_class for STI classes having no type column" do + expect(Management.inheritance_column).to eq("type") + expect(Management.columns.map(&:name)).not_to include("type") + + # Create, update, and destroy a Management and a Customer + customer1 = Customer.create(name: "Cust 1") + customer2 = Management.create(name: "Cust 2") + customer1.update(name: "Cust 1a") + customer2.update(name: "Cust 2a") + customer1.destroy + customer2.destroy + + # All versions end up with an `item_type` of Customer + expect( + PaperTrail::Version.where(item_type: "Customer").count + ).to eq(6) + expect( + PaperTrail::Version.where(item_type: "Management").count + ).to eq(0) + + # The item_subtype, on the other hand, is 3 and 3 + expect( + PaperTrail::Version.where(item_subtype: "Customer").count + ).to eq(3) + expect( + PaperTrail::Version.where(item_subtype: "Management").count + ).to eq(3) + end +end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 0ac641690..fe45779c6 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -5,7 +5,7 @@ # The `Person` model: # # - has a dozen associations of various types -# - has a custome serializer, TimeZoneSerializer, for its `time_zone` attribute +# - has a custom serializer, TimeZoneSerializer, for its `time_zone` attribute RSpec.describe Person, type: :model, versioning: true do describe "#time_zone" do it "returns an ActiveSupport::TimeZone" do diff --git a/spec/models/song_spec.rb b/spec/models/song_spec.rb new file mode 100644 index 000000000..d75bbdc72 --- /dev/null +++ b/spec/models/song_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "spec_helper" + +::RSpec.describe(::Song, type: :model, versioning: true) do + describe "#joins" do + it "works" do + described_class.create! + result = described_class. + joins(:versions). + select("songs.*, max(versions.event) as event"). + first + expect(result.event).to eq("create") + end + end +end