module Refile::AttachmentHelper

Rails view helpers which aid in using Refile from views.

Public Instance Methods

attachment_field(object_name, method, object:, **options) click to toggle source

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.

@param object_name The name of the object to generate a field for @param method The name of the field @param [Hash] options @option options [Object] object Set by the form builder, currently required for direct/presigned uploads to work. @option options [Boolean] direct If set to true, adds the appropriate data attributes for direct uploads with refile.js. @option options [Boolean] presign If set to true, adds the appropriate data attributes for presigned uploads with refile.js. @return [ActiveSupport::SafeBuffer] The generated form field

# File lib/refile/rails/attachment_helper.rb, line 69
def attachment_field(object_name, method, object:, **options)
  options[:data] ||= {}

  definition = object.send(:"#{method}_attachment_definition")
  options[:accept] = definition.accept

  if options[:direct]
    url = Refile.attachment_upload_url(object, method, host: options[:host], prefix: options[:prefix])
    options[:data].merge!(direct: true, as: "file", url: url)
  end

  if options[:presigned] and definition.cache.respond_to?(:presign)
    url = Refile.attachment_presign_url(object, method, host: options[:host], prefix: options[:prefix])
    options[:data].merge!(direct: true, presigned: true, url: url)
  end

  options[:data][:reference] = SecureRandom.hex

  hidden_field(object_name, method,
    multiple: options[:multiple],
    value: object.send("#{method}_data").try(:to_json),
    object: object,
    id: nil,
    data: { reference: options[:data][:reference] }
  ) + file_field(object_name, method, options)
end
attachment_image_tag(record, name, *args, fallback: nil, host: nil, prefix: nil, format: nil, **options) click to toggle source

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.

@param [String] fallback The path to an image asset to be used as a fallback @param [Hash] options Additional options for the image tag @see attachment_url @return [ActiveSupport::SafeBuffer, nil] The generated image tag

# File lib/refile/rails/attachment_helper.rb, line 45
def attachment_image_tag(record, name, *args, fallback: nil, host: nil, prefix: nil, format: nil, **options)
  file = record && record.public_send(name)
  classes = ["attachment", (record.class.model_name.singular if record), name, *options[:class]]

  if file
    image_tag(attachment_url(record, name, *args, host: host, prefix: prefix, format: format), options.merge(class: classes))
  elsif fallback
    classes << "fallback"
    image_tag(fallback, options.merge(class: classes))
  end
end
attachment_url(record, name, *args, fallback: nil, **opts) click to toggle source

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.

@see Refile.attachment_url

@param [Refile::Attachment] object Instance of a class which has an attached file @param [Symbol] name The name of the attachment column @param [String, nil] filename The filename to be appended to the URL @param [String, nil] fallback The path to an asset to be used as a fallback @param [String, nil] format A file extension to be appended to the URL @param [String, nil] host Override the host @return [String, nil] The generated URL

# File lib/refile/rails/attachment_helper.rb, line 26
def attachment_url(record, name, *args, fallback: nil, **opts)
  file = record && record.public_send(name)
  if file
    Refile.attachment_url(record, name, *args, **opts)
  elsif fallback
    asset_url(fallback)
  end
end