Class: VestalVersions::Version

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveSupport::Configurable, Comparable
Defined in:
lib/vestal_versions/version.rb

Overview

The ActiveRecord model representing versions.

Instance Method Summary (collapse)

Instance Method Details

- (Object) <=>(other)

In conjunction with the included Comparable module, allows comparison of version records based on their corresponding version numbers, creation timestamps and IDs.



25
26
27
# File 'lib/vestal_versions/version.rb', line 25

def <=>(other)
  [number, created_at, id].map(&:to_i) <=> [other.number, other.created_at, other.id].map(&:to_i)
end

- (Object) changes



18
19
20
# File 'lib/vestal_versions/version.rb', line 18

def changes
  self[:modifications]
end

- (Boolean) initial?

Returns whether the version has a version number of 1. Useful when deciding whether to ignore the version during reversion, as initial versions have no serialized changes attached. Helps maintain backwards compatibility.

Returns:

  • (Boolean)


32
33
34
# File 'lib/vestal_versions/version.rb', line 32

def initial?
  number == 1
end

- (Object) original_number

Returns the original version number that this version was.



37
38
39
40
41
42
43
44
# File 'lib/vestal_versions/version.rb', line 37

def original_number
  if reverted_from.nil?
    number
  else
    version = versioned.versions.at(reverted_from)
    version.nil? ? 1 : version.original_number
  end
end

- (Object) restore



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vestal_versions/version.rb', line 57

def restore
  if tag == 'deleted'
    attrs = modifications

    class_name = attrs['type'].blank? ? versioned_type : attrs['type']
    klass = class_name.constantize
    model = klass.new

    attrs.each do |k, v|
      begin
        model.send "#{k}=", v
      rescue NoMethodError
        logger.warn "Attribute #{k} does not exist on #{class_name} (Version id: #{id})." rescue nil
      end
    end

    model
  else
    latest_version = self.class.find(:first, :conditions => {:versioned_id => versioned_id, :versioned_type => versioned_type, :tag => 'deleted'})
    latest_version.nil? ? nil : latest_version.restore
  end
end

- (Object) restore!



46
47
48
49
50
51
52
53
54
55
# File 'lib/vestal_versions/version.rb', line 46

def restore!
  model = restore
  
  if model
    model.save!
    destroy
  end
  
  model
end