Skip to content

Instantly share code, notes, and snippets.

@estum
Created June 5, 2020 20:08
Show Gist options
  • Save estum/d06a7c084eb673634177ac8890844538 to your computer and use it in GitHub Desktop.
Save estum/d06a7c084eb673634177ac8890844538 to your computer and use it in GitHub Desktop.
Shrine.rb plugin to separate storages per versions
# 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
@estum
Copy link
Author

estum commented Jun 5, 2020

This solves shrinerb/shrine#254

Was tried for shrine 2.19.3, didn't tested with newer versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment