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

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

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)


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_idObject



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.

Parameters:

  • datetime (DateTime, Time)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Parameters:

  • datetime (DateTime, Time)

Raises:



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