Skip to content

Commit

Permalink
Fix preloaded associations
Browse files Browse the repository at this point in the history
The old behavior tried to detect class names based on 

Fix: #142
  • Loading branch information
tagliala committed Mar 21, 2022
1 parent 63b05f4 commit 0794aea
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
31 changes: 20 additions & 11 deletions lib/chrono_model/patches/preloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ module Patches
# and it is ugly :-(.
#
module Preloader
attr_reader :options
attr_reader :chronomodel_options

# We overwrite the initializer in order to pass the +as_of_time+
# parameter above in the build_preloader
#
def initialize(options = {})
@options = options.freeze
if ActiveRecord::VERSION::STRING >= '7.0'
super(associate_by_default: true, **options)
def initialize(**options)
@chronomodel_options = options.extract!(:as_of_time, :model)
options[:scope] = chronomodel_scope(scope) if options.key?(:scope)

if options.empty?
super()
else
super **options
end
end

Expand All @@ -40,17 +44,22 @@ def initialize(options = {})
# so we use it directly.
#
def preload(records, associations, given_preload_scope = nil)
if options[:as_of_time]
preload_scope = given_preload_scope ||
ChronoModel::Patches::AsOfTimeRelation.new(options[:model])
super records, associations, chronomodel_scope(given_preload_scope)
end

private

preload_scope.as_of_time!(options[:as_of_time])
def chronomodel_scope(given_preload_scope)
preload_scope = nil

if chronomodel_options[:as_of_time]
preload_scope = given_preload_scope || ChronoModel::Patches::AsOfTimeRelation.new(chronomodel_options[:model])
preload_scope.as_of_time!(chronomodel_options[:as_of_time])
elsif given_preload_scope.respond_to?(:as_of_time)
preload_scope = given_preload_scope

end

super records, associations, preload_scope
preload_scope
end

module Association
Expand Down
15 changes: 4 additions & 11 deletions lib/chrono_model/patches/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ module Relation
include ChronoModel::Patches::AsOfTimeHolder

def preload_associations(records) # :nodoc:
return super if ActiveRecord::VERSION::STRING < '7.0'
return super unless ActiveRecord::Associations::Preloader.instance_methods.include?(:call)

preload = preload_values
preload += includes_values unless eager_loading?
scope = strict_loading_value ? StrictLoadingScope : nil
preload.each do |associations|
if as_of_time
## the tree structure of preloading ensure that children are loaded from history
klass = if associations.is_a? Hash
associations.keys[0].to_s.classify.constantize
else
associations.to_s.classify.constantize
end
scope = klass.as_of(as_of_time) if klass.respond_to?(:as_of)
end
ActiveRecord::Associations::Preloader.new(records: records, associations: associations, scope: scope).call
ActiveRecord::Associations::Preloader.new(
records: records, associations: associations, scope: scope, model: model, as_of_time: as_of_time
).call
end
end

Expand Down

0 comments on commit 0794aea

Please sign in to comment.