Module: ActiveAdminAddons::PaperclipAttachment

Defined in:
lib/activeadmin_addons/addons/paperclip_attachment.rb

Constant Summary collapse

KNOWN_EXTENSIONS =
%w{
  3gp 7z ace ai aif aiff amr asf asx bat bin bmp bup cab cbr cda cdl cdr chm
  dat divx dll dmg doc docx dss dvf dwg eml eps exe fla flv gif gz hqx htm html
  ifo indd iso jar jpeg jpg lnk log m4a m4b m4p m4v mcd mdb mid mov mp2 mp4
  mpeg mpg msi mswmm ogg pdf png pps ppt pptx ps psd pst ptb pub qbb qbw qxd ram
  rar rm rmvb rtf sea ses sit sitx ss swf tgz thm tif tmp torrent ttf txt
  vcd vob wav wma wmv wps xls xlsx xpi zip
}

Class Method Summary collapse

Class Method Details

.for_ext(file_extension) ⇒ Object



17
18
19
20
21
22
# File 'lib/activeadmin_addons/addons/paperclip_attachment.rb', line 17

def for_ext file_extension
  ext = file_extension.start_with?('.') ? file_extension[1..-1] : file_extension
  ext.downcase!
  ext = "unknown" unless KNOWN_EXTENSIONS.include?(ext)
  "fileicons/file_extension_#{ext}.png"
end

.icon_for_filename(filename) ⇒ Object



13
14
15
# File 'lib/activeadmin_addons/addons/paperclip_attachment.rb', line 13

def icon_for_filename filename
  for_ext File.extname(filename)
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/activeadmin_addons/addons/paperclip_attachment.rb', line 24

def link(context, model, attribute, options)
  doc = model.send(attribute)
  raise 'you need to pass a paperclip attribute' unless doc.respond_to?(:url)

  icon = icon_for_filename(doc.original_filename)
  icon_img = context.image_tag(icon, width: "20", height: "20", style: "margin-right: 5px; vertical-align: middle;")
  file_name = (options[:truncate] == true) ? context.truncate(doc.original_filename) : doc.original_filename
  label = context.(:span) do
    context.concat(icon_img)
    context.safe_concat(file_name)
  end

  context.link_to(label, doc.url, { target: "_blank" }) if doc.exists?
end