Module: Uploadcare::Rails::ActiveRecord::MountUploadcareFile

Extended by:
ActiveSupport::Concern
Defined in:
lib/uploadcare/rails/active_record/mount_uploadcare_file.rb

Overview

A module containing ActiveRecord extension. Allows to use uploadcare file methods in Rails models

Instance Method Summary collapse

Instance Method Details

#build_uploadcare_file(attribute) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/uploadcare/rails/active_record/mount_uploadcare_file.rb', line 16

def build_uploadcare_file(attribute)
  cdn_url = attributes[attribute.to_s].to_s
  return if cdn_url.empty?

  uuid = IdExtractor.call(cdn_url)
  cache_key = File.build_cache_key(cdn_url)
  default_attributes = { cdn_url: cdn_url, uuid: uuid.presence }
  file_attributes = ::Rails.cache.read(cache_key).presence || default_attributes
  Uploadcare::Rails::File.new(file_attributes)
end