Module: Hoardable::SourceModel
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/hoardable/source_model.rb
Overview
Instance Attribute Summary collapse
-
#hoardable_event_uuid ⇒ String
readonly
A postgres UUID that represents the
version
’sActiveRecord
database transaction. -
#hoardable_operation ⇒ String
readonly
The database operation that created the
version
- eitherupdate
ordelete
. -
#hoardable_version ⇒ Object
readonly
The
Version
class instance for use withinversioned
,reverted
, anduntrashed
callbacks.
Class Method Summary collapse
Instance Method Summary collapse
-
#at(datetime) ⇒ Object
Returns the
version
at the supplieddatetime
ortime
, orself
if there is none. - #hoardable_id ⇒ Object
-
#revert_to!(datetime) ⇒ Object
If a version is found at the supplied datetime, it will
revert!
to it and return it. -
#trashed? ⇒ Boolean
Returns a boolean of whether the record is actually a trashed
version
cast as an instance of the source model. -
#version? ⇒ Boolean
Returns a boolean of whether the record is actually a
version
cast as an instance of the source model. -
#version_at(datetime) ⇒ Object
Returns the
version
at the supplieddatetime
ortime
.
Instance Attribute Details
#hoardable_event_uuid ⇒ String (readonly)
Returns A postgres UUID that represents the version
’s ActiveRecord
database transaction.
17 |
# File 'lib/hoardable/source_model.rb', line 17 delegate :hoardable_event_uuid, :hoardable_operation, to: :hoardable_version, allow_nil: true |
#hoardable_operation ⇒ String (readonly)
Returns The database operation that created the version
- either update
or delete
.
17 |
# File 'lib/hoardable/source_model.rb', line 17 delegate :hoardable_event_uuid, :hoardable_operation, to: :hoardable_version, allow_nil: true |
#hoardable_version ⇒ Object (readonly)
The Version
class instance for use within versioned
, reverted
, and untrashed
callbacks.
11 12 13 |
# File 'lib/hoardable/source_model.rb', line 11 def hoardable_version @hoardable_version end |
Class Method Details
.included(base) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hoardable/source_model.rb', line 49 def self.included(base) base.class_eval do # Returns all +versions+ in ascending order of their temporal timeframes. has_many( :versions, -> { order("UPPER(_during) ASC") }, dependent: nil, class_name: version_class.to_s, inverse_of: :hoardable_source, foreign_key: :hoardable_id ) end end |
Instance Method Details
#at(datetime) ⇒ Object
Returns the version
at the supplied datetime
or time
, or self
if there is none.
82 83 84 85 86 |
# File 'lib/hoardable/source_model.rb', line 82 def at(datetime) return self if datetime.nil? || !created_at version_at(datetime) || (self if created_at < datetime) end |
#hoardable_id ⇒ Object
108 109 110 |
# File 'lib/hoardable/source_model.rb', line 108 def hoardable_id read_attribute("hoardable_id") end |
#revert_to!(datetime) ⇒ Object
If a version is found at the supplied datetime, it will revert!
to it and return it. This will raise an error if you try to revert to a version in the future.
102 103 104 105 106 |
# File 'lib/hoardable/source_model.rb', line 102 def revert_to!(datetime) return unless (version = at(datetime)) version.is_a?(version_class) ? version.revert! : self end |
#trashed? ⇒ Boolean
Returns a boolean of whether the record is actually a trashed version
cast as an instance of the source model.
67 68 69 |
# File 'lib/hoardable/source_model.rb', line 67 def trashed? !self.class.exists?(self.class.primary_key => id) end |
#version? ⇒ Boolean
Returns a boolean of whether the record is actually a version
cast as an instance of the source model.
75 76 77 |
# File 'lib/hoardable/source_model.rb', line 75 def version? hoardable_id != id end |
#version_at(datetime) ⇒ Object
Returns the version
at the supplied datetime
or time
. This will raise an error if you try to find a version in the future.
92 93 94 95 96 |
# File 'lib/hoardable/source_model.rb', line 92 def version_at(datetime) raise(Error, "Future state cannot be known") if datetime.future? versions.at(datetime).limit(1).first end |