Class: ImageFileInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Defined in:
app/inputs/image_file_input.rb

Instance Method Summary collapse

Instance Method Details

#input(wrapper_options = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/inputs/image_file_input.rb', line 2

def input(wrapper_options = nil)
  merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

  # For simple form uploads, use a standard file input with preview
  container_id = "image-upload-#{SecureRandom.hex(8)}"

  # Standard file input (accept any file; preview only when renderable)
  file_input = @builder.file_field(attribute_name, merged_input_options.merge(
    id: container_id,
    class: 'form-control image-file-input',
    data: {
      preview_id: "#{container_id}-preview",
      max_size: options[:max_file_size] || 10.megabytes
    }
  ))

  # Preview container
  preview_container = template.tag.div('',
    id: "#{container_id}-preview",
    class: 'image-preview-container mt-2',
    style: 'display: none;')

  # Wrapper container with Stimulus controller
  template.tag.div(
    file_input + preview_container,
    class: 'image-upload-wrapper',
    data: {
      controller: 'image-preview'
    }
  )
end