Module: Refile::AttachmentHelper
- Defined in:
- lib/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
-
#attachment_field(object_name, method, object:, **options) ⇒ ActiveSupport::SafeBuffer
Generates a form field which can be used with records which have attachments.
-
#attachment_image_tag(record, name, *args, fallback: nil, format: nil, host: nil, **options) ⇒ ActiveSupport::SafeBuffer?
Generates an image tag for the given attachment, adding appropriate classes and optionally falling back to the given fallback image if there is no file attached.
-
#attachment_url(record, name, *args, filename: nil, format: nil, host: nil) ⇒ String?
View helper which generates a url for an attachment.
Instance Method Details
#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.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/refile/rails/attachment_helper.rb', line 91 def (object_name, method, object:, **) [:data] ||= {} 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 html = hidden_field(object_name, method, value: attacher.data.to_json, object: object) html + file_field(object_name, method, ) end |
#attachment_image_tag(record, name, *args, fallback: nil, format: nil, host: nil, **options) ⇒ ActiveSupport::SafeBuffer?
Generates an image tag for the given attachment, adding appropriate classes and optionally falling back to the given fallback image if there is no file attached.
Returns nil
if there is no file attached and no fallback specified.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/refile/rails/attachment_helper.rb', line 67 def (record, name, *args, fallback: nil, format: nil, host: nil, **) file = record.send(name) classes = ["attachment", record.class.model_name.singular, name, *[:class]] if file image_tag((record, name, *args, format: format, host: host), .merge(class: classes)) elsif fallback classes << "fallback" image_tag(fallback, .merge(class: classes)) end end |
#attachment_url(record, name, *args, filename: nil, format: nil, host: nil) ⇒ String?
View helper which generates a url for an attachment. This generates a URL to the Refile::App which is assumed to be mounted in the Rails application.
Optionally the name of a processor and a arguments to it can be appended.
If the filename option is not given, the filename falls back to the
name
.
The host defaults to Refile.host, which is useful for serving all
attachments from a CDN. You can also override the host via the host
option.
Returns nil
if there is no file attached.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/refile/rails/attachment_helper.rb', line 40 def (record, name, *args, filename: nil, format: nil, host: nil) attacher = record.send(:"#{name}_attacher") file = attacher.get return unless file filename ||= attacher.basename || name.to_s format ||= attacher.extension backend_name = Refile.backends.key(file.backend) host = host || Refile.host || request.base_url filename = filename.parameterize("_") filename << "." << format.to_s if format ::File.join(host, main_app.refile_app_path, backend_name, *args.map(&:to_s), file.id.to_s, filename) end |