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

#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.



83
84
85
86
87
# File 'app/models/decidim/attachment.rb', line 83

def big_url
  return unless photo?

  file.big.url
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.



39
40
41
42
43
# File 'app/models/decidim/attachment.rb', line 39

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)


55
56
57
# File 'app/models/decidim/attachment.rb', line 55

def document?
  !photo?
end

#file_typeObject

Which kind of file this is.

Returns String.



62
63
64
# File 'app/models/decidim/attachment.rb', line 62

def file_type
  file.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.



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

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

Whether this attachment is a photo or not.

Returns Boolean.

Returns:

  • (Boolean)


48
49
50
# File 'app/models/decidim/attachment.rb', line 48

def photo?
  @photo ||= content_type.start_with? "image"
end

#thumbnail_urlObject

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

Returns String.



74
75
76
77
78
# File 'app/models/decidim/attachment.rb', line 74

def thumbnail_url
  return unless photo?

  file.thumbnail.url
end