Class: Historical::Models::ModelVersion::Diff

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

Overview

Contains the differences between the current and the previous version.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Historical::MongoMapperEnhancements

belongs_to_active_record

Class Method Details

.from_creation(to) ⇒ Object

Generates a creation diff



59
60
61
# File 'lib/historical/models/model_version/diff.rb', line 59

def self.from_creation(to)
  generate_from_version(to)
end

.from_versions(from, to) ⇒ Object

Generates a diff from two versions.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/historical/models/model_version/diff.rb', line 35

def self.from_versions(from, to)
  return from_creation(to) if !from

  generate_from_version(from, 'update').tap do |d|
    from.record.attribute_names.each do |attr_name|
      attr = attr_name.to_sym
      next if Historical::IGNORED_ATTRIBUTES.include? attr
    
      old_value, new_value = from[attr], to[attr]
   
      Historical::Models::AttributeDiff.specialized_for(d, attr).new.tap do |ad|
        ad.attribute_type = Historical::Models::AttributeDiff.detect_attribute_type(d, attr)
        ad.parent = d
        ad.old_value = old_value
        ad.new_value = new_value
        ad.attribute = attr.to_s
        d.changes << ad
      end if old_value != new_value
    end
  end
end

Instance Method Details

#changesArray<Historical::Models::AttributeDiff>



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

many  :changes,     :class_name => "Historical::Models::AttributeDiff"

#new_versionObject

The version after the diff was applied



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

def new_version
  @parent || _parent_document
end

#old_versionObject

The version before the diff was applied



29
30
31
# File 'lib/historical/models/model_version/diff.rb', line 29

def old_version
  new_version.previous
end

#recordObject

The record the diff was applied on



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

def record
  new_version.record
end