Class: Historical::Models::ModelVersion
- Inherits:
-
Object
- Object
- Historical::Models::ModelVersion
- 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
Class Method Summary collapse
-
.for_class(source_class) ⇒ Object
Retrieve customized class definition for a record class (e.g. TopicVersion, MessageVersion).
-
.for_record(record_or_id, type = nil) ⇒ Object
Return all versions for the provided record.
Instance Method Summary collapse
-
#diff ⇒ Diff
The diff between the current and the previous version (if exists).
-
#meta ⇒ Meta
The meta-data associated with the diff (i.e. this version).
-
#next ⇒ Object
The immediate successor version.
-
#next_versions ⇒ Object
All the next versions (immediate successor first).
-
#previous ⇒ Object
The immediate predecessor version.
-
#previous_versions ⇒ Object
All the previous versions (immediate predecessor first).
-
#restore(base = nil) ⇒ Object
Restores the current version.
-
#restore_with_protection(*args) ⇒ Object
Prevents the restored version from being saved or modifed.
-
#siblings ⇒ Object
All other versions of the associated record.
-
#version_index ⇒ Object
The current version index (zero-based).
Methods included from Historical::MongoMapperEnhancements
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
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
#diff ⇒ Diff
The diff between the current and the previous version (if exists)
21 |
# File 'lib/historical/models/model_version.rb', line 21 one :diff, :class_name => "Historical::Models::ModelVersion::Diff" |
#meta ⇒ Meta
The meta-data associated with the diff (i.e. this version)
25 |
# File 'lib/historical/models/model_version.rb', line 25 one :meta, :class_name => "Historical::Models::ModelVersion::Meta" |
#next ⇒ Object
The immediate successor version
52 53 54 |
# File 'lib/historical/models/model_version.rb', line 52 def next next_versions.first end |
#next_versions ⇒ Object
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 |
#previous ⇒ Object
The immediate predecessor version
42 43 44 |
# File 'lib/historical/models/model_version.rb', line 42 def previous previous_versions.first end |
#previous_versions ⇒ Object
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.
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 |
#siblings ⇒ Object
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_index ⇒ Object
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 |