Possible partial fix for `point_id` reconcilation.

pull/1547/head
Rick Carlino 2019-10-29 11:33:47 -05:00
parent c418e4b263
commit 5072ad0175
1 changed files with 12 additions and 5 deletions

View File

@ -15,7 +15,7 @@ module PointGroups
end
def validate
validate_point_ids if point_ids.any?
validate_point_ids if point_ids.present?
validate_sort_type
end
@ -38,19 +38,26 @@ module PointGroups
end
def maybe_reconcile_points
# Nil means "ignore"
# [] means "reset"
return if point_ids.nil?
# STEP 0: Setup
@old_point_ids = Set.new(point_group.point_group_items.pluck(:id))
@old_point_ids = Set.new(point_group.point_group_items.pluck(:point_id))
@new_point_ids = Set.new(point_ids)
@dont_delete = @new_point_ids & @old_point_ids
@do_delete = (@old_point_ids - @dont_delete).to_a
# STEP 1: "Garbage Collection" of PGIs that are no longer used.
PointGroupItem.where(id: @do_delete).map(&:destroy!)
PointGroupItem
.where(point_group_id: point_group.id)
.where(point_id: @do_delete)
.destroy_all
# STEP 2: Create missing PGIs
@do_create = (@new_point_ids - @dont_delete)
PointGroupItem.create!(@do_create.to_a.uniq.map do |id|
{ point_id: id, point_group_id: point_group.id }
PointGroupItem.create!(@do_create.to_a.uniq.map do |point_id|
{ point_id: point_id, point_group_id: point_group.id }
end)
end
end