Module: Shrine::Plugins::Derivatives::AttacherClassMethods

Defined in:
lib/shrine/plugins/derivatives.rb

Instance Method Summary collapse

Instance Method Details

#derivatives_processor(name = :default, download: true, &block) ⇒ Object Also known as: derivatives

Registers a derivatives processor on the attacher class.

Attacher.derivatives_processor :thumbnails do |original|
  # ...
end

By default, Shrine will convert the source IO object into a file before it's passed to the processor block. You can set `download: false` to pass the source IO object to the processor block as is.

Attacher.derivatives_processor :thumbnails, download: false do |original|
  # ...
end

This can be useful if you'd like to defer or avoid a possibly expensive download operation for processor logic that does not require it.


65
66
67
68
69
70
71
72
73
74
# File 'lib/shrine/plugins/derivatives.rb', line 65

def derivatives_processor(name = :default, download: true, &block)
  if block
    shrine_class.derivatives_options[:processors][name.to_sym] = block
    shrine_class.derivatives_options[:processor_settings][name.to_sym] = { download: download }
  else
    shrine_class.derivatives_options[:processors].fetch(name.to_sym) do
      fail Error, "derivatives processor #{name.inspect} not registered" unless name == :default
    end
  end
end

#derivatives_processor_settings(name) ⇒ Object

Returns settings for the given derivatives processor.

Attacher.derivatives_processor_settings(:thumbnails) #=> { download: true }

80
81
82
# File 'lib/shrine/plugins/derivatives.rb', line 80

def derivatives_processor_settings(name)
  shrine_class.derivatives_options[:processor_settings][name.to_sym] || {}
end

#derivatives_storage(storage_key = nil, &block) ⇒ Object

Specifies default storage to which derivatives will be uploaded.

Attacher.derivatives_storage :other_store
# or
Attacher.derivatives_storage do |name|
  if name == :thumbnail
    :thumbnail_store
  else
    :store
  end
end

95
96
97
98
99
100
101
# File 'lib/shrine/plugins/derivatives.rb', line 95

def derivatives_storage(storage_key = nil, &block)
  if storage_key || block
    shrine_class.derivatives_options[:storage] = storage_key || block
  else
    shrine_class.derivatives_options[:storage]
  end
end