Module: VersionedRecord::InstanceMethods
- Defined in:
- lib/versioned_record.rb
Instance Method Summary collapse
-
#_id ⇒ Object
Just the ID integer value (not the composite id, version key).
-
#build_version(new_attrs = {}) ⇒ Object
Build (but do not save) a new version of the record This allows you to use the object in forms etc After the record is saved, all previous versions will be deprecated and this record will be marked as current.
-
#create_version(new_attrs = {}) ⇒ Object
Same as #create_version! but will not raise if the record is invalid.
-
#create_version!(new_attrs = {}) ⇒ Object
Create a new version of the existing record A new version can only be created once for a given record and subsequent versions must be created by calling create_version! on the latest version.
-
#current_version ⇒ Object
Retrieve the current version of an object (May be itself).
-
#deprecate_old_versions_after_create! ⇒ Object
Ensure that old versions are deprecated when we save (only applies on create).
-
#versions ⇒ Object
Retrieve all versions of this record Can be chained with other scopes.
Instance Method Details
#_id ⇒ Object
Returns just the ID integer value (not the composite id, version key).
26 27 28 |
# File 'lib/versioned_record.rb', line 26 def _id id[0] end |
#build_version(new_attrs = {}) ⇒ Object
Build (but do not save) a new version of the record This allows you to use the object in forms etc After the record is saved, all previous versions will be deprecated and this record will be marked as current
70 71 72 73 74 75 |
# File 'lib/versioned_record.rb', line 70 def build_version(new_attrs = {}) new_version = self.class.new(new_version_attrs(new_attrs)).tap do |built| built.deprecate_old_versions_after_create! preserve_has_one_associations_to(built) end end |
#create_version(new_attrs = {}) ⇒ Object
Same as #create_version! but will not raise if the record is invalid
54 55 56 57 58 |
# File 'lib/versioned_record.rb', line 54 def create_version(new_attrs = {}) create_operation do self.class.create(new_version_attrs(new_attrs)) end end |
#create_version!(new_attrs = {}) ⇒ Object
Create a new version of the existing record A new version can only be created once for a given record and subsequent versions must be created by calling create_version! on the latest version
Attributes that are not specified here will be copied to the new version from the previous version
This method will still fire ActiveRecord callbacks for save/create etc as per normal record creation
45 46 47 48 49 |
# File 'lib/versioned_record.rb', line 45 def create_version!(new_attrs = {}) create_operation do self.class.create!(new_version_attrs(new_attrs)) end end |
#current_version ⇒ Object
Retrieve the current version of an object (May be itself)
91 92 93 |
# File 'lib/versioned_record.rb', line 91 def current_version versions.current_versions.first end |
#deprecate_old_versions_after_create! ⇒ Object
Ensure that old versions are deprecated when we save (only applies on create)
97 98 99 |
# File 'lib/versioned_record.rb', line 97 def deprecate_old_versions_after_create! @deprecate_old_versions_after_create = true end |
#versions ⇒ Object
Retrieve all versions of this record Can be chained with other scopes
84 85 86 |
# File 'lib/versioned_record.rb', line 84 def versions self.class.where(id: self._id) end |