Active Storage 变体¶ ↑
一组可以应用于 blob 以创建变体(variant)的转换。此类通过 ActiveStorage::Blob#variant 方法暴露,通常应避免直接使用。
如果您确实需要直接使用它,它会使用一个转换的哈希进行实例化,其中键是命令,值是参数。示例:
ActiveStorage::Variation.new(resize_to_limit: [100, 100], colourspace: "b-w", rotate: "-90", saver: { trim: true })
选项直接映射到 ImageProcessing 命令。
方法
- C
- D
- E
- F
- K
- N
- T
- W
Attributes
| [R] | transformations |
类公共方法
decode(key) 链接
返回一个 Variation 实例,包含由 encode 编码的转换。
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 35 def decode(key) new ActiveStorage.verifier.verify(key, purpose: :variation) end
encode(transformations) 链接
为 transformations 返回一个签名后的密钥,可用于在 URL 或组合密钥(如 ActiveStorage::Variant#key)中引用特定的变体。
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 41 def encode(transformations) ActiveStorage.verifier.generate(transformations, purpose: :variation) end
new(transformations) 链接
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 46 def initialize(transformations) @transformations = transformations.deep_symbolize_keys end
wrap(variator) 链接
根据给定的 variator 返回一个 Variation 实例。如果 variator 是 Variation,则直接返回。如果它是 String,则传递给 ActiveStorage::Variation.decode。否则,假定它是一个转换 Hash 并直接传递给构造函数。
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 23 def wrap(variator) case variator when self variator when String decode variator else new variator end end
实例公共方法
content_type() 链接
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 70 def content_type Marcel::MimeType.for(extension: format.to_s) end
default_to(defaults) 链接
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 50 def default_to(defaults) self.class.new transformations.reverse_merge(defaults) end
digest() 链接
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 79 def digest OpenSSL::Digest::SHA1.base64digest Marshal.dump(transformations) end
format() 链接
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 62 def format transformations.fetch(:format, :png).tap do |format| if Marcel::Magic.by_extension(format.to_s).nil? raise ArgumentError, "Invalid variant format (#{format.inspect})" end end end
key() 链接
返回此变体实例化时使用的所有 transformations 的签名后密钥。
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 75 def key self.class.encode(transformations) end
transform(file, &block) 链接
接受一个 File 对象,对其执行 transformations,并将转换后的图像保存到临时文件中。
来源: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/variation.rb, line 56 def transform(file, &block) ActiveSupport::Notifications.instrument("transform.active_storage") do transformer.transform(file, format: format, &block) end end