Sha256: f0071446bd361262a432063c9c3315a4a66c66439c323f6c75cdf9a0e19d4aeb

Contents?: true

Size: 1.96 KB

Versions: 10

Compression:

Stored size: 1.96 KB

Contents

module ActiveAdminAddons
  class AttachmentBuilder < CustomBuilder
    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
    }

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

    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

    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.content_tag(:span) do
        context.concat(icon_img)
        context.safe_concat(text)
      end
    end

    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

    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.exists?
      context.link_to(build_label, file.url, target: "_blank", class: "attachment-link")
    end

    def file
      model.send(attribute)
    end
  end
end

ActiveAdminAddons::AttachmentBuilder.create_view_methods

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
activeadmin_addons-1.6.0 lib/activeadmin_addons/addons/attachment_builder.rb
activeadmin_addons-1.5.0 lib/activeadmin_addons/addons/attachment_builder.rb
activeadmin_addons-1.4.0 lib/activeadmin_addons/addons/attachment_builder.rb
activeadmin_addons-1.3.0 lib/activeadmin_addons/addons/attachment_builder.rb
activeadmin_addons-1.2.0 lib/activeadmin_addons/addons/attachment_builder.rb
activeadmin_addons-1.1.2 lib/activeadmin_addons/addons/attachment_builder.rb
activeadmin_addons-1.1.1 lib/activeadmin_addons/addons/attachment_builder.rb
activeadmin_addons-1.1.0 lib/activeadmin_addons/addons/attachment_builder.rb
activeadmin_addons-1.0.1 lib/activeadmin_addons/addons/attachment_builder.rb
activeadmin_addons-1.0.0 lib/activeadmin_addons/addons/attachment_builder.rb