Skip to content

Commit

Permalink
Merged in r2-2648-family-details-disappear (pull request #6559)
Browse files Browse the repository at this point in the history
R2-2648 - Linking case to family makes local family details entries disappear
  • Loading branch information
dhernandez-quoin authored and pnabutovsky committed Oct 25, 2023
2 parents f7f2985 + 0211087 commit fd8fa0a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
16 changes: 14 additions & 2 deletions app/models/concerns/family_linkable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
# Copyright (c) 2014 - 2023 UNICEF. All rights reserved.

# A shared concern for all Records that can be linked to a Family Record
# rubocop:disable Metrics/ModuleLength
module FamilyLinkable
extend ActiveSupport::Concern

included do
belongs_to :family, foreign_key: :family_id, optional: true

before_save :stamp_family_fields
before_save :associate_to_family
before_save :sync_family_members
after_save :associate_family_member
after_save :save_family
after_save :disassociate_family_member
Expand All @@ -23,9 +24,19 @@ def stamp_family_fields
self.family_member_id = nil if family_id.nil?
end

def associate_to_family
def sync_family_members
return unless changes_to_save.key?('family_id') && family_member_id.blank? && family.present?

push_family_details_to_family_members
push_to_family_members
end

def push_family_details_to_family_members
family.family_members = [] if family.family_members.blank?
family.family_members += FamilyLinkageService.build_family_members_for_details([], family_details_section || [])
end

def push_to_family_members
family_member = FamilyLinkageService.child_to_family_member(self)
family.family_members << family_member
self.family_member_id = family_member['unique_id']
Expand Down Expand Up @@ -133,3 +144,4 @@ def family_changes(changes)
field_names
end
end
# rubocop:enable Metrics/ModuleLength
9 changes: 3 additions & 6 deletions app/services/family_linkage_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ def new_family_linked_child(user, source_case, family_detail_id)
def link_child_to_new_family(user, child)
family = Family.new_with_user(user, child_to_family(child))
family.module_id = child.module_id
family.family_members = build_family_members_for_details([], child.family_details_section)
family_member = child_to_family_member(child)
family.family_members << family_member

child.family = family
child.family_member_id = family_member['unique_id']
child.push_family_details_to_family_members
child.push_to_family_members
end

def family_member_to_child(user, family_member)
Expand All @@ -83,7 +80,7 @@ def child_to_family_member(child)
next
end

memo[elem['source']] = child.data[target]
memo[elem['source']] = child.data[target] if child.data.key?(target)
end
end

Expand Down
41 changes: 41 additions & 0 deletions spec/models/concerns/family_linkable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,47 @@
end
end

describe 'sync_family_members' do
it 'associates a family to a case and copies the family details as family members' do
child = Child.new_with_user(
user,
{
name_first: 'Name1',
age: 8,
sex: 'male',
family_details_section: [
{ 'unique_id' => '10001', 'relation' => 'father', 'relation_name' => 'Name2' },
{ 'unique_id' => '10002', 'relation' => 'mother', 'relation_name' => 'Name3' }
]
}
)
child.save!
family = Family.new_with_user(user, { family_number: 'f-10001' })
family.save!

child.family = family
child.save!

expect(child.family_member_id).not_to be_nil
expect(child.family_id).to eq(family.id)
expect(family.family_members.size).to eq(3)
expect(family.family_members).to match_array(
[
{ 'unique_id' => '10001', 'family_relationship' => 'father', 'relation_name' => 'Name2' },
{ 'unique_id' => '10002', 'family_relationship' => 'mother', 'relation_name' => 'Name3' },
{
'unique_id' => child.family_member_id,
'relation_name' => 'Name1',
'relation_sex' => 'male',
'relation_age' => 8,
'case_id' => child.id,
'case_id_display' => child.case_id_display
}
]
)
end
end

describe 'disassociate_from_family' do
it 'disassociates a case from a family record' do
child.update_properties(user, { 'family_id' => nil })
Expand Down

0 comments on commit fd8fa0a

Please sign in to comment.