module FileBlobs::BlobModel::ClassMethods

Class methods on models that include FileBlobsRails::BlobModel.

Public Instance Methods

blob_owner_class_names!(*class_names) click to toggle source

Declares the model classes that use ‘has_file_blob`.

This list is necessary for garbage collection to work correctly. If the class list is incomplete, a blob model may be garbage-collected prematurely.

@param [Enumerable<String>] class_names the names of the model classes

that include the HasFileBlob concern, and therefore point to the
file blob model (the class that includes FileBlobsRails::BlobModel)
# File lib/file_blobs_rails/blob_model.rb, line 50
def blob_owner_class_names!(*class_names)
  # We're using map to create a new array out of the class_list parameter,
  # which can be any iterable.
  #
  # We're not particularly concerned with speed, because this is executed
  # once in production, while the application boots. Also, the class_names
  # array is expected to be small.
  @_blob_owner_class_names ||=
      class_names.map { |class_name| class_name.to_s }.freeze
end
blob_owner_classes() click to toggle source

The classes that include FileBlobsRails::HasFileBlob.

@return [Array<Class>] the model classes

# File lib/file_blobs_rails/blob_model.rb, line 64
def blob_owner_classes
  @_blob_owner_classes ||= @_blob_owner_class_names.map do |class_name|
    # Trigger ActiveSupport's autoloading behavior.
    #
    # ActiveSupport's String#constantize is pretty slow, but we're not
    # particularly concerned with speed, because this is executed once in
    # production, while the application boots. Also, the list of class
    # names is expected to be small.
    class_name.constantize
  end.freeze
end
id_for(blob_contents) click to toggle source

The URL-safe base64-encoded SHA-256 of the given data.

@param [String] blob_contents the file data to be hashed @return [String] a cryptographically strong hash of the given data

# File lib/file_blobs_rails/blob_model.rb, line 35
def id_for(blob_contents)
  # This needs to be kept in sync with
  # active_record_fixture_set_extensions.rb.
  Base64.urlsafe_encode64 Digest::SHA256.digest(blob_contents)
end