Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ActiveStorage] Add ability to use pre-defined variants #39135

Merged
merged 3 commits into from
Jan 29, 2021

Conversation

fatkodima
Copy link
Member

@fatkodima fatkodima commented May 3, 2020

Related discussion

 class User < ActiveRecord::Base
   has_one_attached :avatar do |attachable|
     attachable.variant :thumb, resize: "100x100"
     attachable.variant :medium, resize: "300x300", monochrome: true
   end
 end

 class Gallery < ActiveRecord::Base
   has_many_attached :photos do |attachable|
     attachable.variant :thumb, resize: "100x100"
     attachable.variant :medium, resize: "300x300", monochrome: true
   end
 end

 <%= image_tag user.avatar.variant(:thumb) %>

@kaspth
Copy link
Contributor

kaspth commented May 4, 2020

Let's yield an actual object (yes, no instance_eval either) that you can call variant on:

class User < ActiveRecord::Base
  has_one_attached :avatar do |blob|
    blob.variant  :thumb, resize: "100x100"
    blob.variant :medium, resize: "300x300", monochrome: true
  end
end

class Gallery < ActiveRecord::Base
  has_many_attached :photos do |blob|
    blob.variant  :thumb, resize: "100x100"
    blob.variant :medium, resize: "300x300", monochrome: true
  end
end

Note: there's a potential reading of this API version that makes it look like we're eagerly computing the variants, so whatever ideas you'd have to clarify that are welcome. (Perhaps renaming the yielded variable to attachable in docs would do it?)

@fatkodima fatkodima force-pushed the active_storage-named-variants branch from 2b4a231 to a1d2460 Compare May 4, 2020 10:05
@fatkodima
Copy link
Member Author

@kaspth Updated using the block syntax.

@rails-bot
Copy link

rails-bot bot commented Aug 2, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Aug 2, 2020
@sedubois
Copy link
Contributor

sedubois commented Aug 2, 2020

I’d be interested in such a feature.

@rails-bot rails-bot bot removed the stale label Aug 2, 2020
@fatkodima
Copy link
Member Author

@georgeclaghorn @kaspth
Any chance to get this into the next release?

@mshibuya
Copy link

I definitely need this feature, would be great if this can be a part of the next release.

@guillaumebriday
Copy link
Member

Hey guys, That would be awesome to see this feature merged!

How could we help @fatkodima @rafaelfranca?

🙏

Base automatically changed from master to main January 14, 2021 17:01
@kaspth kaspth merged commit 65979c6 into rails:main Jan 29, 2021
@kaspth
Copy link
Contributor

kaspth commented Jan 29, 2021

@fatkodima nice job! Sorry for the delay on my end 🙏

@yfxie
Copy link
Contributor

yfxie commented Nov 12, 2021

@fatkodima Thanks for your contribution. I referred to this PR and created a gem supporting Rails 6 projects to have this nice feature.
https://github.com/yfxie/active_storage_variantt

@SylarRuby
Copy link

Am i missing something? Im getting ArgumentError (unknown keyword: :variants)

@fatkodima
Copy link
Member Author

@SylarRuby PR description is outdated. Look at the code changes and changelog.

@jmarsh24
Copy link

jmarsh24 commented Mar 7, 2022

Am i missing something? Im getting ArgumentError (unknown keyword: :variants)

I'm also experiencing this error.

@dwightwatson
Copy link
Contributor

@jmarsh24 like above, look at the PR code and the changelog to see how to use variants.

I wonder if there's any hope of using these with attachment previews too.

@fatkodima fatkodima deleted the active_storage-named-variants branch February 14, 2023 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants