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
-
#bootstrap_attachment_field(object_name, method, object:, **options) ⇒ ActiveSupport::SafeBuffer
Generates a form field which can be used with records which have attachments.
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.
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 |
# File 'lib/bootstrap_refile/rails/attachment_helper.rb', line 25 def (object_name, method, object:, **) 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' } [:remove_class] ||= 'btn-primary' [:select_class] ||= 'btn-primary' [:progress_class] ||= '' [:data] ||= {} = 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)? (object, method.to_sym, :fit, 300, 300) : "http://placehold.it/300x200&text=#{}" image_tag = image_tag(image_url, class: 'preview img-responsive') = check_box(object_name, "remove_#{method}".to_sym, { class: 'btn btn-primary' }) attacher = object.send(:"#{method}_attacher") [:accept] = attacher.accept if [:direct] host = [:host] || Refile.host || request.base_url backend_name = Refile.backends.key(attacher.cache) url = ::File.join(host, main_app.refile_app_path, backend_name) [:data].merge!(direct: true, as: "file", url: url) end if [:presigned] and attacher.cache.respond_to?(:presign) [:data].merge!(direct: true).merge!(attacher.cache.presign.as_json) end = hidden_field(object_name, method, value: attacher.data.to_json, object: object, id: nil) += file_field(object_name, method, ) = (file_exists && file_icons[file_extension])? file_icons[file_extension] : file_icons['default'] result = <<-EOD <div class="refile-upload #{(file_exists)? 'filled' : ''} #{(!file_extension.blank?)? "filled-type-#{file_extension}" : ''}"> <h4>#{}</h4> <div class="row"> <div class="col-sm-5"> <span class="thumbnail">#{image_tag}</span> </div> <div class="col-sm-7"> <div class="progress"> <div class="progress-bar #{[:progress_class]}" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"> <span class="sr-only">0% Complete</span> </div> </div> <div class="file-info"><i class="fa #{}"></i> #{file_name}</div> <label style="display: block; padding-bottom: 10px;"> #{} Remove #{} </label> <span class="btn #{[:select_class]} btn-file"> Select #{} #{} </span> </div> </div> </div> EOD return result.html_safe end |