Module: Logidze::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/logidze/model.rb

Overview

Extends model with methods to browse history

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

TIME_FACTOR =

Use this to convert Ruby time to milliseconds

1_000

Instance Method Summary collapse

Instance Method Details

#at(ts) ⇒ Object

Return a dirty copy of record at specified time If time is less then the first version, then return nil. If time is greater then the last version, then return self.



44
45
46
47
48
49
50
51
52
53
# File 'lib/logidze/model.rb', line 44

def at(ts)
  ts = parse_time(ts)
  return nil unless log_data.exists_ts?(ts)
  return self if log_data.current_ts?(ts)

  version = log_data.find_by_time(ts).version

  object_at = dup
  object_at.apply_diff(version, log_data.changes_to(version: version))
end

#at!(ts) ⇒ Object

Revert record to the version at specified time (without saving to DB)



56
57
58
59
60
61
62
63
64
# File 'lib/logidze/model.rb', line 56

def at!(ts)
  ts = parse_time(ts)
  return self if log_data.current_ts?(ts)
  return false unless log_data.exists_ts?(ts)

  version = log_data.find_by_time(ts).version

  apply_diff(version, log_data.changes_to(version: version))
end

#at_version(version) ⇒ Object

Return a dirty copy of specified version of record



67
68
69
70
71
72
73
# File 'lib/logidze/model.rb', line 67

def at_version(version)
  return self if log_data.version == version
  return nil unless log_data.find_by_version(version)

  object_at = dup
  object_at.apply_diff(version, log_data.changes_to(version: version))
end

#at_version!(version) ⇒ Object

Revert record to the specified version (without saving to DB)



76
77
78
79
80
81
# File 'lib/logidze/model.rb', line 76

def at_version!(version)
  return self if log_data.version == version
  return false unless log_data.find_by_version(version)

  apply_diff(version, log_data.changes_to(version: version))
end

#diff_from(ts) ⇒ Object

Return diff object representing changes since specified time.

Examples:


post.diff_from(2.days.ago)
#=> { "id" => 1, "changes" => { "title" => { "old" => "Hello!", "new" => "World" } } }


89
90
91
92
# File 'lib/logidze/model.rb', line 89

def diff_from(ts)
  ts = parse_time(ts)
  { "id" => id, "changes" => log_data.diff_from(time: ts) }
end

#redo!Object

Restore record to the future version (if ‘undo!` was applied) Return false if no future version found, otherwise return updated record.



104
105
106
107
108
# File 'lib/logidze/model.rb', line 104

def redo!
  version = log_data.next_version
  return false if version.nil?
  switch_to!(version.version)
end

#switch_to!(version) ⇒ Object

Restore record to the specified version. Return false if version is unknown.



112
113
114
115
# File 'lib/logidze/model.rb', line 112

def switch_to!(version)
  return false unless at_version!(version)
  self.class.without_logging { save! }
end

#undo!Object

Restore record to the previous version. Return false if no previous version found, otherwise return updated record.



96
97
98
99
100
# File 'lib/logidze/model.rb', line 96

def undo!
  version = log_data.previous_version
  return false if version.nil?
  switch_to!(version.version)
end