Created
June 5, 2020 20:08
-
-
Save estum/d06a7c084eb673634177ac8890844538 to your computer and use it in GitHub Desktop.
Shrine.rb plugin to separate storages per versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class Shrine | |
protected :_store, :_delete | |
module Plugins | |
# Separate storages per versions | |
# | |
# Usage: | |
# | |
# # This will store the "original" version to the "local" store, | |
# # when recent versions will be stored to "store", just as usual. | |
# plugin :version_storage, original: :local | |
module VersionStorage | |
def self.configure(uploader, **opts) | |
uploader.opts[:version_storage] ||= {} | |
uploader.opts[:version_storage].merge!(opts) | |
end | |
module InstanceMethods | |
protected | |
def _store(io, context) | |
if separate_key = storage_for(context) | |
self.class.new(separate_key)._store(io, context) | |
else | |
super | |
end | |
end | |
def _delete(uploaded_file, context) | |
if separate_key = storage_for(context) | |
self.class.new(separate_key)._delete(uploaded_file, context) | |
else | |
super | |
end | |
end | |
private | |
def storage_for(version: nil, **) | |
key = (version && opts[:version_storage][version]) | |
return key if key && storage_key != key | |
end | |
end | |
end | |
register_plugin(:version_storage, VersionStorage) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This solves shrinerb/shrine#254
Was tried for shrine 2.19.3, didn't tested with newer versions.