Raise helpful error when CVC is abstract
Instead of crashing when misconfigured Custom Version Classes are used, an error will be raised earlier, with a much more helpful message. [Fixes #961]
This commit is contained in:
parent
176b2903bb
commit
c5b4a9ec66
|
@ -26,7 +26,9 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
|
|||
|
||||
### Added
|
||||
|
||||
- None
|
||||
- [#961](https://github.com/airblade/paper_trail/issues/961) - Instead of
|
||||
crashing when misconfigured Custom Version Classes are used, an error will be
|
||||
raised earlier, with a much more helpful message.
|
||||
|
||||
### Fixed
|
||||
|
||||
|
|
|
@ -12,6 +12,16 @@ module PaperTrail
|
|||
or disable belongs_to_required_by_default.
|
||||
STR
|
||||
|
||||
E_HPT_ABSTRACT_CLASS = <<~STR.squish.freeze
|
||||
An application model (%s) has been configured to use PaperTrail (via
|
||||
`has_paper_trail`), but the version model it has been told to use (%s) is
|
||||
an `abstract_class`. This could happen when an advanced feature called
|
||||
Custom Version Classes (http://bit.ly/2G4ch0G) is misconfigured. When all
|
||||
version classes are custom, PaperTrail::Version is configured to be an
|
||||
`abstract_class`. This is fine, but all application models must be
|
||||
configured to use concrete (not abstract) version models.
|
||||
STR
|
||||
|
||||
def initialize(model_class)
|
||||
@model_class = model_class
|
||||
end
|
||||
|
@ -98,6 +108,14 @@ module PaperTrail
|
|||
Gem::Version.new(ActiveRecord::VERSION::STRING)
|
||||
end
|
||||
|
||||
# Raises an error if the provided class is an `abstract_class`.
|
||||
# @api private
|
||||
def assert_concrete_activerecord_class(class_name)
|
||||
if class_name.constantize.abstract_class?
|
||||
raise format(E_HPT_ABSTRACT_CLASS, @model_class, class_name)
|
||||
end
|
||||
end
|
||||
|
||||
def cannot_record_after_destroy?
|
||||
Gem::Version.new(ActiveRecord::VERSION::STRING).release >= Gem::Version.new("5") &&
|
||||
::ActiveRecord::Base.belongs_to_required_by_default
|
||||
|
@ -123,6 +141,8 @@ module PaperTrail
|
|||
|
||||
@model_class.send :attr_accessor, :paper_trail_event
|
||||
|
||||
assert_concrete_activerecord_class(@model_class.version_class_name)
|
||||
|
||||
@model_class.has_many(
|
||||
@model_class.versions_association_name,
|
||||
-> { order(model.timestamp_sort_order) },
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AbstractVersion < ActiveRecord::Base
|
||||
include PaperTrail::VersionConcern
|
||||
self.abstract_class = true
|
||||
end
|
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
module PaperTrail
|
||||
::RSpec.describe ModelConfig do
|
||||
describe "when has_paper_trail is called" do
|
||||
it "raises an error" do
|
||||
expect {
|
||||
class MisconfiguredCVC < ActiveRecord::Base
|
||||
has_paper_trail class_name: "AbstractVersion"
|
||||
end
|
||||
}.to raise_error(
|
||||
/use concrete \(not abstract\) version models/
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue