Class: Historical::Models::ModelVersion

Inherits:
Object
  • Object
show all
Extended by:
Historical::MongoMapperEnhancements
Includes:
MongoMapper::Document
Defined in:
lib/historical/models/model_version.rb,
lib/historical/models/model_version/diff.rb,
lib/historical/models/model_version/meta.rb

Overview

A complete snapshot of a model.

Defined Under Namespace

Classes: Diff, Meta

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Historical::MongoMapperEnhancements

belongs_to_active_record

Class Method Details

.for_class(source_class) ⇒ Object

Retrieve customized class definition for a record class (e.g. TopicVersion, MessageVersion)



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/historical/models/model_version.rb', line 102

def self.for_class(source_class)
  Historical::Models::Pool.pooled(Historical::Models::Pool.pooled_name(source_class, self)) do
    Class.new(self).tap do |cls|
      source_class.columns.each do |col|
        next if Historical::IGNORED_ATTRIBUTES.include? col.name.to_sym
        type = Historical::ActiveRecord.sql_to_type(col.type)
        cls.send :key, col.name, type.constantize
      end
    end
  end
end

.for_record(record_or_id, type = nil) ⇒ Object

Return all versions for the provided record

Parameters:

  • record_or_id (Record, Integer)

    The id of the record (or a record - then type can be left nil)

  • type (String) (defaults to: nil)

    The type of the record



93
94
95
96
97
98
99
# File 'lib/historical/models/model_version.rb', line 93

def self.for_record(record_or_id, type = nil)
  if type
    ModelVersion.where(:_record_id => record_or_id, :_record_type => type)
  else
    ModelVersion.where(:_record_id => record_or_id.id, :_record_type => record_or_id.class.name)
  end
end

Instance Method Details

#diffDiff

The diff between the current and the previous version (if exists)

Returns:



21
# File 'lib/historical/models/model_version.rb', line 21

one :diff, :class_name => "Historical::Models::ModelVersion::Diff"

#metaMeta

The meta-data associated with the diff (i.e. this version)

Returns:



25
# File 'lib/historical/models/model_version.rb', line 25

one :meta, :class_name => "Historical::Models::ModelVersion::Meta"

#nextObject

The immediate successor version



52
53
54
# File 'lib/historical/models/model_version.rb', line 52

def next
  next_versions.first
end

#next_versionsObject

All the next versions (immediate successor first)



47
48
49
# File 'lib/historical/models/model_version.rb', line 47

def next_versions
  siblings.where(:"meta.created_at".gte => created_at, :_id.gt => _id).sort(:"meta.created_at".asc, :_id.asc)
end

#previousObject

The immediate predecessor version



42
43
44
# File 'lib/historical/models/model_version.rb', line 42

def previous
  previous_versions.first
end

#previous_versionsObject

All the previous versions (immediate predecessor first)



37
38
39
# File 'lib/historical/models/model_version.rb', line 37

def previous_versions
  (new? ? siblings : siblings.where(:"meta.created_at".lte => created_at, :_id.lt => _id)).sort(:"meta.created_at".desc, :_id.desc)
end

#restore(base = nil) ⇒ Object

Restores the current version.

Parameters:

  • base (defaults to: nil)

    A reference to the record (to reduce DB queries)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/historical/models/model_version.rb', line 63

def restore(base = nil)
  base ||= record
  
  base.clone.tap do |r|
    r.class.columns.each do |c|
      attr = c.name.to_sym
      next if Historical::IGNORED_ATTRIBUTES.include? attr
      
      r[attr] = send(attr)
    end
    
    r.id = base.id
    r.historical_version = version_index
    r.clear_association_cache
    r.invalidate_history!
  end
end

#restore_with_protection(*args) ⇒ Object

Prevents the restored version from being saved or modifed.



82
83
84
85
86
# File 'lib/historical/models/model_version.rb', line 82

def restore_with_protection(*args)
  restore_without_protection(*args).tap do |r|
    r.readonly!
  end
end

#siblingsObject

All other versions of the associated record



32
33
34
# File 'lib/historical/models/model_version.rb', line 32

def siblings
  self.class.for_record(_record_id, _record_type)
end

#version_indexObject

The current version index (zero-based)



57
58
59
# File 'lib/historical/models/model_version.rb', line 57

def version_index
  previous_versions.count
end