Skip to content

Commit

Permalink
Merge pull request #162 from ifad/bugfix/fix-preloaded-records-160
Browse files Browse the repository at this point in the history
WIP Preloader association fix
  • Loading branch information
tagliala authored May 20, 2022
2 parents e2c790e + f6f3eba commit fb679d6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/chrono_model/patches/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def preload_associations(records) # :nodoc:
end
end

def empty_scope?
return super unless @_as_of_time

@values == klass.as_of(as_of_time).values
end

def load
return super unless @_as_of_time && !loaded?

Expand Down
1 change: 1 addition & 0 deletions spec/chrono_model/history_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
expected['boos'] = Boo::History if defined?(Boo::History)

expected['sub_bars'] = SubBar::History if defined?(SubBar::History)
expected['sub_sub_bars'] = SubSubBar::History if defined?(SubSubBar::History)

# default_scope_spec
expected['defoos'] = Defoo::History if defined?(Defoo::History)
Expand Down
2 changes: 2 additions & 0 deletions spec/chrono_model/time_machine/as_of_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
it { expect(Foo.as_of($t.subbar.ts[1]).includes(:bars, :sub_bars).first.sub_bars.first.name).to eq 'bar sub-bar' }
it { expect(Foo.as_of($t.subbar.ts[2]).includes(:bars, :sub_bars).first.sub_bars.first.name).to eq 'sub-bar sub-bar' }
it { expect(Foo.as_of($t.subbar.ts[3]).includes(:bars, :sub_bars).first.sub_bars.first.name).to eq 'new sub-bar' }

it { expect(Foo.as_of(Time.now).includes(:bars, :sub_bars, :sub_sub_bars).first.sub_sub_bars.compact.size).to eq 1 }
end

it 'does not raise RecordNotFound when no history records are found' do
Expand Down
15 changes: 15 additions & 0 deletions spec/support/time_machine/structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ::Foo < ActiveRecord::Base

has_many :bars
has_many :sub_bars, through: :bars
has_many :sub_sub_bars, through: :sub_bars

belongs_to :goo, class_name: 'FooGoo', optional: true
end
Expand Down Expand Up @@ -93,10 +94,22 @@ class ::SubBar < ActiveRecord::Base
include ChronoModel::TimeMachine

belongs_to :bar
has_many :sub_sub_bars

has_timeline with: :bar
end

adapter.create_table 'sub_sub_bars', temporal: true do |t|
t.string :name
t.references :sub_bar
end

class ::SubSubBar < ActiveRecord::Base
include ChronoModel::TimeMachine

belongs_to :sub_bar
end

adapter.create_table 'bazs' do |t|
t.string :name
t.references :bar
Expand Down Expand Up @@ -126,6 +139,8 @@ class ::Baz < ActiveRecord::Base
$t.subbar = ts_eval { SubBar.create! name: 'sub-bar', bar: $t.bar }
ts_eval($t.subbar) { update! name: 'bar sub-bar' }

ts_eval { SubSubBar.create! name: 'sub-sub-bar', sub_bar: $t.subbar }

ts_eval($t.foo) { update! name: 'new foo' }

ts_eval($t.bar) { update! name: 'bar bar' }
Expand Down

0 comments on commit fb679d6

Please sign in to comment.