From 159bdc2c69aa638093addc671fae2adb519e3cdc Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Tue, 3 Dec 2024 17:34:49 -0600 Subject: [PATCH 1/3] Fix issue with orphaned codes --- app/models/code.rb | 2 +- app/models/persona.rb | 6 +++--- app/models/response.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/code.rb b/app/models/code.rb index 22938fd..583f8ea 100644 --- a/app/models/code.rb +++ b/app/models/code.rb @@ -15,7 +15,7 @@ class Code validates_uniqueness_of :name, scope: :context has_many :in, :personas, rel_class: :Experiences - has_many :in, :categories, rel_class: :CategorizedAs, dependent: :delete_orphans + has_many :in, :categories, rel_class: :CategorizedAs # Given a context, generates a hash with each unique Codes as a key and the counts of its uses as a value. def self.histogram(context) diff --git a/app/models/persona.rb b/app/models/persona.rb index 1d29ad1..1703ce6 100644 --- a/app/models/persona.rb +++ b/app/models/persona.rb @@ -11,9 +11,9 @@ class Persona validates :name, presence: true validates :case_id, presence: true - has_many :out, :codes, rel_class: :Experiences, dependent: :delete_orphans - has_many :out, :identities, rel_class: :IdentifiesWith, dependent: :delete_orphans - has_many :out, :keywords, rel_class: :ReflectsOn, dependent: :delete_orphans + has_many :out, :codes, rel_class: :Experiences + has_many :out, :identities, rel_class: :IdentifiesWith + has_many :out, :keywords, rel_class: :ReflectsOn # Traverses the codes-[]-categories relationship to return a list of unique Categories. def categories diff --git a/app/models/response.rb b/app/models/response.rb index 49ee4ba..fdfb2cd 100644 --- a/app/models/response.rb +++ b/app/models/response.rb @@ -41,7 +41,7 @@ def populate_codes self.raw_codes.compact.uniq.each do |name| if code = Code.find_or_create_by(name: name, context: context_name) next unless code.valid? - Experiences.create(from_node: persona, to_node: code) + persona.codes << code end end From f2eb08b3661d54e0f7309271cba6b54436960d3c Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Tue, 3 Dec 2024 17:37:26 -0600 Subject: [PATCH 2/3] Symmetrical change to identity logic --- app/models/response.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/response.rb b/app/models/response.rb index fdfb2cd..288a584 100644 --- a/app/models/response.rb +++ b/app/models/response.rb @@ -58,7 +58,7 @@ def populate_identities self.raw_codes.compact.uniq.each do |name| if identity = Identity.find_or_create_by(name: name.strip, context: context_name) next unless identity.valid? - IdentifiesWith.create(from_node: persona, to_node: identity) + persona.identities << identity end end From 06488ea726102e43cfce53f831c0cc94d708f688 Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Tue, 3 Dec 2024 17:51:45 -0600 Subject: [PATCH 3/3] Reflect change in relationship building in spec --- spec/models/response_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/response_spec.rb b/spec/models/response_spec.rb index 58d792f..80887a7 100644 --- a/spec/models/response_spec.rb +++ b/spec/models/response_spec.rb @@ -8,6 +8,8 @@ allow(Persona).to receive(:find_or_create_by).and_return(persona) allow(Persona).to receive(:find_or_initialize_by).and_return(persona) + allow(persona).to receive(:codes).and_return([]) + allow(persona).to receive(:identities).and_return([]) allow(Identity).to receive(:find_or_create_by).and_return(identity) allow(Identity).to receive(:reap_orphans) @@ -43,14 +45,12 @@ it "creates identities" do allow(Response).to receive(:find).and_return(response_1) expect(Identity).to receive(:find_or_create_by).with(name: "not okay", context: "age").and_return(identity) - expect(IdentifiesWith).to receive(:create) response_1.sync_to_graph end it "creates codes" do allow(Response).to receive(:find).and_return(response_2) expect(Code).to receive(:find_or_create_by).with(name: "just okay", context: "age"). and_return(code) - expect(Experiences).to receive(:create) response_2.sync_to_graph end