Module: ActiveStorageDragAndDrop::FormBuilder

Defined in:
lib/active_storage_drag_and_drop/form_builder.rb

Overview

Custom FormBuilder module. All code in this module is executed within the context of ActionView::Helpers::FormBuilder when ActionView is first loaded via the Engine

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#drag_and_drop_file_field(method, content_or_options = nil, options = {}, &block) ⇒ String

Returns a file upload input tag wrapped in markup that allows dragging and dropping of files onto the element.

Examples:

# Accept only PNGs or JPEGs up to 5MB in size:
form.drag_and_drop_file_field :images, nil, accept: 'image/png, image/jpeg',
                                            size_limit: 5_000_000
# Pass custom content string:
 form.drag_and_drop_file_field :images, '<div>Drag and Drop!</div>', accept: 'image/png'
# Pass a block of content instead of passing a string
<%= form.drag_and_drop_file_field(:images, accept: 'image/png') do %>
  <strong>Drag and Drop</strong> PNG files here or <strong>click to browse</strong>
<% end %>

Parameters:

  • method (Symbol)

    The attribute on the target model to attach the files to.

  • content (String)

    The content to render inside of the drag and drop file field.

  • options (Hash) (defaults to: {})

    A hash of options to customise the file field.

Options Hash (options):

  • :disabled (Boolean)

    If set to true, the user will not be able to use this input.

  • :mutiple (Boolean)

    If set to true, *in most updated browsers* the user will be allowed to select multiple files.

  • :accept (String)

    If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations.

  • :size_limit (Integer)

    The upper limit on filesize to accept in bytes. Client-side validation only. You still need to set up model validations.

Returns:

  • (String)

    The generated file field markup.

See Also:

Since:

  • 0.1.0



44
45
46
47
48
49
50
51
# File 'lib/active_storage_drag_and_drop/form_builder.rb', line 44

def drag_and_drop_file_field(method, content_or_options = nil, options = {}, &block)
  if block_given?
    options = content_or_options if content_or_options.is_a? Hash
    drag_and_drop_file_field_string(method, capture(&block), options)
  else
    drag_and_drop_file_field_string(method, content_or_options, options)
  end
end