Module: VersionedRecord::InstanceMethods

Defined in:
lib/versioned_record.rb

Instance Method Summary collapse

Instance Method Details

#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


68
69
70
71
72
# File 'lib/versioned_record.rb', line 68

def build_version(new_attrs = {})
  new_version = self.class.new(new_version_attrs(new_attrs)).tap do |built|
    built.deprecate_old_versions_after_create!
  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!


52
53
54
55
56
# File 'lib/versioned_record.rb', line 52

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')


43
44
45
46
47
# File 'lib/versioned_record.rb', line 43

def create_version!(new_attrs = {})
  create_operation do
    self.class.create!(new_version_attrs(new_attrs))
  end
end

#deprecate_old_versions_after_create!Object

Ensure that old versions are deprecated when we save (only applies on create)



87
88
89
# File 'lib/versioned_record.rb', line 87

def deprecate_old_versions_after_create!
  @deprecate_old_versions_after_create = true
end

#idObject



24
25
26
# File 'lib/versioned_record.rb', line 24

def id
  id_with_version[0]
end

#versionsObject

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

Examples:

Versions ordered by version number


person.versions.order(:version)


81
82
83
# File 'lib/versioned_record.rb', line 81

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