Episodes
raimon said over 4 years ago on In-depth Look into ActiveStorage :

Hi Dave,


Excellent post. I just have two questions regarding ActiveStorage:

- How can we manipulate (add prefixes, for example) the filename that has to be uploaded?

- How would you approach an scenario where you need to mark the attachments? I amb thinking, for example, in the case where you want to set some images as banned. As we cannot manipulate ActiveStorage tables, it looks very tricky to imagine a solution. maybe a proxy table? with your example, it would be something like "users" -> "files" -> "attachments". So you can always query for the "files" filtered and get the proper "attachments".


Thanks for the tutorial!


David Kimura PRO said over 4 years ago on In-depth Look into ActiveStorage :

For the filename, it can be handled a few different ways. Let's say you have a user model and the user model has_one_attached :image

If you're manually uploading an image then you can override the filename at the time of attaching it.

uri = URI.parse(URL_OF_IMAGE);
filename = File.basename(uri.path);
io = open(uri);
content_type = io.content_type;
user.image.attach(io: io, filename: filename, content_type: content_type);
user.save;

In this same example with a user and a image, we can get access to the image afterwards.

user.image.blob.filename
=> "whatever.png"
user.image.blob.filename = "newname.png"
user.image.blob.save
=> "newname.png"

For banning a file, I think you're heading in the right direction.

I would have a separate model for files that are uploaded for a user. Something like a User has_many Datei and Datei belongs to User.

The Datei model (german for file because I wouldn't want a model called File) would have a few attributes plus whatever else needed

user_id:integer:index
banned:boolean, default: false

The model would have something like has_one_attached :document.

So now a user can have many files (Datei) and each file has an attached document. You can flag the file as banned (or whatever else params).


raimon said over 4 years ago on In-depth Look into ActiveStorage :

Hi Dave,

Many thanks! yes, I have adjusted both solutions for my application and both worked as I needed.

Kind regards!



David Kimura PRO said over 4 years ago on In-depth Look into ActiveStorage :

Very cool!


Simon Kiteley PRO said over 4 years ago on In-depth Look into ActiveStorage :
Great video. Wonder if I am the only person who thinks a summernote uploading images, and activestorage video would be incredibly useful :)

Simon Kiteley PRO said over 4 years ago on In-depth Look into ActiveStorage :
Your current summernote video javascript does not need changes (except to match the different names I used for my models). Great work.

marklar said over 4 years ago on In-depth Look into ActiveStorage :
Do you know if it's possible (or advisable) to use ActiveStorage with Rails 5.1.6? In the Rails guides it states: > After upgrading your application to Rails 5.2 http://guides.rubyonrails.org/active_storage_overview.html#setup So I wonder whether it would be ill advised to add ActiveStorage as a gem to 5.1.6.

David Kimura PRO said over 4 years ago on In-depth Look into ActiveStorage :
I haven't tried adding it to an "older" version of rails. It does seem to be fairly decoupled from Rails as its own gem. Though, one weird thing that they do which is a little strange that could prevent this from easily working is in the ActiveStorage gemspec. ```ruby s.add_dependency "actionpack", version s.add_dependency "activerecord", version ``` It has this reference to version which is in the main branch's `RAILS_VERSION` file. If you reference a `5.1.6` tag, the ActiveStorage gem wouldn't exist. You may need to clone the repo and manually adjust it. This would inherently add a level of technical debt as you would have to migrate any bugfixes/security patches/enhancements to your branch.

marklar said over 4 years ago on In-depth Look into ActiveStorage :
Yikes, it sounds like it's not the best idea for a simple plug and play solution with 5.1.6 then. Thanks for taking the time to reply and thanks for the great video.

MARCEL COMPANY said over 4 years ago on In-depth Look into ActiveStorage :
Thanks a lot for this video. Always great. I'm fighting to get drag'n drop working, did you tried as well ? http://edgeguides.rubyonrails.org/active_storage_overview.html#integrating-with-libraries-or-frameworks

Login to Comment