Class: ActiveAdminAddons::AttachmentBuilder

Inherits:
CustomBuilder show all
Defined in:
lib/activeadmin_addons/addons/attachment_builder.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 mp3 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
}

Instance Attribute Summary

Attributes inherited from CustomBuilder

#args, #block, #context, #model

Instance Method Summary collapse

Methods inherited from CustomBuilder

builder_method_name, create_view_methods, #initialize

Constructor Details

This class inherits a constructor from ActiveAdminAddons::CustomBuilder

Instance Method Details

#build_labelObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/activeadmin_addons/addons/attachment_builder.rb', line 23

def build_label
  icon = icon_for_filename(file.original_filename)
  style = { width: "20", height: "20", style: "margin-right: 5px; vertical-align: middle;" }
  icon_img = context.image_tag(icon, style)
  text = label_text

  context.(:span) do
    context.concat(icon_img)
    context.safe_concat(text)
  end
end

#fileObject



54
55
56
# File 'lib/activeadmin_addons/addons/attachment_builder.rb', line 54

def file
  model.send(attribute)
end

#for_ext(file_extension) ⇒ Object



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

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



12
13
14
# File 'lib/activeadmin_addons/addons/attachment_builder.rb', line 12

def icon_for_filename(filename)
  for_ext File.extname(filename.to_s)
end

#label_textObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/activeadmin_addons/addons/attachment_builder.rb', line 35

def label_text
  label = if options[:label].nil?
            file.original_filename
          elsif options[:label].is_a? Proc
            options[:label].call(model).to_s
          else
            options[:label].to_s
          end

  options[:truncate] ? context.truncate(label) : label
end

#renderObject



47
48
49
50
51
52
# File 'lib/activeadmin_addons/addons/attachment_builder.rb', line 47

def render
  raise "you need to pass a paperclip attribute" unless file.respond_to?(:url)
  options[:truncate] = options.fetch(:truncate, true)
  return nil unless file.file?
  context.link_to(build_label, file.url, target: "_blank", class: "attachment-link")
end