Sha256: 0dc453b348ffd5cbc95055d0cdaa692731ee256f27fc3c50c52c4860b9961664

Contents?: true

Size: 935 Bytes

Versions: 3

Compression:

Stored size: 935 Bytes

Contents

# encoding: utf-8
module CarrierWave
  module Workers

    module Base
      attr_accessor :klass, :id, :column, :record

      def initialize(*args)
        super(*args) unless self.class.superclass == Object
        set_args(*args) if args.present?
      end

      def perform(*args)
        set_args(*args) if args.present?
        self.record = constantized_resource.find id
      rescue *not_found_errors
      end

      private

      def not_found_errors
        [].tap do |errors|
          errors << ::ActiveRecord::RecordNotFound      if defined?(::ActiveRecord)
          errors << ::Mongoid::Errors::DocumentNotFound if defined?(::Mongoid)
        end
      end

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

      def constantized_resource
        klass.is_a?(String) ? klass.constantize : klass
      end
    end # Base

  end # Workers
end # CarrierWave

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
carrierwave_backgrounder-1.0.2 lib/backgrounder/workers/base.rb
carrierwave_backgrounder-1.0.1 lib/backgrounder/workers/base.rb
carrierwave_backgrounder-1.0.0 lib/backgrounder/workers/base.rb