Class: SingleDeletableFileInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Includes:
ActionDispatch::Routing::PolymorphicRoutes, ActionView::Helpers::AssetTagHelper
Defined in:
lib/simple_form_bs5_file_input/single_deletable_file_input.rb

Instance Method Summary collapse

Instance Method Details

#change_file_textObject



46
47
48
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 46

def change_file_text
  I18n.t('simple_form_bs5_file_input.replace_file')
end

#existing_file_name_or_default_textObject



54
55
56
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 54

def existing_file_name_or_default_text
  "#{file_attachment.filename}" if should_display_file?
end

#field_classes(wrapper_options) ⇒ Object



41
42
43
44
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 41

def field_classes(wrapper_options)
  # "form-control is-valid single_deletable_file optional"
  "sdfi-deletable-file__block #{merge_wrapper_options(input_html_options, wrapper_options)[:class].join(' ')}"
end

#field_idObject



50
51
52
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 50

def field_id
  "#{object_name}_#{reflection_or_attribute_name}"
end

#has_file_classObject



28
29
30
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 28

def has_file_class
  'sdfi-deletable-file--with-file' if should_display_file?
end

#input(wrapper_options = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 7

def input(wrapper_options = nil)
  format('
    <div class="sdfi-deletable-file js-sdfi-deletable-file %s">
      %s
      <div class="%s">
        <button type="button" class="btn js-sdfi-deletable-file__change-btn">%s</button>
        <label for="%s" class="sdfi-deletable-file__label js-sdfi-deletable-file__label">
          %s
        </label>
        <div class="sdfi-deletable-file__delete-btn js-sdfi-deletable-file__delete-btn"></div>
        <div class="sdfi-deletable-file__upload-background"></div>
        <div class="sdfi-deletable-file__upload-progress"></div>
        <input type="hidden" name="%s" class="js-sdfi-deletable-file__infos-field" %s />
        <input type="hidden" name="%s" class="js-sdfi-deletable-file__delete-field" %s />
      </div>
      %s
      %s
    </div>
  ', has_file_class, input_field(wrapper_options), field_classes(wrapper_options), change_file_text, field_id, existing_file_name_or_default_text, input_infos_name, input_infos_value, input_delete_name, input_delete_value, preview_div, resize_div)
end

#input_delete_nameObject



58
59
60
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 58

def input_delete_name
  "#{@builder.object_name}[#{attribute_name.to_s}_delete]"
end

#input_delete_valueObject



62
63
64
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 62

def input_delete_value
  "value='true'" if @builder.object.send("#{attribute_name}_delete") == 'true'
end

#input_field(wrapper_options) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 32

def input_field(wrapper_options)
  merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
  if options[:direct_upload]
    merged_input_options[:data] = {} if merged_input_options[:data].nil?
    merged_input_options[:data]['direct-upload-url'] = Rails.application.routes.url_helpers.rails_direct_uploads_path
  end
  @builder.file_field(attribute_name, merged_input_options)
end

#input_infos_nameObject



66
67
68
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 66

def input_infos_name
  "#{@builder.object_name}[#{attribute_name.to_s}_infos]"
end

#input_infos_valueObject



70
71
72
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 70

def input_infos_value
  "value='#{@builder.object.send("#{attribute_name}_infos")}'" if @builder.object.send("#{attribute_name}_infos")
end

#preview_divObject



74
75
76
77
78
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 74

def preview_div
  if options[:preview]
    format('<div class="sdfi-deletable-file__preview js-sdfi-deletable-file__preview" data-size="%s">%s</div>', preview_image_width, preview_image_tag)
  end
end

#resize_divObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 80

def resize_div
  if options[:resize]
    format('<div class="sdfi-deletable-file__resize js-sdfi-deletable-file__resize modal" tabindex="-1">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title">%s</h5>
                    <button type="button" class="btn-close js-sdfi-deletable-file__resize-cancel" data-bs-dismiss="modal" aria-label="%s"></button>
                  </div>
                  <div class="modal-body">
                    <div class="sdfi-sdfi-deletable-file__resize-image js-sdfi-sdfi-deletable-file__resize-image" data-ratio="%s">

                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-sm btn-secondary js-sdfi-deletable-file__resize-cancel" data-bs-dismiss="modal">%s</button>
                    <button type="button" class="btn btn-sm btn-primary js-sdfi-deletable-file__resize-validate">%s</button>
                  </div>
                </div>
              </div>
            </div>', modal_title, close_btn_text, resize_ratio, close_btn_text, validate_btn_text)
  end
end