From 8c724c258b27ed03e63445120684133a1b00e265 Mon Sep 17 00:00:00 2001 From: Toks Date: Sun, 15 Dec 2013 19:49:01 -0500 Subject: [PATCH] #2042: Fix pool histories --- app/models/pool_version.rb | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/app/models/pool_version.rb b/app/models/pool_version.rb index 341e4714b..a81d54477 100644 --- a/app/models/pool_version.rb +++ b/app/models/pool_version.rb @@ -55,12 +55,35 @@ class PoolVersion < ActiveRecord::Base old_posts = version.present? ? version.post_id_array : [] return { - :added_posts => new_posts - old_posts, - :removed_posts => old_posts - new_posts, - :unchanged_posts => new_posts & old_posts + :added_posts => array_difference_with_duplicates(new_posts, old_posts), + :removed_posts => array_difference_with_duplicates(old_posts, new_posts), + :unchanged_posts => array_intersect_with_duplicates(new_posts, old_posts) } end + def array_difference_with_duplicates(array, other_array) + new_array = array.dup + other_array.each do |id| + index = new_array.index(id) + if index + new_array.delete_at(index) + end + end + new_array + end + + def array_intersect_with_duplicates(array, other_array) + other_array = other_array.dup + array.inject([]) do |intersect, id| + index = other_array.index(id) + if index + intersect << id + other_array.delete_at(index) + end + intersect + end + end + def changes @changes ||= diff(previous) end