Class: FilePickerInput

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

Overview

Returns a file upload input tag for a given block, along with label and instructions.

Instance Method Summary collapse

Instance Method Details

#input(_wrapper_options = nil) ⇒ Object



9
10
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
# File 'app/inputs/file_picker_input.rb', line 9

def input(_wrapper_options = nil)
  # New blocks will not have their attachments created yet.
  object.ensure_attachment_exists if object.respond_to?(:ensure_attachment_exists)

  # Need to explictly use correct id (i.e. image_block_file) rather than the autogenerated one (i.e. image_block_attachments_attributes_0_attachment_name)
  # Otherwise <label for=""/> won't be right.
  tag = ActionView::Helpers::Tags::Base.new(object_name, attribute_name, template, options)
  tag_id = tag.send(:tag_id)

  html = ""
  if render_section_picker?
    sections = sections_with_full_paths
    sections.each do |s|
      html << template.(:span, "", {:class => "section_id_map", style: 'display: hidden', :data => {:id => s.id, :path => s.prependable_path}})
    end
  end
  @builder.simple_fields_for :attachments do |a|
    if matching_attachment?(a)
      html << a.hidden_field("attachment_name", value: attribute_name.to_s)
      html << a.file_field(:data, input_html_options.merge('data-purpose' => "cms_file_field", id: tag_id))
      html << a.hint(options.delete(:hint)) if has_hint?
      if render_section_picker?
        html << a.input(:section_id, collection: sections, label_method: :full_path, include_blank: false, label: "Section", wrapper_html: {class: "inline-section-picker"}, input_html: {'data-purpose' => "section_selector"})
      end
      if render_path_input?
        klass = object.new_record? ? "suggest_file_path" : "keep_existing_path"
        html << a.input(:data_file_path, label: "Path", wrapper_html: {class: "inline-path"}, input_html: {class: klass})
      end
    end
    html << a.input(:id, as: :hidden, wrapper: false)
  end
  html.html_safe

end