Class: GalleryInput

Inherits:
SimpleForm::Inputs::CollectionSelectInput
  • Object
show all
Defined in:
app/inputs/gallery_input.rb

Instance Method Summary collapse

Instance Method Details

#input(wrapper_options = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/inputs/gallery_input.rb', line 11

def input(wrapper_options = nil)
  obj = input_html_options.fetch :object, object
  input_html_options.merge! id: object_name.parameterize

  associated_records = obj.send(reflection_or_attribute_name)

  field_name = input_html_options.fetch :field_name, "#{input_html_options[:id]}"

  placeholder = options[:placeholder] || 'Click here to add images to this gallery.'

  selected_images = template.render(partial: 'admin/media_items/media_item_grid_layout', collection: associated_records, as: :media_item, cached: true)

  modal_data_attributes = {
    toggle: 'modal',
    target: '.media-item-chooser',
    multiple: true,
    media_item_input: "##{field_name}",
    media_item_modal_path: template.admin_media_items_path
  }

  modal_data_attributes_for_preview = selected_images.present? ? {} : modal_data_attributes

  if selected_images.blank?
    images_or_placeholder = template.(:div, placeholder, class: 'text-center')
  else
    images_or_placeholder = selected_images
  end

  preview_html = template.(:div, images_or_placeholder, class: "media-gallery-preview row small-gutters", id: "#{field_name}_preview")

  content = ActiveSupport::SafeBuffer.new

  content << template.(:div,
                                  preview_html,
                                  class: "media-gallery-preview-wrapper #{selected_images.present? ? 'media-gallery-preview-wrapper--has-images' : 'media-gallery-preview-wrapper--no-images'} well",
                                  data: {
                                    **modal_data_attributes_for_preview})

  content << template.(:button, 'Choose Images',
                type: 'button',
                class: "media-item-chooser__button btn btn-default",
                data: {
                  **modal_data_attributes
                })

  content << @builder.collection_select(attribute_name,
            associated_records, :id, :title, { selected: associated_records&.collect(&:id) },
            multiple: true,
            id: field_name,
            class: 'gallery__input',
            hidden: true
          )

  content
end

#label(wrapper_options) ⇒ Object



3
4
5
6
7
8
9
# File 'app/inputs/gallery_input.rb', line 3

def label(wrapper_options)
  if options[:label]
    super
  else
    false
  end
end