Module: Admin::FilePreviewHelper

Included in:
ResourcesHelper
Defined in:
app/helpers/admin/file_preview_helper.rb

Instance Method Summary collapse

Instance Method Details

#get_type_of_attachment(attachment) ⇒ Object



4
5
6
7
8
9
10
# File 'app/helpers/admin/file_preview_helper.rb', line 4

def get_type_of_attachment(attachment)
  if defined?(Paperclip) && attachment.is_a?(Paperclip::Attachment)
    :paperclip
  elsif defined?(Dragonfly) && attachment.is_a?(Dragonfly::ActiveModelExtensions::Attachment)
    :dragonfly
  end
end


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/admin/file_preview_helper.rb', line 12

def link_to_detach_attribute(attribute)
  validators = @item.class.validators.delete_if { |i| i.class != ActiveModel::Validations::PresenceValidator }.map { |i| i.attributes }.flatten.map { |i| i.to_s }

  attachment = @item.send(attribute)

  field = case get_type_of_attachment(attachment)
          when :dragonfly then attribute
          when :paperclip then "#{attribute}_file_name"
          end

  if !validators.include?(field) && attachment
    attribute_i18n = @item.class.human_attribute_name(attribute)
    message = Typus::I18n.t("Remove")
    label_text = <<-HTML
#{attribute_i18n}
<small>#{link_to message, { :action => 'update', :id => @item.id, :attribute => attribute }, :confirm => Typus::I18n.t("Are you sure?")}</small>
    HTML
    label_text.html_safe
  end
end

#typus_file_preview(item, attribute, options = {}) ⇒ Object



33
34
35
36
37
38
# File 'app/helpers/admin/file_preview_helper.rb', line 33

def typus_file_preview(item, attribute, options = {})
  if (attachment = item.send(attribute))
    adapter = get_type_of_attachment(attachment)
    send("typus_file_preview_for_#{adapter}", attachment, options)
  end
end

#typus_file_preview_for_dragonfly(attachment, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/admin/file_preview_helper.rb', line 40

def typus_file_preview_for_dragonfly(attachment, options = {})
  if attachment.mime_type =~ /^image\/.+/
    render "admin/helpers/file_preview",
           :preview => attachment.process(:thumb, Typus.image_preview_size).url,
           :thumb => attachment.process(:thumb, Typus.image_thumb_size).url,
           :options => options
  else
    link_to attachment.name, attachment.url
  end
end

#typus_file_preview_for_paperclip(attachment, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/admin/file_preview_helper.rb', line 51

def typus_file_preview_for_paperclip(attachment, options = {})
  if attachment.exists?
    styles = attachment.styles.keys
    if styles.include?(Typus.file_preview) && styles.include?(Typus.file_thumbnail)
      render "admin/helpers/file_preview",
             :preview => attachment.url(Typus.file_preview, false),
             :thumb => attachment.url(Typus.file_thumbnail, false),
             :options => options
    else
      link_to attachment.original_filename, attachment.url(:original, false)
    end
  end
end