-
Notifications
You must be signed in to change notification settings - Fork 402
/
base.rb
38 lines (30 loc) · 935 Bytes
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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