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

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.

Parameters:

  • object_name

    The name of the object to generate a field for

  • method

    The name of the field

  • options (Hash)

Options Hash (**options):

  • object (Object)

    Set by the form builder

Returns:

  • (ActiveSupport::SafeBuffer)

    The generated hidden form field


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 attachment_cache_field(object_name, method, object:, **options)
  options[:data] ||= {}
  options[:data][:reference] ||= SecureRandom.hex

  attacher_value = object.send("#{method}_data")

  hidden_options = {
    multiple: options[:multiple],
    value: attacher_value.try(:to_json),
    object: object,
    disabled: attacher_value.blank?,
    id: nil,
    data: { reference: options[:data][:reference] }
  }
  hidden_options.merge!(index: options[:index]) if options.key?(:index)

  hidden_field(object_name, method, hidden_options)
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.

Parameters:

  • object_name

    The name of the object to generate a field for

  • method

    The name of the field

  • options (Hash)

Options Hash (**options):

  • object (Object)

    Set by the form builder, currently required for direct/presigned uploads to work.

  • direct (Boolean)

    If set to true, adds the appropriate data attributes for direct uploads with refile.js.

  • presign (Boolean)

    If set to true, adds the appropriate data attributes for presigned uploads with refile.js.

Returns:

  • (ActiveSupport::SafeBuffer)

    The generated form field


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 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
  options[:include_hidden] = false

  attachment_cache_field(object_name, method, object: object, **options) + file_field(object_name, method, options)
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.

Parameters:

  • fallback (String) (defaults to: nil)

    The path to an image asset to be used as a fallback

  • options (Hash)

    Additional options for the image tag

Returns:

  • (ActiveSupport::SafeBuffer, nil)

    The generated image tag

See Also:


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/refile/rails/attachment_helper.rb', line 51

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) ⇒ 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.

Parameters:

  • object (Refile::Attachment)

    Instance of a class which has an attached file

  • name (Symbol)

    The name of the attachment column

  • filename (String, nil)

    The filename to be appended to the URL

  • fallback (String, nil) (defaults to: nil)

    The path to an asset to be used as a fallback

  • format (String, nil)

    A file extension to be appended to the URL

  • host (String, nil)

    Override the host

Returns:

  • (String, nil)

    The generated URL

See Also:


32
33
34
35
36
37
38
39
# File 'lib/refile/rails/attachment_helper.rb', line 32

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