Sha256: 9124403485b0c0910c53b1d4ac1bde0ac5cee3e6ef120abec53f4e8fdf8d6e24

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module Refile
  module Backgrounder
    class StoreWorker < ::ActiveJob::Base
      attr_reader :record, :attachment_name
      queue_as :refile

      def perform(record, attachment_name)
        @record = record
        @attachment_name = attachment_name
        return unless attachment_cached?

        file = upload attacher.get
        cleanup_cache!
        update_record_attachment(file) if file
        yield if block_given?
      end

      private

        def attacher
          @_attacher ||= record.send("#{attachment_name}_attacher")
        end

        def attachment_cached?
          attacher.cache_id
        end

        def upload(file)
          attacher.store.upload(file)
        end

        def cleanup_cache!
          attacher.delete!
        end

        def update_record_attachment(file)
          record._skip_refile_backgrounder = true
          record.send "#{attachment_name}_cache_id=", nil if record.respond_to? "#{attachment_name}_cache_id="
          record.send "#{attachment_name}_id=", file.id   if record.respond_to? "#{attachment_name}_id="
          record.save
        end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
refile-backgrounder-0.0.4 lib/refile/backgrounder/store_worker.rb
refile-backgrounder-0.0.3 lib/refile/backgrounder/store_worker.rb