Possible partial fix for `point_id` reconcilation.
parent
c418e4b263
commit
5072ad0175
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue