luckyframework / luckyframework/avram

ActiveStorage alternative?

Open
#156 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Crystal
Stars
183
Forks
67
PR merge metrics
No merged PRs in 30d

Description

It would be very cool if Avram could have, either built in or as a separate shard, a good alternative to ActiveStorage. I did a little playing around and thought this would make a good API for it:

**config/storage.cr**
```crystal
require "avram/storage/local"
require "avram/storage/s3"

# Here is where you configure how Avram stores files
Avram::Storage.configure do |storage|
# Store files on a local hard disk
disk_adapter = Avram::Storage::DiskAdapter.new(root: "/path/to/root")\
storage.attach_adapter(:local, disk_adapter)

# Store files remotely using Amazon S3
s3_adapter = Avram::Storage::S3Adapter.new(
access_key_id: "",
secret_key: "",
region: "",
bucket: ""
)
storage.attach_adapter(:s3, s3_adapter)

# Set the default adapater based on the environment.
if Lucky::Env.production?
storage.set_default_adapter(:s3)
else
storage.set_default_adapter(:local)
end
end
```

**src/models/user.cr**
```crystal
class User < BaseModel
include Avram::Attachable
include Authentic::PasswordAuthenticatable

table :users do
column email : String
column encrypted_password : String

# One-to-one attachment
has_one_attached :avatar

# Set an adapter explicitly
has_one_attached :avatar, adapter: :s3

# One-to-many attachment
has_many_attached :avatars
end
end
```

As for how to implement it we could probably do something similar to what ActiveStorage does on the back end and just create a polymorphic join table called `avram_attachments` or something that references the name of the class and the path the file is stored at (relative to the adapter). The module `Avram::Attachable` could keep track of the classes that contain attachments.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposed config/storage.cr and src/models/user.cr examples, then review Avram's existing model and migration conventions. The scope needs agreement around local and S3 adapters, attachment associations, and the polymorphic avram_attachments table before implementation can be considered complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, crystal
Domain
backend, cloud, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.