Module: Hoardable::SourceModel

Extended by:
ActiveSupport::Concern
Defined in:
lib/hoardable/source_model.rb

Overview

This concern contains the Hoardable relationships, callbacks, and API methods for an ActiveRecord. It is included by Model after the dynamic generation of the Version class variant.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hoardable_event_uuidString (readonly)

Returns A postgres UUID that represents the version’s ActiveRecord database transaction.

Returns:

  • (String)

    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_operationString (readonly)

Returns The database operation that created the version - either update or delete.

Returns:

  • (String)

    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_versionObject (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.

Parameters:

  • datetime (DateTime, Time)


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_idObject



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.

Parameters:

  • datetime (DateTime, Time)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Parameters:

  • datetime (DateTime, Time)

Raises:



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