Hello all -
Firstly, huge thanks to those who have worked on ActiveStorage so far. The library seems to be coming along nicely.
ActiveStorage currently allows you to define variants of an attachment as follows:
class User < ActiveRecord::Base
has_one_attached :avatar
# ...
end
user.avatar.variant(resize: "100x100>")
user.avatar.variant(resize: "100x100>", caption: "foo")
user.avatar.variant(resize: "200x200", rotate: "-90")
I’d like to propose the following functionality that lets users configure and pre-define variants.
class User < ActiveRecord::Base
has_one_attached(
:avatar,
variants: {
small: { resize: "100x100>" }
small_captioned: { resize: "100x100>", caption: "foo" }
medium_rotated: { resize: "200x200", rotate: "-90" }
}
)
# ...
end
user.avatar.variant(:small)
user.avatar.variant(:small_captioned)
user.avatar.variant(:medium_rotated)
# Something not pre-definied
user.avatar.variant(rotate: "120")
This is similar in concept to how existing attachment libraries (paperclip, carrierwave, etc…) have allowed definition and configuration of variants.
It is true that this functionality can be mimicked outside of activestorage by having the developer maintain a manual mapping of key names to variant configurations. However, I believe this should be part of ActiveStorage directly because -
- It leads to cleaner/more readable code (e.g.
user.avatar.variant(:small)
is easy to understand) - It keeps configuration consolidated inline with
has_one_attached
, which is similar to how options are already defined inline withhas_one
,has_many
, etc… - It’s fully backward compatible with how variants are invoked right now and doesn’t force you to use a particular approach.
Would such a feature be accepted if I were to submit a pull request for it?
Thank you!