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



143
144
145
# File 'app/models/decidim/attachment.rb', line 143

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.



127
128
129
130
131
# File 'app/models/decidim/attachment.rb', line 127

def big_url
  return unless photo?

  @big_url ||= attached_uploader(:file).variant_url(:big)
end

#can_participate?(user) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
# File 'app/models/decidim/attachment.rb', line 147

def can_participate?(user)
  return true unless attached_to
  return true unless attached_to.respond_to?(:can_participate?)

  attached_to.can_participate?(user)
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

#editable_link?Boolean

Whether this attachment is a link that can be edited or not.

Returns Boolean.

Returns:

  • (Boolean)


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

def editable_link?
  !destroyed? && !frozen? && link?
end

#file?Boolean

Whether this attachment has a file or not.

Returns Boolean.

Returns:

  • (Boolean)


82
83
84
# File 'app/models/decidim/attachment.rb', line 82

def file?
  file.attached?
end

#file_typeObject

Which kind of file this is.

Returns String.



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

def file_type
  if file?
    file.filename.extension&.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

#private_download_authorized?(user, requested_attachment_name) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
157
158
# File 'app/models/decidim/attachment.rb', line 154

def private_download_authorized?(user, requested_attachment_name)
  return false unless requested_attachment_name.to_s == "file"

  can_participate?(user)
end

#private_download_required?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
# File 'app/models/decidim/attachment.rb', line 160

def private_download_required?
  return attached_to.restricted? if attached_to.respond_to?(:restricted?)

  attached_to.respond_to?(:component) && attached_to.component&.restricted_space?
end

#set_content_type_and_sizeObject



133
134
135
136
# File 'app/models/decidim/attachment.rb', line 133

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


138
139
140
141
# File 'app/models/decidim/attachment.rb', line 138

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.



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

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/decidim/attachment.rb', line 100

def url
  @url ||=
    if file?
      if private_download_required?
        Decidim::Core::Engine.routes.url_helpers.private_download_path(
          Decidim::PrivateDownload.for(self, attachment_name: :file).token
        )
      else
        attached_uploader(:file).url
      end
    elsif link?
      link
    end
end