Sha256: d7f5526c576e656b37a78d1f18c916509c592dc0340f89677eeacafc60859220

Contents?: true

Size: 1.57 KB

Versions: 28

Compression:

Stored size: 1.57 KB

Contents

class Shrine
  module Plugins
    # Allows an uploader to have more than one 'cache' -- although the main one registered
    # as normal will ordinarily be used, you can manually assign UploadedFiles (or hashes)
    # specifying other caches, and they will be accepted, and promoted.
    #
    # Invented for use with shrine-url.
    #
    #     Shrine.storages = {
    #       cache: ...,
    #       store: ...,
    #       remote_url: Shrine::Storage::Url.new
    #      }
    #
    #     class SomeUploader < Shrine
    #       plugin :kithe_multi_cache, additional_cache: [:remote_url, :something_else]
    #       ...
    #     end
    #
    # Now in your model, you can
    #
    #     my_model.attached_file = { "id" => "http://example.com", "storage" => "remote_url"}
    #
    # And the data can be saved, and the remote url (shrine-url) file will be promoted as usual,
    # even though it's not registered as the cache storage.
    #
    module KitheMultiCache
      def self.configure(uploader, options = {})
        uploader.opts[:kithe_multi_cache_keys]  = Array(options[:additional_cache]).collect(&:to_sym)
      end

      # override #cache to lazily extend with our custom module. Kinda hacky,
      # but couldn't think of any other way to only extend the "cache" uploader,
      # and not the "store" uploader.
      module AttacherMethods
        def cached?(file = self.file)
          super || (file && shrine_class.opts[:kithe_multi_cache_keys].include?(file.storage_key.to_sym))
        end
      end
    end

    register_plugin(:kithe_multi_cache, KitheMultiCache)
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
kithe-2.16.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.15.1 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.15.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.14.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.13.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.12.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.11.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.10.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.9.1 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.9.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.8.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.7.1 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.7.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.6.1 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.6.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.5.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.4.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.3.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.2.0 lib/shrine/plugins/kithe_multi_cache.rb
kithe-2.1.0 lib/shrine/plugins/kithe_multi_cache.rb