Class: Alchemy::Attachment

Inherits:
BaseRecord
  • Object
show all
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::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_humanized_name, #convert_to_urlname

Methods included from SearchableResource

#ransackable_associations, #ransackable_attributes, #ransortable_attributes

Class Method Details

.alchemy_resource_filtersObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/alchemy/attachment.rb', line 61

def alchemy_resource_filters
  [
    {
      name: :by_file_type,
      values: distinct.pluck(:file_mime_type).map { |type| [Alchemy.t(type, scope: "mime_types"), type] }.sort_by(&:first)
    },
    {
      name: :misc,
      values: %w[recent last_upload without_tag]
    }
  ]
end

.allowed_filetypesObject



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

def allowed_filetypes
  Config.get(:uploader).fetch("allowed_filetypes", {}).fetch("alchemy/attachments", [])
end

.last_uploadObject



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

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

  where(id: last_id)
end

.searchable_alchemy_resource_attributesObject



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

def searchable_alchemy_resource_attributes
  %w[name file_name]
end

.url_classObject

The class used to generate URLs for attachments

See Also:



50
51
52
# File 'app/models/alchemy/attachment.rb', line 50

def url_class
  @_url_class ||= Alchemy::Attachment::Url
end

.url_class=(klass) ⇒ Object

Set a different attachment url class

See Also:



57
58
59
# File 'app/models/alchemy/attachment.rb', line 57

def url_class=(klass)
  @_url_class = klass
end

Instance Method Details

#extensionObject Also known as: suffix

File format suffix



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

def extension
  file_name.split(".").last
end

#icon_css_classObject

Returns a css class name for kind of file



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/alchemy/attachment.rb', line 138

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)


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

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

#slugObject

An url save filename without format suffix



120
121
122
# File 'app/models/alchemy/attachment.rb', line 120

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

#to_jq_uploadObject

Instance methods



105
106
107
108
109
110
111
# File 'app/models/alchemy/attachment.rb', line 105

def to_jq_upload
  {
    "name" => read_attribute(:file_name),
    "size" => read_attribute(:file_size),
    "error" => errors[:file].join
  }
end

#url(options = {}) ⇒ Object



113
114
115
116
117
# File 'app/models/alchemy/attachment.rb', line 113

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