[Feature] [ActiveStorage] ActiveStorage custom storage path configuration per Model attribute

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[Feature] [ActiveStorage] ActiveStorage custom storage path configuration per Model attribute

Frantisek Rokusek
Hello Rails Team!
First of all I want to thank you for all job that you make here!!! Rails is a wonderful framework to work with.

I am creating an issue because I am not sure I have understood all functionalities of ActiveStorage. My pull request is not quite ready...

Here is my issue: **ActiveStorage custom storage path configuration per Model attribute**

Here is our business issue: *We use the [apartment gem](https://github.com/influitive/apartment) to split our datas on a client basis and we want to make the same with uploaded files and store them in separate folders.*

**thoughtbot** has made a wonderfull [paperclip gem](https://github.com/thoughtbot/paperclip), but since it is deprecated we are in the process of switching to ActiveStorage all our attachments management.

In Paperclip there is a fine feature where you can choose a [specific storage path](https://github.com/thoughtbot/paperclip) for each of the model attachable attribute and interpolate values in it as follows:

# app/models/user.rb

# in Paperclip
has_attached_file :avatar,
                  path: ':tenant/users/:user_id/:hash/:filename'

### Expected behaviour
Being able to configure a custom path.
The idea is to make this possible in ActiveStorage too and have something like that:

# app/models/user.rb

# in future ActiveStorage
has_one_attached :avatar,
                 path: ':tenant/users/:id/:unique_secure_token'

### Our current solution
For the moment we have made a *monkey_patching* to implement those storage path by changing the `ActiveStorage::Blob#key`.

# app/models/user.rb

def document=(attachable)

def attachable_storage_path

# app/models/application_record.rb

def attachable_storage_path
  raise NotImplementedError

def create_blob(attachable)
  return if attachable.nil?

  ActiveStorage::Blob.new.tap do |blob|
    blob.filename = attachable.original_filename
    blob.key = attachable_storage_path

### Future solution
I have began to make a PR on ActiveStorage but want to have your feedback on this feature before digging deeper in this problematic and validate the right direction!

Here it is: https://github.com/frantisekrokusekpa/rails/pull/1/files

Thank you for your feedback and advices!

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/3ec91c68-2216-4a57-ac4c-39ddde11f1e2%40googlegroups.com.