Module: FileBlobs::ActiveRecordTableDefinitionExtensions

Defined in:
lib/file_blobs_rails/active_record_table_definition_extensions.rb

Overview

Module mixed into ActiveRecord::ConnectionAdapters::TableDefinition.

Instance Method Summary collapse

Instance Method Details

#file_blob(column_name_base = :file, options = {}, &block) ⇒ Object

Creates the columns used to reference a file blob

Parameters:

  • column_name_base (Symbol) (defaults to: :file)

    the prefix used to generate column names; this should match the attribute name given to has_file_blob

  • options (Hash<Symbol, Object>) (defaults to: {})

Options Hash (options):

  • null (Boolean)

    true

  • mime_type_limit (Integer)

    the maximum size of the column used to store the file name provided by the user’s browser

  • file_name_limit (Integer)

    the maximum size of the column used to store the file blob’s MIME type



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/file_blobs_rails/active_record_table_definition_extensions.rb', line 17

def file_blob(column_name_base = :file, options = {}, &block)
  allow_null = options[:null] || false
  mime_type_limit = options[:mime_type_limit] || 64
  file_name_limit = options[:file_name_limit] || 256

  # The index is needed for garbage-collection eligibility checks.
  string :"#{column_name_base}_blob_id", limit: 48, null: allow_null,
         index: true

  integer :"#{column_name_base}_size", null: allow_null
  string :"#{column_name_base}_mime_type", limit: mime_type_limit,
         null: allow_null
  string :"#{column_name_base}_original_name", limit: file_name_limit,
         null: allow_null
end