Module: BootstrapRefile::AttachmentHelper

Defined in:
lib/bootstrap_refile/rails/attachment_helper.rb

Overview

Rails view helpers which aid in using Refile from views.

Defined Under Namespace

Modules: FormBuilder

Instance Method Summary collapse

Instance Method Details

#bootstrap_attachment_field(object_name, method, object:, **options) ⇒ ActiveSupport::SafeBuffer

Generates a form field which can be used with records which have attachments. This will generate both a file field as well as a hidden field which tracks the id of the file in the cache before it is permanently stored.

Options Hash (**options):

  • object (Object)

    Set by the form builder, currently required for direct/presigned uploads to work.

  • direct (Boolean)

    If set to true, adds the appropriate data attributes for direct uploads with refile.js.

  • presign (Boolean)

    If set to true, adds the appropriate data attributes for presigned uploads with refile.js.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bootstrap_refile/rails/attachment_helper.rb', line 25

def bootstrap_attachment_field(object_name, method, object:, **options)
  file_icons = {
    'default' => 'fa-file-o',
    'pdf' => 'fa-file-pdf-o',
    'xls' => 'fa-file-excel-o',
    'xlsx' => 'fa-file-excel-o',
    'csv' => 'fa-file-excel-o',
    'doc' => 'fa-file-word-o',
    'docx' => 'fa-file-word-o',
    'jpg' => 'fa-file-image-o',
    'jpeg' => 'fa-file-image-o',
    'png' => 'fa-file-image-o',
    'gif' => 'fa-file-image-o'
  }

  options[:remove_class] ||= 'btn-primary'
  options[:select_class] ||= 'btn-primary'
  options[:progress_class] ||= ''

  options[:data] ||= {}

  attachment_name = method.to_s.humanize

  file_exists = (!object["#{method}_id"].nil?)
  file_extension = (file_exists)? File.extname(object["#{method}_filename"].to_s).gsub(/\./, '') : ''
  file_name = (file_exists && !object["#{method}_filename"].to_s.blank?)? object["#{method}_filename"].to_s : 'No file selected'

  image_url = (file_exists)? attachment_url(object, method.to_sym, :fit, 300, 300) : "http://placehold.it/300x200&text=#{attachment_name}"
  image_tag = image_tag(image_url, class: 'preview img-responsive')

  remove_attachment = check_box(object_name, "remove_#{method}".to_sym, { class: 'btn btn-primary' })
  attacher = object.send(:"#{method}_attacher")
  options[:accept] = attacher.accept

  if options[:direct]
    host = options[:host] || Refile.host || request.base_url
    backend_name = Refile.backends.key(attacher.cache)

    url = ::File.join(host, main_app.refile_app_path, backend_name)
    options[:data].merge!(direct: true, as: "file", url: url)
  end

  if options[:presigned] and attacher.cache.respond_to?(:presign)
    options[:data].merge!(direct: true).merge!(attacher.cache.presign.as_json)
  end

  attachment_field = hidden_field(object_name, method, value: attacher.data.to_json, object: object, id: nil)
  attachment_field += file_field(object_name, method, options)

  attachment_icon = (file_exists && file_icons[file_extension])? file_icons[file_extension] : file_icons['default']

  result = "  <div class=\"refile-upload \#{(file_exists)? 'filled' : ''} \#{(!file_extension.blank?)? \"filled-type-\#{file_extension}\" : ''}\">\n    <h4>\#{attachment_name}</h4>\n    <div class=\"row\">\n      <div class=\"col-sm-5\">\n        <span class=\"thumbnail\">\#{image_tag}</span>\n      </div>\n      <div class=\"col-sm-7\">\n        <div class=\"progress\">\n          <div class=\"progress-bar \#{options[:progress_class]}\" role=\"progressbar\" aria-valuenow=\"0\" aria-valuemin=\"0\" aria-valuemax=\"100\">\n            <span class=\"sr-only\">0% Complete</span>\n          </div>\n        </div>\n        <div class=\"file-info\"><i class=\"fa \#{attachment_icon}\"></i> \#{file_name}</div>\n        <span class=\"btn-group btn-remove\" data-toggle=\"buttons\">\n          <label class=\"btn \#{options[:remove_class]}\">\n            Remove \#{attachment_name}\n            \#{remove_attachment}\n          </label>\n        </span>\n        <span class=\"btn \#{options[:select_class]} btn-file\">\n          Select \#{attachment_name}\n          \#{attachment_field}\n        </span>\n      </div>\n    </div>\n  </div>\n"
  return result.html_safe
end