Class: Historical::ModelHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/historical/model_history.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ ModelHistory

Returns a new instance of ModelHistory.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/historical/model_history.rb', line 5

def initialize(record)
  @record = record
  
  if record.historical_version
    @base_version = record.historical_version
  elsif record.new_record?
    @base_version = -1
  else
    version_count = versions.count
    
    if version_count.zero?
      spawn_creation!
      version_count = 1
    end
    
    @base_version = version_count - 1
  end
end

Instance Attribute Details

#base_versionObject (readonly)

Returns the value of attribute base_version.



3
4
5
# File 'lib/historical/model_history.rb', line 3

def base_version
  @base_version
end

#recordObject (readonly)

Returns the value of attribute record.



3
4
5
# File 'lib/historical/model_history.rb', line 3

def record
  @record
end

Instance Method Details

#creationObject



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

def creation
  versions.where("diff.diff_type" => "creation").first
end

#destroyObject



24
25
26
27
# File 'lib/historical/model_history.rb', line 24

def destroy
  versions.remove
  record.invalidate_history! if record.history == self
end

#find_version(position) ⇒ Object



65
66
67
# File 'lib/historical/model_history.rb', line 65

def find_version(position)
  versions.skip(position).limit(1).first
end

#latest_versionObject



49
50
51
# File 'lib/historical/model_history.rb', line 49

def latest_version
  versions.last
end

#next_versionObject



45
46
47
# File 'lib/historical/model_history.rb', line 45

def next_version
  own_version.next
end

#original_versionObject



53
54
55
# File 'lib/historical/model_history.rb', line 53

def original_version
  versions.first
end

#own_versionObject



35
36
37
38
39
# File 'lib/historical/model_history.rb', line 35

def own_version
  versions.skip(@base_version).limit(1).first.tap do |own|
    raise "couldn't find myself (base_version: #{@base_version}, versions: #{versions.count})" unless own
  end
end

#previous_versionObject



41
42
43
# File 'lib/historical/model_history.rb', line 41

def previous_version
  own_version.previous
end

#restore(query) ⇒ Object

Raises:

  • (::ActiveRecord::RecordNotFound)


79
80
81
82
83
# File 'lib/historical/model_history.rb', line 79

def restore(query)
  version = version_by_query(query)
  raise ::ActiveRecord::RecordNotFound, "version (base_version: #{base_version}, query: #{query}) does not exist" unless version
  version.restore(record)
end

#updatesObject



61
62
63
# File 'lib/historical/model_history.rb', line 61

def updates
  versions.where("diff.diff_type" => "update")
end

#version_by_query(query) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/historical/model_history.rb', line 69

def version_by_query(query)
  case query
  when Numeric                then find_version(query)
  when Symbol                 then send(query)
  when Models::ModelVersion   then query
  else
    nil
  end
end

#versionsObject



29
30
31
# File 'lib/historical/model_history.rb', line 29

def versions
  Models::ModelVersion.for_record(record).sort(:"meta.created_at".asc, :_id.asc)
end