class SimpleFormExtension::Inputs::ImageInput

Public Instance Methods

input(_wrapper_options = nil) click to toggle source
# File lib/simple_form_extension/inputs/image_input.rb, line 9
def input(_wrapper_options = nil)
  input_html_options[:class] << 'image-upload'
  input_html_options[:accept] ||= SimpleFormExtension.default_image_input_accept

  input_markup
end

Private Instance Methods

displayable?(image) click to toggle source

Ensure the image is displayable and stored into the backend, otherwise we cannot generate a preview and/or URL for it.

# File lib/simple_form_extension/inputs/image_input.rb, line 68
def displayable?(image)
  (image.variable? || image.previewable?) && image.persisted?
end
existing_image_tag() click to toggle source
# File lib/simple_form_extension/inputs/image_input.rb, line 35
def existing_image_tag
  # If an existing paperclip image is found
  if file_exists?
    file_preview_and_remove_button
  else
    content_tag(:div, class: 'fileinput-preview thumbnail') do
      content_tag(:div, '', class: 'empty-thumbnail')
    end
  end
end
file_preview_and_remove_button() click to toggle source
# File lib/simple_form_extension/inputs/image_input.rb, line 50
def file_preview_and_remove_button
  return super unless displayable?(image)

  container_style = 'position: relative; height: 100%; width: 100%; min-height: 50px;min-width: 58px; display: block;'

  content_tag(:div, class: 'fileinput-preview thumbnail') do
    content_tag(:div, style: container_style, data: { provides: 'existing-file' }) do
      image_tag(
        file_url,
        style: 'height: 100%; width: 100%; display: block;', data: { toggle: 'existing-file' }
      ) + remove_image_button
    end
  end
end
file_url() click to toggle source

Returns the paperclip or active_storage attachment url to show in the preview thumbnail

# File lib/simple_form_extension/inputs/image_input.rb, line 75
def file_url
  if paperclip_attachment_attached?
    object.send(attribute_name).url(image_style)
  elsif activestorage_attachment_attached?
    attachment = object.send(attribute_name)

    if attachment.representable?
      attachment.representation(resize: '400x150>')
    else
      attachment.service_url
    end
  end
end
image() click to toggle source
# File lib/simple_form_extension/inputs/image_input.rb, line 46
def image
  @image ||= object.try(attribute_name)
end
image_style() click to toggle source
# File lib/simple_form_extension/inputs/image_input.rb, line 101
def image_style
  styles = object.send(attribute_name).styles.map(&:first)
  # Check if there's a :thumb or :thumbnail style in attachment definition
  thumb = styles.find { |s| %w[thumb thumbnail].include?(s.to_s) }
  # Return the potentially smallest size !
  thumb || styles.first || :original
end
input_markup() click to toggle source
# File lib/simple_form_extension/inputs/image_input.rb, line 18
def input_markup
  content_tag(:div, class: 'fileinput fileinput-new', data: { provides: 'fileinput' }) do
    content_tag(:div) do
      content_tag(:div, class: 'btn btn-default btn-file') do
        content_tag(:div, _translate('image.select'), class: 'fileinput-new') +
          content_tag(:div, _translate('image.change'), class: 'fileinput-exists') +
          @builder.file_field(attribute_name, input_html_options)
      end +
        content_tag(:button, class: 'btn btn-danger fileinput-exists', type: 'button',
                             data: { dismiss: 'fileinput' }) do
          content_tag(:i, '', class: 'fa fa-times')
        end
    end +
      existing_image_tag
  end
end
remove_image_button() click to toggle source
# File lib/simple_form_extension/inputs/image_input.rb, line 89
def remove_image_button
  return unless object.respond_to?(:"#{remove_attachment_method}=")

  button_style = 'position: absolute; top: 10px; left: 10px;'

  content_tag(:button, class: 'btn btn-danger', style: button_style, type: 'button',
                       data: { dismiss: 'existing-file' }) do
    content_tag(:i, '', class: 'fa fa-remove', data: { 'removed-class': 'fa fa-refresh' }) +
      @builder.hidden_field(remove_attachment_method, class: 'remove-file-input', value: nil)
  end
end