Class: Alchemy::Attachment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Filetypes, NameConversions, Touching
Defined in:
app/models/alchemy/attachment.rb

Constant Summary

Constants included from Filetypes

Filetypes::ARCHIVE_FILE_TYPES, Filetypes::AUDIO_FILE_TYPES, Filetypes::IMAGE_FILE_TYPES, Filetypes::VCARD_FILE_TYPES, Filetypes::VIDEO_FILE_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Touching

#touch

Methods included from NameConversions

#convert_to_humanized_name, #convert_to_urlname

Class Method Details

.allowed_filetypesObject



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

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

.file_types_for_selectObject



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

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

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
120
121
122
123
124
125
# File 'app/models/alchemy/attachment.rb', line 96

def icon_css_class
  case file_mime_type
  when "application/x-shockwave-flash"
    then "flash"
  when "image/x-psd"
    then "psd"
  when "text/plain"
    then "text"
  when "application/rtf"
    then "rtf"
  when "application/pdf"
    then "pdf"
  when "application/msword"
    then "word"
  when "application/vnd.ms-excel"
    then "excel"
  when *VCARD_FILE_TYPES
    then "vcard"
  when *ARCHIVE_FILE_TYPES
    then "archive"
  when *AUDIO_FILE_TYPES
    then "audio"
  when *IMAGE_FILE_TYPES
    then "image"
  when *VIDEO_FILE_TYPES
    then "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