Module: VersionedRecord::InstanceMethods

Defined in:
lib/versioned_record.rb

Instance Method Summary collapse

Instance Method Details

#_idObject

Returns just the ID integer value (not the composite id, version key).

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

Examples:


new_version = first_version.build_version
new_version.save


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

See Also:

  • VersionedRecord#create_version!


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

Examples:


person_v1 = Person.create(name: 'Dan')
person_v2 = person_v1.create_version!(name: 'Daniel')


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_versionObject

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

#versionsObject

Retrieve all versions of this record Can be chained with other scopes

Examples:

Versions ordered by version number


person.versions.order(:version)


84
85
86
# File 'lib/versioned_record.rb', line 84

def versions
  self.class.where(id: self._id)
end