Module: Hoardable::VersionModel

Extended by:
ActiveSupport::Concern
Defined in:
lib/hoardable/version_model.rb

Overview

This concern is included into the dynamically generated Version kind of the parent ActiveRecord class.

Instance Method Summary collapse

Instance Method Details

#changesObject

Returns the ActiveRecord changes that were present during version creation.



119
120
121
# File 'lib/hoardable/version_model.rb', line 119

def changes
  _data&.dig("changes")
end

#revert!Object

Reverts the parent ActiveRecord instance to the saved attributes of this version. Raises an error if the version is trashed.

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hoardable/version_model.rb', line 83

def revert!
  raise(Error, "Version is trashed, cannot revert") unless hoardable_operation == "update"

  transaction do
    hoardable_source.tap do |reverted|
      reverted.reload.update!(
        hoardable_source_attributes.without(self.class.superclass.primary_key, "hoardable_id")
      )
      reverted.instance_variable_set(:@hoardable_version, self)
      reverted.run_callbacks(:reverted)
    end
  end
end

#untrash!Object

Inserts a trashed version back into its parent ActiveRecord table with its original primary key. Raises an error if the version is not trashed.

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hoardable/version_model.rb', line 99

def untrash!
  raise(Error, "Version is not trashed, cannot untrash") unless hoardable_operation == "delete"

  transaction do
    insert_untrashed_source.tap do |untrashed|
      untrashed
        .send("hoardable_client")
        .insert_hoardable_version("insert") do
          untrashed.instance_variable_set(:@hoardable_version, self)
          untrashed.run_callbacks(:untrashed)
        end
    end
  end
end