Skip to content

Commit

Permalink
Extract private method: assert_valid_recording_order_for_on_destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredbeck committed Aug 30, 2021
1 parent 63bd41e commit df8cc94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
# Offense count: 5
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 19 # Goal: the default (17?)
Max: 18.3 # Goal: 17, the default

# Offense count: 1
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 8 # Goal: 7
Max: 8 # Goal: 7, the default

# Offense count: 1
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Max: 9 # Goal: 7
Max: 9 # Goal: 8, the default

# Offense count: 56
# Cop supports --auto-correct.
Expand Down
19 changes: 12 additions & 7 deletions lib/paper_trail/model_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ def on_create
#
# @api public
def on_destroy(recording_order = "before")
unless %w[after before].include?(recording_order.to_s)
raise ArgumentError, 'recording order can only be "after" or "before"'
end

if recording_order.to_s == "after" && cannot_record_after_destroy?
raise Error, E_CANNOT_RECORD_AFTER_DESTROY
end
assert_valid_recording_order_for_on_destroy(recording_order)

@model_class.send(
"#{recording_order}_destroy",
Expand Down Expand Up @@ -141,6 +135,17 @@ def assert_concrete_activerecord_class(class_name)
end
end

# @api private
def assert_valid_recording_order_for_on_destroy(recording_order)
unless %w[after before].include?(recording_order.to_s)
raise ArgumentError, 'recording order can only be "after" or "before"'
end

if recording_order.to_s == "after" && cannot_record_after_destroy?
raise Error, E_CANNOT_RECORD_AFTER_DESTROY
end
end

def cannot_record_after_destroy?
::ActiveRecord::Base.belongs_to_required_by_default
end
Expand Down

0 comments on commit df8cc94

Please sign in to comment.