Module: VestalVersions::Changes

Extended by:
ActiveSupport::Concern
Defined in:
lib/vestal_versions/changes.rb

Overview

Provides the ability to manipulate hashes in the specific format that ActiveRecord gives to dirty attribute changes: string keys and unique, two-element array values.

Defined Under Namespace

Modules: HashMethods

Instance Method Summary collapse

Instance Method Details

#changes_between(from, to) ⇒ Object

Methods available to versioned ActiveRecord::Base instances in order to manage changes used for version creation. Collects an array of changes from a record’s versions between the given range and compiles them into one summary hash of changes. The from and to arguments can each be either a version number, a symbol representing an association proxy method, a string representing a version tag or a version object itself.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vestal_versions/changes.rb', line 18

def changes_between(from, to)
  from_number, to_number = versions.number_at(from), versions.number_at(to)
  return {} if from_number == to_number
  chain = versions.between(from_number, to_number).reject(&:initial?)
  return {} if chain.empty?

  backward = from_number > to_number
  backward ? chain.pop : chain.shift unless from_number == 1 || to_number == 1

  chain.inject({}) do |changes, version|
    changes.append_changes!(backward ? version.changes.reverse_changes : version.changes)
  end
end