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
39
40
41
42
43
44
45
46
|
# File 'lib/uploadcare/rails/active_record.rb', line 6
def is_uploadcare_file attribute, options = {}
options.symbolize_keys!
opts = {
autostore: true,
force_autostore: false
}.update options
define_method "#{attribute}" do
cdn_url = attributes[attribute.to_s].to_s
return nil if cdn_url.empty?
if instance_variable_defined?("@#{attribute}_cached")
instance_variable_get("@#{attribute}_cached")
else
api = ::Rails.application.config.uploadcare.api
file_data = File.new(api, cdn_url)
instance_variable_set("@#{attribute}_cached", file_data)
file_data
end
end
if opts[:autostore]
after_save "store_#{attribute}"
define_method "store_#{attribute}" do
re = /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/i
m = re.match(attributes[attribute.to_s])
return unless m && m[0]
uuid = m[0]
stored = ::Rails.cache.exist?(
"uploadcare.file.#{uuid}.store",
force: opts[:force_autostore]
)
unless stored
send(attribute).api.store
::Rails.cache.write("uploadcare.file.#{uuid}.store", true)
end
end
end
end
|