Class: Decidim::Attachment
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Attachment
- 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
-
#big_url ⇒ Object
The URL to download the a big version of the file.
- #can_participate?(user) ⇒ Boolean
-
#context ⇒ Object
The context of the attachments defines which file upload settings constraints should be used when the file is uploaded.
-
#document? ⇒ Boolean
Whether this attachment is a document or not.
-
#editable_link? ⇒ Boolean
Whether this attachment is a link that can be edited or not.
-
#file? ⇒ Boolean
Whether this attachment has a file or not.
-
#file_type ⇒ Object
Which kind of file this is.
-
#link? ⇒ Boolean
Whether this attachment is a link or not.
-
#organization ⇒ Object
Returns the organization related to this attachment in case the attached_to model belongs to an organization.
-
#photo? ⇒ Boolean
(also: #image?)
Whether this attachment is a photo or not.
- #set_content_type_and_size ⇒ Object
- #set_link_content_type_and_size ⇒ Object
-
#thumbnail_url ⇒ Object
The URL to download the thumbnail of the file.
-
#url ⇒ Object
The URL that points to the attachment.
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_url ⇒ Object
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
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 |
#context ⇒ Object
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. if attached_to.respond_to?(:attachment_context) :participant end |
#document? ⇒ Boolean
Whether this attachment is a document or not.
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.
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.
82 83 84 |
# File 'app/models/decidim/attachment.rb', line 82 def file? file.attached? end |
#file_type ⇒ Object
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.
68 69 70 |
# File 'app/models/decidim/attachment.rb', line 68 def link? link.present? end |
#organization ⇒ Object
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.
53 54 55 |
# File 'app/models/decidim/attachment.rb', line 53 def photo? @photo ||= file.attached? && file.image? end |
#set_content_type_and_size ⇒ Object
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 |
#set_link_content_type_and_size ⇒ Object
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_url ⇒ Object
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 |
#url ⇒ Object
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 |