Clean up pool version diffs

Related to #1724
This commit is contained in:
Toks
2013-10-26 00:38:45 -04:00
parent 5cfea6a3d0
commit 2924bf6086
2 changed files with 30 additions and 8 deletions

View File

@@ -49,4 +49,23 @@ class PoolVersion < ActiveRecord::Base
def post_id_array
@post_id_array ||= post_ids.scan(/\d+/).map(&:to_i)
end
def diff(version)
new_posts = post_id_array
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
}
end
def changes
@changes ||= diff(previous)
end
def previous
PoolVersion.where(["pool_id = ? and updated_at < ?", pool_id, updated_at]).order("updated_at desc").first
end
end