Class: Types::Objects::Base::AttachmentType

Inherits:
BaseObject
  • Object
show all
Defined in:
app/graphql/types/objects/base/attachment_type.rb

Instance Method Summary collapse

Instance Method Details

#base64Object



47
48
49
50
51
52
53
54
# File 'app/graphql/types/objects/base/attachment_type.rb', line 47

def base64
  data = if object.class.eql?(ActiveStorage::Variant)
           Base64.strict_encode64(object.blob.download)
         else
           Base64.strict_encode64(object.download)
         end
  "data:#{object.content_type};base64,#{data}"
end

#byte_sizeObject



56
57
58
59
60
61
62
# File 'app/graphql/types/objects/base/attachment_type.rb', line 56

def byte_size
  if object.class.eql?(ActiveStorage::Variant)
    object.blob.byte_size
  else
    object.byte_size
  end
end

#file_sizeObject



64
65
66
67
68
# File 'app/graphql/types/objects/base/attachment_type.rb', line 64

def file_size
  return nil if byte_size.blank?

  ActiveSupport::NumberHelper.number_to_human_size(byte_size)
end

#filenameObject



20
21
22
23
24
25
26
# File 'app/graphql/types/objects/base/attachment_type.rb', line 20

def filename
  if object.class.eql?(ActiveStorage::Variant)
    object.blob.filename.to_s + "-" + object.variation.transformations[:resize]
  else
    object.filename.to_s
  end
end

#idObject



12
13
14
15
16
17
18
# File 'app/graphql/types/objects/base/attachment_type.rb', line 12

def id
  if object.class.eql?(ActiveStorage::Variant)
    object.blob.id
  else
    object.id
  end
end

#resize_image(width, height, attachment) ⇒ Object



39
40
41
42
43
44
45
# File 'app/graphql/types/objects/base/attachment_type.rb', line 39

def resize_image(width, height, attachment)
  if width.present? && height.present? && (attachment.attached? && attachment.content_type.include?('image'))
    attachment.variant(resize_to_fill: [width, height])
  else
    attachment
  end
end

#url(resolution:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'app/graphql/types/objects/base/attachment_type.rb', line 28

def url(resolution:)
  resized_image = resize_image(resolution&.width, resolution&.height, object)
  if resized_image.class.eql?(ActiveStorage::Variant) || resized_image.class.eql?(ActiveStorage::VariantWithRecord)
    Rails.application.routes.url_helpers.rails_representation_url(resized_image)
  elsif defined?(resized_image.service_url)
    resized_image.service_url
  else
    resized_image.url
  end
end