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



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

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.



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

def big_url
  return unless photo?

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

#can_participate?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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?
    url&.split(".")&.last&.split("&")&.first&.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



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

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


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

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.



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

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
# File 'app/models/decidim/attachment.rb', line 100

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