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
-
#changes ⇒ Object
Returns the
ActiveRecordchanges that were present during version creation. -
#revert! ⇒ Object
Reverts the parent
ActiveRecordinstance to the saved attributes of thisversion. -
#untrash! ⇒ Object
Inserts a trashed
versionback into its parentActiveRecordtable with its original primary key.
Instance Method Details
#changes ⇒ Object
Returns the ActiveRecord changes that were present during version creation.
122 123 124 |
# File 'lib/hoardable/version_model.rb', line 122 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.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/hoardable/version_model.rb', line 86 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.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/hoardable/version_model.rb', line 102 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 |