Module: PresignedUpload::Models

Defined in:
lib/presigned_upload/models.rb

Overview

Module extended by ActiveRecord::Base to allow configuring models with resources of the gem

Instance Method Summary collapse

Instance Method Details

#presigned_uploadableObject

Calling this method in the context of the model class includes Uploadable module, which makes available the methods for requesting presigned_urls and deleting file from the cloud storage

@example:

UploadLink < ApplicationRecord
  presigned_uploadable
end

upload_link = UploadLink.new
upload_link.presigned_url('key', :get, expires_in = 3600)
upload_link.delete_file('key')


24
25
26
# File 'lib/presigned_upload/models.rb', line 24

def presigned_uploadable
  include Uploadable
end

#presigned_uploadable_model(options = {}) ⇒ Object

Calling this method in the context of the model class includes Uploadable::Model module, which includes all the behavior from the Uploadable module and the Uploadable::Model, including validations and callbacks

Examples:


UploadLink < ApplicationRecord
  presigned_uploadable_model, store_dir: :generate_store_dir
end

Parameters:

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

    Options hash

Options Hash (options):

  • :store_dir (Symbol)

    The path to the storage location directory Accepts a Symbol value, which will try to call a method with same name in model, a String value representing the store directory or a Proc to call



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/presigned_upload/models.rb', line 44

def presigned_uploadable_model(options = {})
  include Uploadable::Model

  store_dir = options[:store_dir]

  define_method :store_dir do
    case store_dir
    when Symbol
      __send__(store_dir)
    when String
      store_dir
    when Proc
      instance_exec(&store_dir)
    else
      "uploads/#{model_name.plural}/#{id}"
    end
  end
end