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_cache_field(object_name, method, object:, **options) ⇒ ActiveSupport::SafeBuffer
Generates a hidden form field which tracks the id of the file in the cache before it is permanently stored.
-
#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_cache_field(object_name, method, object:, **options) ⇒ ActiveSupport::SafeBuffer
Generates a hidden form field which tracks the id of the file in the cache before it is permanently stored.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/refile/rails/attachment_helper.rb', line 105 def (object_name, method, object:, **) [:data] ||= {} [:data][:reference] ||= SecureRandom.hex attacher_value = object.send("#{method}_data") = { multiple: [:multiple], value: attacher_value.try(:to_json), object: object, disabled: attacher_value.blank?, id: nil, data: { reference: [:data][:reference] } } .merge!(index: [:index]) if .key?(:index) hidden_field(object_name, method, ) end |
#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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/refile/rails/attachment_helper.rb', line 75 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 [:include_hidden] = false (object_name, method, object: object, **) + 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.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/refile/rails/attachment_helper.rb', line 51 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.
32 33 34 35 36 37 38 39 |
# File 'lib/refile/rails/attachment_helper.rb', line 32 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 |