From c5baf370b0dcde16c3b2731e9d1041e5c6f76d75 Mon Sep 17 00:00:00 2001 From: Jared Beck Date: Sun, 29 Aug 2021 23:51:00 -0400 Subject: [PATCH] Extract private method init_model_by_finding_item_id --- .rubocop_todo.yml | 2 +- lib/paper_trail/reifier.rb | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 07ccb876..22d8b95e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -9,7 +9,7 @@ # Offense count: 5 # Configuration parameters: IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: - Max: 18.3 # Goal: 17, the default + Max: 17.5 # Goal: 17, the default # Offense count: 1 # Configuration parameters: IgnoredMethods. diff --git a/lib/paper_trail/reifier.rb b/lib/paper_trail/reifier.rb index 7a37e3a0..d78f8720 100644 --- a/lib/paper_trail/reifier.rb +++ b/lib/paper_trail/reifier.rb @@ -60,9 +60,7 @@ module PaperTrail model = if options[:dup] == true || version.event == "destroy" klass.new else - find_cond = { klass.primary_key => version.item_id } - - version.item || klass.unscoped.where(find_cond).first || klass.new + version.item || init_model_by_finding_item_id(klass, version) || klass.new end if options[:unversioned_attributes] == :nil && !model.new_record? @@ -72,6 +70,11 @@ module PaperTrail model end + # @api private + def init_model_by_finding_item_id(klass, version) + klass.unscoped.where(klass.primary_key => version.item_id).first + end + # Look for attributes that exist in `model` and not in this version. # These attributes should be set to nil. Modifies `attrs`. # @api private