Class: Decidim::Attachment

Inherits:
ApplicationRecord show all
Includes:
HasUploadValidations, TranslatableResource
Defined in:
app/models/decidim/attachment.rb

Overview

Attachment can be any type of document or images related to a partcipatory process.

Instance Method Summary collapse

Methods included from HasUploadValidations

#attached_uploader, #maximum_avatar_size, #maximum_upload_size

Instance Method Details

#big_urlObject

The URL to download the a big version of the file. Only works with images.

Returns String.



85
86
87
88
89
# File 'app/models/decidim/attachment.rb', line 85

def big_url
  return unless photo?

  attached_uploader(:file).path(variant: :big)
end

#contextObject

The context of the attachments defines which file upload settings constraints should be used when the file is uploaded. The different contexts can limit for instance which file types the user is allowed to upload.

Returns Symbol.



41
42
43
44
45
# File 'app/models/decidim/attachment.rb', line 41

def context
  return attached_to.attachment_context if attached_to.respond_to?(:attachment_context)

  :participant
end

#document?Boolean

Whether this attachment is a document or not.

Returns Boolean.

Returns:

  • (Boolean)


58
59
60
# File 'app/models/decidim/attachment.rb', line 58

def document?
  !photo?
end

#file_typeObject

Which kind of file this is.

Returns String.



65
66
67
# File 'app/models/decidim/attachment.rb', line 65

def file_type
  url&.split(".")&.last&.downcase
end

#organizationObject

Returns the organization related to this attachment in case the attached_to model belongs to an organization. Otherwise will return nil.

Returns Decidim::Organization or nil.



27
28
29
30
31
32
33
# File 'app/models/decidim/attachment.rb', line 27

def organization
  return unless attached_to
  return attached_to if attached_to.is_a?(Decidim::Organization)
  return unless attached_to.respond_to?(:organization)

  attached_to.organization
end

#photo?Boolean Also known as: image?

Whether this attachment is a photo or not.

Returns Boolean.

Returns:

  • (Boolean)


50
51
52
# File 'app/models/decidim/attachment.rb', line 50

def photo?
  @photo ||= file.attached? && file.image?
end

#set_content_type_and_sizeObject



91
92
93
94
# File 'app/models/decidim/attachment.rb', line 91

def set_content_type_and_size
  self.content_type = file.content_type
  self.file_size = file.byte_size
end

#thumbnail_urlObject

The URL to download the thumbnail of the file. Only works with images.

Returns String.



76
77
78
79
80
# File 'app/models/decidim/attachment.rb', line 76

def thumbnail_url
  return unless photo?

  attached_uploader(:file).path(variant: :thumbnail)
end

#urlObject



69
70
71
# File 'app/models/decidim/attachment.rb', line 69

def url
  attached_uploader(:file).path
end