Class: Alchemy::Attachment
- Inherits:
-
BaseRecord
- Object
- ActiveRecord::Base
- BaseRecord
- Alchemy::Attachment
- Includes:
- Filetypes, NameConversions, Taggable, TouchElements
- Defined in:
- app/models/alchemy/attachment.rb,
app/models/alchemy/attachment/url.rb
Defined Under Namespace
Classes: Url
Constant Summary
Constants included from Filetypes
Filetypes::ARCHIVE_FILE_TYPES, Filetypes::AUDIO_FILE_TYPES, Filetypes::EXCEL_FILE_TYPES, Filetypes::IMAGE_FILE_TYPES, Filetypes::TEXT_FILE_TYPES, Filetypes::VCARD_FILE_TYPES, Filetypes::VIDEO_FILE_TYPES
Class Method Summary collapse
- .allowed_filetypes ⇒ Object
- .file_types_for_select ⇒ Object
- .searchable_alchemy_resource_attributes ⇒ Object
-
.url_class ⇒ Object
The class used to generate URLs for attachments.
-
.url_class=(klass) ⇒ Object
Set a different attachment url class.
Instance Method Summary collapse
-
#extension ⇒ Object
(also: #suffix)
File format suffix.
-
#icon_css_class ⇒ Object
Returns a css class name for kind of file.
-
#restricted? ⇒ Boolean
Checks if the attachment is restricted, because it is attached on restricted pages only.
-
#slug ⇒ Object
An url save filename without format suffix.
-
#to_jq_upload ⇒ Object
Instance methods.
- #url(options = {}) ⇒ Object
Methods included from TouchElements
Methods included from Taggable
Methods included from NameConversions
#convert_to_humanized_name, #convert_to_urlname
Class Method Details
.allowed_filetypes ⇒ Object
58 59 60 |
# File 'app/models/alchemy/attachment.rb', line 58 def allowed_filetypes Config.get(:uploader).fetch("allowed_filetypes", {}).fetch("alchemy/attachments", []) end |
.file_types_for_select ⇒ Object
62 63 64 65 66 67 |
# File 'app/models/alchemy/attachment.rb', line 62 def file_types_for_select file_types = Alchemy::Attachment.pluck(:file_mime_type).uniq.map do |type| [Alchemy.t(type, scope: "mime_types"), type] end file_types.sort_by(&:first) end |
.searchable_alchemy_resource_attributes ⇒ Object
54 55 56 |
# File 'app/models/alchemy/attachment.rb', line 54 def searchable_alchemy_resource_attributes %w(name file_name) end |
.url_class ⇒ Object
The class used to generate URLs for attachments
43 44 45 |
# File 'app/models/alchemy/attachment.rb', line 43 def url_class @_url_class ||= Alchemy::Attachment::Url end |
.url_class=(klass) ⇒ Object
Set a different attachment url class
50 51 52 |
# File 'app/models/alchemy/attachment.rb', line 50 def url_class=(klass) @_url_class = klass end |
Instance Method Details
#extension ⇒ Object Also known as: suffix
File format suffix
110 111 112 |
# File 'app/models/alchemy/attachment.rb', line 110 def extension file_name.split(".").last end |
#icon_css_class ⇒ Object
Returns a css class name for kind of file
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/models/alchemy/attachment.rb', line 118 def icon_css_class case file_mime_type when "application/pdf" "file-pdf" when "application/msword" "file-word" when *TEXT_FILE_TYPES "file-alt" when *EXCEL_FILE_TYPES "file-excel" when *VCARD_FILE_TYPES "address-card" when *ARCHIVE_FILE_TYPES "file-archive" when *AUDIO_FILE_TYPES "file-audio" when *IMAGE_FILE_TYPES "file-image" when *VIDEO_FILE_TYPES "file-video" else "file" end end |
#restricted? ⇒ Boolean
Checks if the attachment is restricted, because it is attached on restricted pages only
105 106 107 |
# File 'app/models/alchemy/attachment.rb', line 105 def restricted? pages.any? && pages.not_restricted.blank? end |
#slug ⇒ Object
An url save filename without format suffix
100 101 102 |
# File 'app/models/alchemy/attachment.rb', line 100 def slug CGI.escape(file_name.gsub(/\.#{extension}$/, "").tr(".", " ")) end |
#to_jq_upload ⇒ Object
Instance methods
85 86 87 88 89 90 91 |
# File 'app/models/alchemy/attachment.rb', line 85 def to_jq_upload { "name" => read_attribute(:file_name), "size" => read_attribute(:file_size), "error" => errors[:file].join, } end |
#url(options = {}) ⇒ Object
93 94 95 96 97 |
# File 'app/models/alchemy/attachment.rb', line 93 def url( = {}) if file self.class.url_class.new(self).call() end end |