module Latch::Avram::Model

Overview

Integrates Latch uploads with Avram models.

Include this module in your model to use the attach macro for declaring file attachments backed by Latch uploaders.

class User < BaseModel
  include Latch::Avram::Model

  table do
    attach avatar : ImageUploader::StoredFile?
  end
end

By including this module, the model's operation classes will also receive the required macros and methods to seamlessly integrate with Latch.

Defined in:

latch/avram/model.cr

Macro Summary

Macro Detail

macro attach(type_declaration) #

Registers a serializable column for an attachment. The type should be an uploader's StoredFile class.

attach avatar : ImageUploader::StoredFile
# or
attach avatar : ImageUploader::StoredFile?

It is assumed that a jsonb column exists with the same name. So in your migration, you'll need to add the column as follows:

add avatar : JSON::Any
# or
add avatar : JSON::Any?

The data of a stored file can then be accessed through the avatar method:

user.avatar.class
# => ImageUploader::StoredFile

user.avatar.url
# => "https://bucket.s3.amazonaws.com/user/1/avatar/abc123.jpg"

# for presigned URLs
user.avatar.url(expires_in: 1.hour)

[View source]