Class: Alchemy::Attachment

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

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

Instance Method Summary collapse

Methods included from ContentTouching

included

Methods included from Taggable

included, #tag_list=

Methods included from NameConversions

#convert_to_humanized_name, #convert_to_urlname

Methods inherited from BaseRecord

#active_record_5_1?

Class Method Details

.allowed_filetypesObject


44
45
46
# File 'app/models/alchemy/attachment.rb', line 44

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

.file_types_for_selectObject


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

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_attributesObject


40
41
42
# File 'app/models/alchemy/attachment.rb', line 40

def searchable_alchemy_resource_attributes
  %w(name file_name)
end

Instance Method Details

#extensionObject Also known as: suffix

File format suffix


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

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

#icon_css_classObject

Returns a css class name for kind of file


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/alchemy/attachment.rb', line 96

def icon_css_class
  case file_mime_type
  when "application/pdf"
    then "file-pdf"
  when "application/msword"
    then "file-word"
  when *TEXT_FILE_TYPES
    then "file-alt"
  when *EXCEL_FILE_TYPES
    then "file-excel"
  when *VCARD_FILE_TYPES
    then "address-card"
  when *ARCHIVE_FILE_TYPES
    then "file-archive"
  when *AUDIO_FILE_TYPES
    then "file-audio"
  when *IMAGE_FILE_TYPES
    then "file-image"
  when *VIDEO_FILE_TYPES
    then "file-video"
  else
    "file"
  end
end

#restricted?Boolean

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

Returns:

  • (Boolean)

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

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

#to_jq_uploadObject

Instance methods


70
71
72
73
74
75
76
# File 'app/models/alchemy/attachment.rb', line 70

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

#urlnameObject

An url save filename without format suffix


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

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