/active_storage_variant

Use the pre-defined variants feature in Rails 5 projects.

Primary LanguageRubyMIT LicenseMIT

ActiveStorageVariant

Build Status Maintainability codecov Gem Version

The pre-defined variants is a very convenient feature. It's something like the styles option in Paperclip gem. Without it, it is a pain to use ActiveStorage as an alternative to Paperclip. Thanks to @fatkodima for the contribution.

The cool feature is introduced in Rails 7. By using this gem, you can take the feature in advance for projects using Rails 6. All configurations are the same, which means when you upgrade to Rails 7 in the future, you can remove this plugin without any effect.

Installation

gem "active_storage_variant"

Usage

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) %>

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/yfxie/active_storage_variant.

License

MIT