Skip to content

Commit

Permalink
Restrict to Rails < 7, minimize active record patch
Browse files Browse the repository at this point in the history
... by delegating to super so we don't have to copy more of active
record than necessary.
  • Loading branch information
maiwald committed Mar 25, 2024
1 parent 6c8b959 commit 1f776c3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/permanent_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ def self.dependent_permanent_reflections(klass)
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.include PermanentRecords::ActiveRecord

if [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] == [5, 2] || ActiveRecord::VERSION::MAJOR > 5
if [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] == [5, 2] ||
ActiveRecord::VERSION::MAJOR.between(5, 7)
require 'permanent_records/active_record_5_2'
end
end
18 changes: 6 additions & 12 deletions lib/permanent_records/active_record_5_2.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
# rubocop:disable Metrics/AbcSize
# Support destroy for rails belongs_to assocations.
module HandlePermanentRecordsDestroyedInBelongsToAssociation
def handle_dependency
return unless load_target

# only patch :destroy case and delegate to super otherwise
case options[:dependent]
when :destroy
target.destroy
raise ActiveRecord::Rollback if target.respond_to?(:deleted?) && !target.deleted?
else
target.send(options[:dependent])
super
end
end
end

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity
# Support destroy for rails 5.2. has_on associations.
# Support destroy for rails 5.2. has_one associations.
module HandlePermanentRecordsDestroyedInHasOneAssociation
def delete(method = options[:dependent])
return unless load_target

# only patch :destroy case and delegate to super otherwise
case method
when :delete
target.delete
when :destroy
target.destroyed_by_association = reflection
target.destroy
throw(:abort) if target.respond_to?(:deleted?) && !target.deleted?
when :nullify
target.update_columns(reflection.foreign_key => nil) if target.persisted?
else
super(method)
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
ActiveRecord::Associations::BelongsToAssociation.prepend(HandlePermanentRecordsDestroyedInBelongsToAssociation)
ActiveRecord::Associations::HasOneAssociation.prepend(HandlePermanentRecordsDestroyedInHasOneAssociation)
4 changes: 2 additions & 2 deletions permanent_records.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Gem::Specification.new do |s|
s.homepage = 'https://github.com/JackDanger/permanent_records'
s.require_paths = ['lib']

s.add_runtime_dependency 'activerecord'
s.add_runtime_dependency 'activesupport'
s.add_runtime_dependency 'activerecord', '< 7'
s.add_runtime_dependency 'activesupport', '< 7'

s.add_development_dependency 'appraisal'
s.add_development_dependency 'database_cleaner', '>= 1.5.1'
Expand Down

0 comments on commit 1f776c3

Please sign in to comment.