# frozen_string_literal: true
# Active Storage patch for application wide transformations
# The main purpose of the code below is DRY.
# We don't want to repeat everywhere `User.avatar.variant(many_transformation_options)`.
# Instead of this we simply write `User.avatar.variant(:medium)`
# And if one day when we decide to change any size of variants, then we change it in one place.
#
# Waiting for https://github.com/rails/rails/pull/35290
ActiveStorage.extend(Module.new do
def transformations
return @transformations if defined?(@transformations)
files = [
Rails.root.join("config", "transformations.yml"),
# fallback to default config
Pathname.new(File.join(__dir__, "transformations.yml"))
]
file = files.detect(&:exist?)
@transformations = YAML.load_file(file).deep_symbolize_keys!
raise ArgumentError, "Variant called :medium is missing in #{file}" unless
@transformations.key?(:medium)
@transformations
end
end)
# Original source at https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/transformers/transformer.rb
ActiveStorage::Transformers::Transformer.prepend(Module.new do
def transformations
ActiveStorage.transformations.fetch(@transformations) { super }
end
end)
# "Fix" this change https://github.com/rails/rails/commit/6be1446fc7f4b159097533562920662b85155113
ActiveSupport.on_load(:active_storage_blob) do
ActiveStorage::Variation.prepend(Module.new do
def initialize(transformations)
return @transformations = transformations if transformations.is_a?(Symbol)
super
end
end)
end
active storage patch.rb
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 原文 大家好,欢迎回到学习:)。在本文中,我将继续讨论Apache Spark的深度学习。你可以在这里看到第一部分...
- 原文链接:https://yq.aliyun.com/articles/178374 0. 简介 在过去,我写的主...
- 人生不在于你做多少事,把事情做到极致离开课程26天今天静下来反思自己的成长,有进步,有退缩,有原地踏步同时自己在很...