Skip to content

Commit

Permalink
Raise helpful error when CVC is abstract
Browse files Browse the repository at this point in the history
Instead of crashing when misconfigured Custom Version Classes are
used, an error will be raised earlier, with a much more helpful message.

[Fixes #961]
  • Loading branch information
jaredbeck committed Jan 22, 2018
1 parent 176b290 commit e43bbf1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions lib/paper_trail/model_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ class ModelConfig
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`. PaperTrail has an advanced feature called Custom
Version Classes (http://bit.ly/2G4ch0G), in which 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
Expand Down Expand Up @@ -98,6 +107,14 @@ def active_record_gem_version
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
Expand All @@ -123,6 +140,8 @@ def setup_associations(options)

@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) },
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy_app/app/versions/abstract_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class AbstractVersion < ActiveRecord::Base
include PaperTrail::VersionConcern
self.abstract_class = true
end
23 changes: 23 additions & 0 deletions spec/paper_trail/model_config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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(
format(
PaperTrail::ModelConfig::E_HPT_ABSTRACT_CLASS,
"MisconfiguredCVC",
"AbstractVersion"
)
)
end
end
end
end

0 comments on commit e43bbf1

Please sign in to comment.