Class: Historical::ModelHistory
- Inherits:
-
Object
- Object
- Historical::ModelHistory
- Defined in:
- lib/historical/model_history.rb
Instance Attribute Summary collapse
-
#base_version ⇒ Object
readonly
Returns the value of attribute base_version.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #creation ⇒ Object
- #destroy ⇒ Object
- #find_version(position) ⇒ Object
-
#initialize(record) ⇒ ModelHistory
constructor
A new instance of ModelHistory.
- #latest_version ⇒ Object
- #next_version ⇒ Object
- #original_version ⇒ Object
- #own_version ⇒ Object
- #previous_version ⇒ Object
- #restore(query) ⇒ Object
- #updates ⇒ Object
- #version_by_query(query) ⇒ Object
- #versions ⇒ Object
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_version ⇒ Object (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 |
#record ⇒ Object (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
#creation ⇒ Object
57 58 59 |
# File 'lib/historical/model_history.rb', line 57 def creation versions.where("diff.diff_type" => "creation").first end |
#destroy ⇒ Object
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_version ⇒ Object
49 50 51 |
# File 'lib/historical/model_history.rb', line 49 def latest_version versions.last end |
#next_version ⇒ Object
45 46 47 |
# File 'lib/historical/model_history.rb', line 45 def next_version own_version.next end |
#original_version ⇒ Object
53 54 55 |
# File 'lib/historical/model_history.rb', line 53 def original_version versions.first end |
#own_version ⇒ Object
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_version ⇒ Object
41 42 43 |
# File 'lib/historical/model_history.rb', line 41 def previous_version own_version.previous end |
#restore(query) ⇒ Object
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 |
#updates ⇒ Object
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 |
#versions ⇒ Object
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 |