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



123
124
125
# File 'app/models/decidim/attachment.rb', line 123

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.



107
108
109
110
111
# File 'app/models/decidim/attachment.rb', line 107

def big_url
  return unless photo?

  @big_url ||= attached_uploader(:file).variant_url(: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.



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

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)


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

def document?
  !photo?
end

#file_typeObject

Which kind of file this is.

Returns String.



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

def file_type
  if file?
    url&.split(".")&.last&.downcase
  elsif link?
    "link"
  end
end

#link?Boolean

Whether this attachment is a link or not.

Returns Boolean.

Returns:

  • (Boolean)


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

def link?
  link.present?
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.



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

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)


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

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

#set_content_type_and_sizeObject



113
114
115
116
# File 'app/models/decidim/attachment.rb', line 113

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


118
119
120
121
# File 'app/models/decidim/attachment.rb', line 118

def set_link_content_type_and_size
  self.content_type = "text/uri-list"
  self.file_size = 0
end

#thumbnail_urlObject

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

Returns String.



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

def thumbnail_url
  return unless photo?

  @thumbnail_url ||= attached_uploader(:file).variant_url(:thumbnail)
end

#urlObject

The URL that points to the attachment

Returns String.



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

def url
  @url ||=
    if file?
      attached_uploader(:file).url
    elsif link?
      link
    end
end