class SimpleFormExtension::Inputs::ImageInput
Public Instance Methods
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
Source
# File lib/simple_form_extension/inputs/image_input.rb, line 68 def displayable?(image) (image.variable? || image.previewable?) && image.persisted? end
Ensure the image is displayable and stored into the backend, otherwise we cannot generate a preview and/or URL for it.
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
Source
# 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
Returns the paperclip or active_storage attachment url to show in the preview thumbnail
Source
# File lib/simple_form_extension/inputs/image_input.rb, line 46 def image @image ||= object.try(attribute_name) end
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
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