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, host: nil, prefix: nil, format: 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, fallback: nil, **opts) ⇒ 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.
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 |
# File 'lib/refile/rails/attachment_helper.rb', line 69 def (object_name, method, object:, **) [:data] ||= {} definition = object.send(:"#{method}_attachment_definition") [:accept] = definition.accept if [:direct] url = Refile.(object, method, host: [:host], prefix: [:prefix]) [:data].merge!(direct: true, as: "file", url: url) end if [:presigned] and definition.cache.respond_to?(:presign) url = Refile.(object, method, host: [:host], prefix: [:prefix]) [:data].merge!(direct: true, presigned: true, url: url) end [:data][:reference] = SecureRandom.hex hidden_field(object_name, method, multiple: [:multiple], value: object.send("#{method}_data").try(:to_json), object: object, id: nil, data: { reference: [:data][:reference] } ) + file_field(object_name, method, ) end |
#attachment_image_tag(record, name, *args, fallback: nil, host: nil, prefix: nil, format: 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.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/refile/rails/attachment_helper.rb', line 45 def (record, name, *args, fallback: nil, host: nil, prefix: nil, format: nil, **) file = record && record.public_send(name) classes = ["attachment", (record.class.model_name.singular if record), name, *[:class]] if file image_tag((record, name, *args, host: host, prefix: prefix, format: format), .merge(class: classes)) elsif fallback classes << "fallback" image_tag(fallback, .merge(class: classes)) end end |
#attachment_url(record, name, *args, fallback: nil, **opts) ⇒ 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.
26 27 28 29 30 31 32 33 |
# File 'lib/refile/rails/attachment_helper.rb', line 26 def (record, name, *args, fallback: nil, **opts) file = record && record.public_send(name) if file Refile.(record, name, *args, **opts) elsif fallback asset_url(fallback) end end |