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.
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 |
Instance Method Details
#at(datetime) ⇒ Object
Returns the version
at the supplied datetime
or time
, or self
if there is none.
78 79 80 81 82 |
# File 'lib/hoardable/source_model.rb', line 78 def at(datetime) return self if datetime.nil? || !created_at version_at(datetime) || (self if created_at < datetime) end |
#hoardable_id ⇒ Object
104 105 106 |
# File 'lib/hoardable/source_model.rb', line 104 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.
98 99 100 101 102 |
# File 'lib/hoardable/source_model.rb', line 98 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.
63 64 65 |
# File 'lib/hoardable/source_model.rb', line 63 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.
71 72 73 |
# File 'lib/hoardable/source_model.rb', line 71 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.
88 89 90 91 92 |
# File 'lib/hoardable/source_model.rb', line 88 def version_at(datetime) raise(Error, "Future state cannot be known") if datetime.future? versions.at(datetime).limit(1).first end |