Module: Uploadcare::Rails::Mongoid::MountUploadcareFileGroup

Extended by:
ActiveSupport::Concern
Defined in:
lib/uploadcare/rails/mongoid/mount_uploadcare_file_group.rb

Overview

A module containing Mongoid extension. Allows to use uploadcare group methods in Rails models

Constant Summary collapse

GROUP_ID_REGEX =
/\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b~\d+/.freeze

Instance Method Summary collapse

Instance Method Details

#build_uploadcare_file_group(attribute) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/uploadcare/rails/mongoid/mount_uploadcare_file_group.rb', line 19

def build_uploadcare_file_group(attribute)
  cdn_url = read_attribute(attribute).to_s
  return if cdn_url.empty?

  group_id = IdExtractor.call(cdn_url, GROUP_ID_REGEX).presence
  cache_key = Group.build_cache_key(cdn_url)
  files_count = FilesCountExtractor.call(group_id)
  default_attributes = { cdn_url: cdn_url, id: group_id, files_count: files_count }
  file_attributes = ::Rails.cache.read(cache_key).presence || default_attributes
  Uploadcare::Rails::Group.new(file_attributes)
end