Module: FileBlobs::ActiveSupportTestFixtures

Defined in:
lib/file_blobs_rails/active_support_test_extensions.rb

Overview

Module mixed into ActiveRecord::FixtureSet.

Instance Method Summary collapse

Instance Method Details

#file_blob_data(path) ⇒ String

The contents of a blob.

Parameters:

  • path (String)

    the path of the file whose contents is used in the fixture, relative to the Rails application’s test/fixtures directory

Returns:

  • (String)

    the blob contents



14
15
16
17
# File 'lib/file_blobs_rails/active_support_test_extensions.rb', line 14

def file_blob_data(path)
  file_path = Rails.root.join('test/fixtures'.freeze).join(path)
  File.binread file_path
end

#file_blob_id(path) ⇒ String

Computes the ID assigned to a blob.

Parameters:

  • path (String)

    the path of the file whose contents is used in the fixture, relative to the Rails application’s test/fixtures directory

Returns:

  • (String)

    the ID used to represent the blob contents



24
25
26
27
# File 'lib/file_blobs_rails/active_support_test_extensions.rb', line 24

def file_blob_id(path)
  # This needs to be kept in sync with blob_model.rb.
  Base64.urlsafe_encode64(Digest::SHA256.digest(file_blob_data(path)))
end

#file_blob_size(path) ⇒ String

The size of a blob.

Parameters:

  • path (String)

    the path of the file whose contents is used in the fixture, relative to the Rails application’s test/fixtures directory

Returns:

  • (String)

    the blob contents



34
35
36
37
# File 'lib/file_blobs_rails/active_support_test_extensions.rb', line 34

def file_blob_size(path)
  file_path = Rails.root.join('test/fixtures'.freeze).join(path)
  File.stat(file_path).size
end