Module: FileBlobs::ActiveRecordFixtureSetExtensions

Defined in:
lib/file_blobs_rails/active_record_fixture_set_extensions.rb

Overview

Module mixed into ActiveRecord::FixtureSet.

Instance Method Summary collapse

Instance Method Details

#file_blob_data(path, options = {}) ⇒ String

The contents of a blob, in a YAML-friendly format.

Parameters:

  • path (String)

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

  • options (Hash) (defaults to: {})

    optionally specify the current indentation level

Options Hash (options):

  • indent (Integer)

    the number of spaces that the current line in the YAML fixture file is indented by

Returns:

  • (String)

    the base64-encoded blob contents



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/file_blobs_rails/active_record_fixture_set_extensions.rb', line 30

def file_blob_data(path, options = {})
  # The line with base64 data must be indented further than the current line.
  indent = ' ' * ((options[:indent] || 2) + 2)

  file_path = Rails.root.join('test/fixtures'.freeze).join(path)
  blob_contents = File.binread file_path
  base64_data = Base64.encode64 blob_contents
  base64_data.gsub! "\n", "\n#{indent}"
  base64_data.strip!

  "!!binary |\n#{indent}#{base64_data}"
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



14
15
16
17
18
19
20
# File 'lib/file_blobs_rails/active_record_fixture_set_extensions.rb', line 14

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

  # This needs to be kept in sync with blob_model.rb.
  Base64.urlsafe_encode64(Digest::SHA256.digest(blob_contents)).inspect
end

#file_blob_size(path) ⇒ Integer

The number of bytes in a file.

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:

  • (Integer)

    the nubmer of bytes in the file



48
49
50
51
# File 'lib/file_blobs_rails/active_record_fixture_set_extensions.rb', line 48

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