Module: FileBlobs::BlobModel

Extended by:
ActiveSupport::Concern
Defined in:
lib/file_blobs_rails/blob_model.rb

Overview

Included in the model that stores file data.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#eligible_for_garbage_collection?Boolean

Checks if this blob can be garbage-collected.

This check’s result can become invalid after another Blob-owning model is created. To prevent data races, the check and its corresponding garbage collection must be done in the same database transaction.

Returns:

  • (Boolean)

    true if this blob is not referenced by any Blob-owning model, and thus is eligible for garbage collection



99
100
101
102
103
# File 'lib/file_blobs_rails/blob_model.rb', line 99

def eligible_for_garbage_collection?
  self.class.blob_owner_classes.all? do |klass|
    klass.file_blob_eligible_for_garbage_collection? self
  end
end

#maybe_garbage_collectBoolean

Garbage-collects this blob if it is not referenced by any other model.

Returns:

  • (Boolean)

    true if the blob was garbage-collected



80
81
82
83
84
85
86
87
88
89
# File 'lib/file_blobs_rails/blob_model.rb', line 80

def maybe_garbage_collect
  self.class.transaction do
    if eligible_for_garbage_collection?
      destroy
      true
    else
      false
    end
  end
end