Skip to content

Instantly share code, notes, and snippets.

@micahbf
Created May 26, 2016 18:01
Show Gist options
  • Save micahbf/db4d202832d03de60b2fe849808fb296 to your computer and use it in GitHub Desktop.
Save micahbf/db4d202832d03de60b2fe849808fb296 to your computer and use it in GitHub Desktop.
Find versions where a specific thing was changed
class ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_PaperTrail_Version
def changes_to
raise ArgumentError, "block required" unless block_given?
result = []
changes = scope.to_a.map do |version|
version_state = yield version.reify
[version_state, version]
end.each_cons(2).select do |old, new|
old.first != new.first
end
return [] if changes.empty?
changes << [changes.last.last, nil]
changes.map do |(state, version), _|
[state, {whodunnit: version.whodunnit, when: version.created_at, version_id: version.id}]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment