Skip to content

Commit

Permalink
Do not allow multiple has_paper_trail definitions for models
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima authored and jaredbeck committed Sep 14, 2024
1 parent ff7b398 commit 82b73c9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Breaking Changes

- None
- [#1478](https://github.com/paper-trail-gem/paper_trail/issues/1478) Do not allow
multiple `has_paper_trail` definitions for models. Previously, when `has_paper_trail`
was called on a parent and a child from STI, then possibly multiple `version` records
will be created per event (`create`, `destroy` etc).

### Added

Expand Down
2 changes: 2 additions & 0 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ module ClassMethods
#
# @api public
def has_paper_trail(options = {})
raise Error, "has_paper_trail must be called only once" if self < InstanceMethods

defaults = PaperTrail.config.has_paper_trail_defaults
paper_trail.setup(defaults.merge(options))
end
Expand Down
32 changes: 24 additions & 8 deletions spec/dummy_app/app/models/callback_modifier.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

class CallbackModifier < ApplicationRecord
has_paper_trail on: []
module CallbackModifier
extend ActiveSupport::Concern

included do
self.table_name = "callback_modifiers"
end

def test_destroy
transaction do
Expand All @@ -17,33 +21,45 @@ def flagged_deleted?
end
end

class BeforeDestroyModifier < CallbackModifier
class BeforeDestroyModifier < ApplicationRecord
include CallbackModifier

has_paper_trail on: []
paper_trail.on_destroy :before
end

unless ActiveRecord::Base.belongs_to_required_by_default
class AfterDestroyModifier < CallbackModifier
class AfterDestroyModifier < ApplicationRecord
include CallbackModifier

has_paper_trail on: []
paper_trail.on_destroy :after
end
end

class NoArgDestroyModifier < CallbackModifier
class NoArgDestroyModifier < ApplicationRecord
include CallbackModifier

has_paper_trail on: []
paper_trail.on_destroy
end

class UpdateModifier < CallbackModifier
class UpdateModifier < ApplicationRecord
include CallbackModifier

has_paper_trail on: []
paper_trail.on_update
end

class CreateModifier < CallbackModifier
class CreateModifier < ApplicationRecord
include CallbackModifier

has_paper_trail on: []
paper_trail.on_create
end

class DefaultModifier < CallbackModifier
class DefaultModifier < ApplicationRecord
include CallbackModifier

has_paper_trail
end
10 changes: 10 additions & 0 deletions spec/paper_trail/model_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
module PaperTrail
::RSpec.describe ModelConfig do
describe "has_paper_trail" do
it "raises when called multiple times" do
klass = Class.new(ApplicationRecord) do
has_paper_trail
end

expect do
klass.has_paper_trail
end.to raise_error(Error, "has_paper_trail must be called only once")
end

describe "versions:" do
it "name can be passed instead of an options hash", :deprecated do
allow(PaperTrail.deprecator).to receive(:warn)
Expand Down

0 comments on commit 82b73c9

Please sign in to comment.