Module: SimpleFormExtension::FileConcern

Included in:
Inputs::FileInput, Inputs::ImageInput
Defined in:
lib/simple_form_extension/file_concern.rb

Instance Method Summary collapse

Instance Method Details

#existing_file_nameObject



28
29
30
31
32
33
34
# File 'lib/simple_form_extension/file_concern.rb', line 28

def existing_file_name
  if object.try(:"#{ attribute_name }?")
    object.send(:"#{ attribute_name }_file_name").html_safe
  elsif (attachment = object.try(attribute_name)).try(:attached?)
    attachment.filename.to_s
  end
end

#existing_file_tagObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/simple_form_extension/file_concern.rb', line 3

def existing_file_tag
  return '' unless file_exists?

  (:div, class: 'input-group help-block existing-file', data: { provides: 'existing-file'}) do
    (:span, class: 'input-group-addon') do
      "#{ _translate('file.existing_file') } : ".html_safe
    end +

    file_preview_and_remove_button
  end
end

#file_exists?Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/simple_form_extension/file_concern.rb', line 49

def file_exists?
  @file_exists ||= object.try(:"#{ attribute_name }?") ||
                   object.try(attribute_name).try(:attached?)
end

#file_preview_and_remove_buttonObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_form_extension/file_concern.rb', line 15

def file_preview_and_remove_button
  (:div, class: 'btn-group') do
    (:a, class: 'btn btn-default ', href: file_url, target: '_blank', data: { toggle: 'existing-file' }) do
      (:i, '', class: 'fa fa-file') +
      " ".html_safe +

      existing_file_name
    end +

    remove_file_button
  end
end

#file_urlObject



54
55
56
57
58
59
60
# File 'lib/simple_form_extension/file_concern.rb', line 54

def file_url
  if object.try(:"#{ attribute_name }?")
    object.send(attribute_name)
  elsif object.try(attribute_name).try(:attached?)
    object.try(attribute_name).try(:service_url)
  end
end

#remove_attachment_methodObject



45
46
47
# File 'lib/simple_form_extension/file_concern.rb', line 45

def remove_attachment_method
  options[:remove_method] || :"remove_#{ attribute_name }"
end

#remove_file_buttonObject



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

def remove_file_button
  return unless object.respond_to?(:"#{ remove_attachment_method }=")

  (:button, class: 'btn btn-default', type: 'button', data: { dismiss: 'existing-file' }) do
    (:i, '', class: 'fa fa-remove', data: { :'removed-class' => 'fa fa-refresh' }) +
    @builder.hidden_field(remove_attachment_method, class: 'remove-file-input', value: nil)
  end
end