Class: Alchemy::Attachment

Inherits:
BaseRecord
  • Object
show all
Includes:
Filetypes, NameConversions, RelatableResource, Taggable, TouchElements
Defined in:
app/models/alchemy/attachment.rb

Constant Summary

Constants included from RelatableResource

RelatableResource::RELATED_INGREDIENTS_SUBQUERY

Constants included from Filetypes

Filetypes::ARCHIVE_FILE_TYPES, Filetypes::AUDIO_FILE_TYPES, Filetypes::EXCEL_FILE_TYPES, Filetypes::IMAGE_FILE_TYPES, Filetypes::POWERPOINT_FILE_TYPES, Filetypes::TEXT_FILE_TYPES, Filetypes::VCARD_FILE_TYPES, Filetypes::VIDEO_FILE_TYPES, Filetypes::WORD_FILE_TYPES

Constants included from SearchableResource

SearchableResource::SEARCHABLE_COLUMN_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TouchElements

included

Methods included from Taggable

included, #tag_list=

Methods included from NameConversions

#convert_to_urlname, #sanitized_filename

Methods included from ConfigMissing

#const_missing

Methods included from SearchableResource

#ransackable_associations, #ransackable_attributes, #ransackable_scopes, #ransortable_attributes

Class Method Details

.allowed_filetypesObject



107
108
109
# File 'app/models/alchemy/attachment.rb', line 107

def allowed_filetypes
  Alchemy.config.uploader.allowed_filetypes.alchemy_attachments
end

.file_types(scope = all, from_extensions: nil) ⇒ Object



98
99
100
101
102
103
104
105
# File 'app/models/alchemy/attachment.rb', line 98

def file_types(scope = all, from_extensions: nil)
  if from_extensions.present?
    scope = by_file_type(
      Array(from_extensions).map { |extension| Marcel::MimeType.for(extension:) }
    )
  end
  Alchemy.storage_adapter.file_formats(name, scope:)
end

.last_uploadObject



79
80
81
82
83
84
# File 'app/models/alchemy/attachment.rb', line 79

def last_upload
  last_id = Attachment.maximum(:id)
  return Attachment.all unless last_id

  where(id: last_id)
end

.ransackable_associations(_auth_object = nil) ⇒ Object



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

def ransackable_associations(_auth_object = nil)
  Alchemy.storage_adapter.ransackable_associations(name)
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



90
91
92
# File 'app/models/alchemy/attachment.rb', line 90

def ransackable_attributes(_auth_object = nil)
  Alchemy.storage_adapter.ransackable_attributes(name)
end

.ransackable_scopes(_auth_object = nil) ⇒ Object



111
112
113
# File 'app/models/alchemy/attachment.rb', line 111

def ransackable_scopes(_auth_object = nil)
  %i[by_file_type not_file_type recent last_upload without_tag deletable]
end

.searchable_alchemy_resource_attributesObject



86
87
88
# File 'app/models/alchemy/attachment.rb', line 86

def searchable_alchemy_resource_attributes
  Alchemy.storage_adapter.searchable_alchemy_resource_attributes(name)
end

.url_classObject

The class used to generate URLs for attachments



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

def url_class
  @_url_class ||= Alchemy.storage_adapter.attachment_url_class
end

.url_class=(klass) ⇒ Object

Set a different attachment url class

See Also:

  • Url


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

def url_class=(klass)
  @_url_class = klass
end

Instance Method Details

#deletable?Boolean

Override Alchemy::RelatableResource#deletable? to also consider /attachment/:id/download links inside ingredient values (e.g. Richtext markup, Link ingredients, raw Html).

Returns:

  • (Boolean)


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

def deletable?
  super && !referenced_in_ingredient_value?
end

#extensionObject Also known as: suffix

File format suffix



161
162
163
# File 'app/models/alchemy/attachment.rb', line 161

def extension
  Alchemy.storage_adapter.file_extension(self)
end

#file_mime_typeObject



156
157
158
# File 'app/models/alchemy/attachment.rb', line 156

def file_mime_type
  Alchemy.storage_adapter.file_mime_type(self)
end

#file_nameObject

File name



147
148
149
# File 'app/models/alchemy/attachment.rb', line 147

def file_name
  Alchemy.storage_adapter.file_name(self)
end

#file_sizeObject

File size



152
153
154
# File 'app/models/alchemy/attachment.rb', line 152

def file_size
  Alchemy.storage_adapter.file_size(self)
end

#icon_css_classObject

Returns a css class name for kind of file



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/models/alchemy/attachment.rb', line 172

def icon_css_class
  case file_mime_type
  when "application/pdf"
    "file-pdf-2"
  when *TEXT_FILE_TYPES
    "file-text"
  when *EXCEL_FILE_TYPES
    "file-excel-2"
  when *POWERPOINT_FILE_TYPES
    "file-ppt-2"
  when *WORD_FILE_TYPES
    "file-word-2"
  when *VCARD_FILE_TYPES
    "profile"
  when *ARCHIVE_FILE_TYPES
    "file-zip"
  when *AUDIO_FILE_TYPES
    "file-music"
  when *IMAGE_FILE_TYPES
    "file-image"
  when *VIDEO_FILE_TYPES
    "file-video"
  else
    "file-3"
  end
end

#restricted?Boolean

Checks if the attachment is restricted, because it is attached on restricted pages only

Returns:

  • (Boolean)


142
143
144
# File 'app/models/alchemy/attachment.rb', line 142

def restricted?
  related_pages.any? && related_pages.not_restricted.blank?
end

#slugObject

An url save filename without format suffix



130
131
132
# File 'app/models/alchemy/attachment.rb', line 130

def slug
  CGI.escape(file_name.gsub(/\.#{extension}$/, "").tr(".", " "))
end

#svg?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'app/models/alchemy/attachment.rb', line 166

def svg?
  file_mime_type == "image/svg+xml"
end

#url(options = {}) ⇒ Object

Instance methods



125
126
127
# File 'app/models/alchemy/attachment.rb', line 125

def url(options = {})
  self.class.url_class.new(self).call(options)
end