Module: VestalVersions::Reversion
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/vestal_versions/reversion.rb
Overview
Enables versioned ActiveRecord::Base instances to revert to a previously saved version.
Instance Method Summary collapse
-
#revert_to(value) ⇒ Object
Accepts a value corresponding to a specific version record, builds a history of changes between that version and the current version, and then iterates over that history updating the object’s attributes until the it’s reverted to its prior state.
-
#revert_to!(value) ⇒ Object
Behaves similarly to the
revert_to
method except that it automatically saves the record after the reversion. -
#reverted? ⇒ Boolean
Returns a boolean specifying whether the object has been reverted to a previous version or if the object represents the latest version in the version history.
-
#version ⇒ Object
Provides the base instance methods required to revert a versioned instance.
Instance Method Details
#revert_to(value) ⇒ Object
Accepts a value corresponding to a specific version record, builds a history of changes between that version and the current version, and then iterates over that history updating the object’s attributes until the it’s reverted to its prior state.
The single argument should adhere to one of the formats as documented in the at
method of VestalVersions::Versions.
After the object is reverted to the target version, it is not saved. In order to save the object after the reversion, use the revert_to!
method.
The version number of the object will reflect whatever version has been reverted to, and the return value of the revert_to
method is also the target version number.
24 25 26 27 28 29 30 31 32 |
# File 'lib/vestal_versions/reversion.rb', line 24 def revert_to(value) to_number = versions.number_at(value) changes_between(version, to_number).each do |attribute, change| write_attribute(attribute, change.last) end reset_version(to_number) end |
#revert_to!(value) ⇒ Object
Behaves similarly to the revert_to
method except that it automatically saves the record after the reversion. The return value is the success of the save.
36 37 38 39 40 |
# File 'lib/vestal_versions/reversion.rb', line 36 def revert_to!(value) revert_to(value) reset_version if saved = save saved end |
#reverted? ⇒ Boolean
Returns a boolean specifying whether the object has been reverted to a previous version or if the object represents the latest version in the version history.
44 45 46 |
# File 'lib/vestal_versions/reversion.rb', line 44 def reverted? version != last_version end |
#version ⇒ Object
Provides the base instance methods required to revert a versioned instance. Returns the current version number for the versioned object.
8 9 10 |
# File 'lib/vestal_versions/reversion.rb', line 8 def version @version ||= last_version end |