Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Preloader association fix #162

Merged
merged 2 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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