From 8b9933ebef5408b1d3f460581c3e50186e82cf8a Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Sun, 2 Jun 2024 11:52:48 +0200 Subject: [PATCH] Change `Relation#load` to match AR implementation `Relation#load` method: - Show that a block is accepted as an argument - Checks for `scheduled?` predicate - Returns the `Relation` itself (not an array) This signature is available from 7.0.0, the minimum requested AR version Close #297 Ref: - rails/rails@caa178c - rails/rails@2a90104 --- lib/chrono_model/patches/relation.rb | 6 ++++-- spec/chrono_model/time_machine/as_of_spec.rb | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/chrono_model/patches/relation.rb b/lib/chrono_model/patches/relation.rb index 56eee6fa..8a7a195e 100644 --- a/lib/chrono_model/patches/relation.rb +++ b/lib/chrono_model/patches/relation.rb @@ -23,10 +23,12 @@ def empty_scope? @values == klass.unscoped.as_of(as_of_time).values end - def load - return super unless @_as_of_time && !loaded? + def load(&block) + return super unless @_as_of_time && (!loaded? || scheduled?) super.each { |record| record.as_of_time!(@_as_of_time) } + + self end def merge(*) diff --git a/spec/chrono_model/time_machine/as_of_spec.rb b/spec/chrono_model/time_machine/as_of_spec.rb index 0d00099f..acae03b9 100644 --- a/spec/chrono_model/time_machine/as_of_spec.rb +++ b/spec/chrono_model/time_machine/as_of_spec.rb @@ -181,5 +181,11 @@ it { expect($t.baz.as_of($t.bar.ts[2]).bar.foo.name).to eq 'new foo' } it { expect($t.baz.as_of($t.bar.ts[3]).bar.foo.name).to eq 'new foo' } end + + describe '#load' do + it 'returns a relation' do + expect(Foo.as_of(Time.now).load).to be_an(ActiveRecord::Relation) + end + end end end