module Shrine::Plugins::MetadataAttributes

  1. lib/shrine/plugins/metadata_attributes.rb

The metadata_attributes plugin allows you to sync attachment metadata to additional record attributes.

plugin :metadata_values

It provides Attacher.metadata_attributes method which allows you to specify mappings between metadata fields on the attachment and attribute names on the record.

Attacher.metadata_attributes :size => :size, :mime_type => :type

The above configuration will sync size metadata field to <attachment>_size record attribute, and mime_type metadata field to <attachment>_type record attribute.

user.avatar = image
user.avatar.metadata["size"]      #=> 95724
user.avatar_size                  #=> 95724
user.avatar.metadata["mime_type"] #=> "image/jpeg"
user.avatar_type                  #=> "image/jpeg"

user.avatar = nil
user.avatar_size #=> nil
user.avatar_type #=> nil

If any corresponding metadata attribute doesn't exist on the record, that metadata sync will be silently skipped.

Methods

Public Class

  1. configure

Public Class methods

configure (uploader)
[show source]
# File lib/shrine/plugins/metadata_attributes.rb, line 31
def self.configure(uploader)
  uploader.opts[:metadata_attributes_mappings] ||= {}
end