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



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

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

#existing_file_name_or_default_textObject



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

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

#field_classes(wrapper_options) ⇒ Object



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

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



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

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

#has_file_classObject



32
33
34
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 32

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
# 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__hidden-field" %s />
      </div>
      %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_hidden_name, input_hidden_value, preview_div)
end

#input_field(wrapper_options) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 36

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_hidden_nameObject



64
65
66
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 64

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

#input_hidden_valueObject



68
69
70
# File 'lib/simple_form_bs5_file_input/single_deletable_file_input.rb', line 68

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

#preview_divObject



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

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