Skip to content

Commit

Permalink
Add warnings for missing methods we want to prepend
Browse files Browse the repository at this point in the history
  • Loading branch information
jrafanie committed Jun 21, 2024
1 parent 33d07f2 commit 6799ec9
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/active_record/virtual_attributes/virtual_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,35 @@ def merge_includes(hash1, hash2)
end
end

def assert_klass_has_instance_method(klass, instance_method)
begin
klass.instance_method(instance_method)
rescue NameError => err
warn "#{klass} is missing the method our prepended code depends on: #{err}."
warn "Was the method removed or renamed upstream? See: #{__FILE__}"
end
end

if ActiveRecord.version >= Gem::Version.new(7.0) # Rails 7.0 expected methods to patch
%w[
grouped_records
].each {|method| assert_klass_has_instance_method(ActiveRecord::Associations::Preloader::Branch, method) }
elsif ActiveRecord.version >= Gem::Version.new(6.1) # Rails 6.1 methods to patch
%w[
preloaders_for_reflection
preloaders_for_hash
preloaders_for_one
grouped_records
].each {|method| assert_klass_has_instance_method(ActiveRecord::Associations::Preloader, method) }
end

# Expected methods to patch on any version
%w[
build_select
arel_column
construct_join_dependency
].each {|method| assert_klass_has_instance_method(ActiveRecord::Relation, method) }

module ActiveRecord
class Base
include ActiveRecord::VirtualAttributes::VirtualFields
Expand Down

0 comments on commit 6799ec9

Please sign in to comment.