lib/refile/rails/attachment_helper.rb in refile-0.5.5 vs lib/refile/rails/attachment_helper.rb in refile-0.6.0
- old
+ new
@@ -17,15 +17,21 @@
# @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
- def attachment_url(record, name, *args, **opts)
- Refile.attachment_url(record, name, *args, **opts)
+ 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
# 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.
@@ -34,16 +40,16 @@
#
# @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
- def attachment_image_tag(record, name, *args, fallback: nil, format: nil, host: nil, **options)
- file = record.send(name)
- classes = ["attachment", record.class.model_name.singular, name, *options[:class]]
+ 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, format: format, host: host), options.merge(class: classes))
+ 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
@@ -61,25 +67,30 @@
# @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
def attachment_field(object_name, method, object:, **options)
options[:data] ||= {}
- attacher = object.send(:"#{method}_attacher")
- options[:accept] = attacher.accept
+ definition = object.send(:"#{method}_attachment_definition")
+ options[:accept] = definition.accept
if options[:direct]
- host = options[:host] || Refile.host || request.base_url
- backend_name = Refile.backends.key(attacher.cache)
-
- url = ::File.join(host, main_app.refile_app_path, backend_name)
+ 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 attacher.cache.respond_to?(:presign)
- options[:data].merge!(direct: true).merge!(attacher.cache.presign.as_json)
+ 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
- html = hidden_field(object_name, method, value: attacher.data.to_json, object: object, id: nil)
- html + file_field(object_name, method, options)
+ 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
end
end