Class: Decidim::Attachment

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

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasUploadValidations

#attached_uploader, #maximum_avatar_size, #maximum_upload_size

Class Method Details

.log_presenter_class_for(_log) ⇒ Object



98
99
100
# File 'app/models/decidim/attachment.rb', line 98

def self.log_presenter_class_for(_log)
  Decidim::AdminLog::AttachmentPresenter
end

Instance Method Details

#big_urlObject

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

Returns String.



87
88
89
90
91
# File 'app/models/decidim/attachment.rb', line 87

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.



43
44
45
46
47
# File 'app/models/decidim/attachment.rb', line 43

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)


60
61
62
# File 'app/models/decidim/attachment.rb', line 60

def document?
  !photo?
end

#file_typeObject

Which kind of file this is.

Returns String.



67
68
69
# File 'app/models/decidim/attachment.rb', line 67

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.



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

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)


52
53
54
# File 'app/models/decidim/attachment.rb', line 52

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

#set_content_type_and_sizeObject



93
94
95
96
# File 'app/models/decidim/attachment.rb', line 93

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.



78
79
80
81
82
# File 'app/models/decidim/attachment.rb', line 78

def thumbnail_url
  return unless photo?

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

#urlObject



71
72
73
# File 'app/models/decidim/attachment.rb', line 71

def url
  attached_uploader(:file).path
end