Sha256: e7aa5573c55e26fb144822d97e964ee4dfe5e8ee6f68ac7f7ce9eb863f184bac

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# encoding: utf-8
module CarrierWave
  module Workers

    class StoreAsset < Struct.new(:klass, :id, :column)
      if defined?(::Sidekiq)
        include ::Sidekiq::Worker

        sidekiq_options :queue => @queue
      end

      def self.perform(*args)
        new(*args).perform
      end

      def perform(*args)
        set_args(*args) unless args.empty?
        resource = klass.is_a?(String) ? klass.constantize : klass
        record = resource.find id
        if tmp = record.send(:"#{column}_tmp")
          asset = record.send(:"#{column}")
          cache_dir  = [asset.root, asset.cache_dir].join("/")
          cache_path = [cache_dir, tmp].join("/")
          tmp_dir = [cache_dir, tmp.split("/")[0]].join("/")
          record.send :"process_#{column}_upload=", true
          record.send :"#{column}_tmp=", nil
          File.open(cache_path) { |f| record.send :"#{column}=", f }
          if record.save!
            FileUtils.rm_r(tmp_dir, :force => true)
          end
        end
      end

      def set_args(klass, id, column)
        self.klass, self.id, self.column = klass, id, column
      end

    end # StoreAsset

  end # Workers
end # Backgrounder

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carrierwave_backgrounder-0.1.1 lib/backgrounder/workers/store_asset.rb