From 6668d79bbea3002a9d262bad4f77eeabf912cabb Mon Sep 17 00:00:00 2001 From: Vasif Date: Fri, 22 Dec 2023 14:19:23 +0400 Subject: [PATCH 1/2] Fix single rel caching issue. --- lib/active_graph/node/has_n.rb | 6 +++++- spec/e2e/association_proxy_spec.rb | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/active_graph/node/has_n.rb b/lib/active_graph/node/has_n.rb index 00ce6a7e9..7140dbf80 100644 --- a/lib/active_graph/node/has_n.rb +++ b/lib/active_graph/node/has_n.rb @@ -111,13 +111,17 @@ def init_cache def add_to_cache(object, rel = nil) (@cached_rels ||= []) << rel if rel - (@cached_result ||= []).tap { |results| results << object if object && !results.include?(object) } + (@cached_result ||= []).tap { |results| results << object if !results.include?(object) } if object end def rels @cached_rels || super.tap { |rels| rels.each { |rel| add_to_cache(nil, rel) } } end + def rel + rels.first + end + def cache_query_proxy_result (result_cache_proc_cache || @query_proxy).to_a.tap { |result| cache_result(result) } end diff --git a/spec/e2e/association_proxy_spec.rb b/spec/e2e/association_proxy_spec.rb index 6ed861b28..86a7c5a61 100644 --- a/spec/e2e/association_proxy_spec.rb +++ b/spec/e2e/association_proxy_spec.rb @@ -12,16 +12,23 @@ has_one :out, :favorite_lesson, type: nil, model_class: :Lesson has_many :out, :homework, type: :HOMEWORK, model_class: %w[Lesson Exam] has_many :out, :friends, type: :friend, model_class: :Student + has_one :out, :school, rel_class: :SchoolRel end stub_relationship_class('LessonEnrollment') do from_class :Student to_class :Lesson - type :has_studet + type :has_student property :grade end + stub_relationship_class('SchoolRel') do + from_class :Student + to_class :School + type :has_student + end + stub_node_class('Lesson') do property :subject property :level, type: Integer @@ -34,6 +41,11 @@ has_many :in, :lessons, model_class: :Lesson, origin: :exams_given has_many :out, :students, type: :has_student, model_class: :Student end + + stub_node_class('School') do + property :name + has_many :out, :students, type: :has_student, model_class: :Student + end end let(:billy) { Student.create(name: 'Billy') } @@ -44,6 +56,7 @@ let(:science_exam2) { Exam.create(name: 'Science Exam 2') } let(:leszek) { Student.create(name: 'Leszek', friends: [zinto]) } let(:zinto) { Student.create(name: 'Zinto') } + let(:code_academy) { School.create(name: 'Code Academy') } before do [math, science].each { |lesson| billy.lessons << lesson } @@ -52,6 +65,7 @@ science.exams_given << science_exam science.exams_given << science_exam2 billy.favorite_lesson = math + billy.school = code_academy end context 'self referencing relationships' do @@ -314,11 +328,17 @@ end describe '#rels' do - it 'caches results for consecutive calls' do + it 'caches multi rels for consecutive calls' do expect_queries(1) do 2.times { billy.lessons.rels } end end + + it 'caches single rel for consecutive calls' do + expect_queries(1) do + 2.times { billy.school.rel } + end + end end describe '#inspect' do From 1443ca8cd90fce0640c1c9344d1eb7342c4319e0 Mon Sep 17 00:00:00 2001 From: Vasif Date: Thu, 28 Dec 2023 16:59:26 +0400 Subject: [PATCH 2/2] fix failed specs --- spec/e2e/association_proxy_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/e2e/association_proxy_spec.rb b/spec/e2e/association_proxy_spec.rb index 86a7c5a61..5dbe01c83 100644 --- a/spec/e2e/association_proxy_spec.rb +++ b/spec/e2e/association_proxy_spec.rb @@ -44,7 +44,7 @@ stub_node_class('School') do property :name - has_many :out, :students, type: :has_student, model_class: :Student + has_many :out, :students, type: :has_student, model_class: :School end end