Class: Effective::FormInputs::FileField

Inherits:
Effective::FormInput show all
Defined in:
app/models/effective/form_inputs/file_field.rb

Constant Summary

Constants inherited from Effective::FormInput

Effective::FormInput::BLANK, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES

Instance Attribute Summary

Attributes inherited from Effective::FormInput

#name, #options

Instance Method Summary collapse

Methods inherited from Effective::FormInput

#feedback_options, #hint_options, #initialize, #input_group_options, #input_js_options, #label_options, #to_html, #wrapper_options

Constructor Details

This class inherits a constructor from Effective::FormInput

Instance Method Details

#build_attachment(attachment) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/effective/form_inputs/file_field.rb', line 30

def build_attachment(attachment)
  url = (@template.url_for(attachment) rescue false)
  return unless url

  (:div, class: 'col') do
    (:div, class: 'card mb-3') do
      if attachment.image?
        (:div, class: 'card-body') do
          (:img, '', class: 'img-fluid', src: url, alt: attachment.filename.to_s) +
          link_to(attachment.filename, url, class: 'card-link')
        end
      else
        (:div, class: 'card-body') do
          (:p, link_to(attachment.filename, url, class: 'card-link'), class: 'card-text') +
          (:p, class: 'card-text') do
            (attachment.content_type + '<br>' + @template.number_to_human_size(attachment.byte_size)).html_safe
          end
        end

      end.html_safe
    end
  end

end

#build_attachmentsObject



22
23
24
25
26
27
28
# File 'app/models/effective/form_inputs/file_field.rb', line 22

def build_attachments
  return ''.html_safe unless object.respond_to?(name) && object.send(name).respond_to?(:attached?) && object.send(name).attached?

  attachments = object.send(name).respond_to?(:length) ? object.send(name) : [object.send(name)]

  (:div, attachments.map { |attachment| build_attachment(attachment) }.join.html_safe, class: 'attachments row')
end

#build_input(&block) ⇒ Object



5
6
7
# File 'app/models/effective/form_inputs/file_field.rb', line 5

def build_input(&block)
  build_attachments + build_uploads + super
end

#build_uploadsObject



55
56
57
# File 'app/models/effective/form_inputs/file_field.rb', line 55

def build_uploads
  (:div, '', class: 'uploads')
end

#input_html_optionsObject



9
10
11
12
13
14
15
16
# File 'app/models/effective/form_inputs/file_field.rb', line 9

def input_html_options
  {
    class: 'form-control form-control-file btn btn-outline-secondary',
    multiple: multiple?,
    direct_upload: true,
    'data-progress-template': progress_template
  }
end

#multiple?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/models/effective/form_inputs/file_field.rb', line 18

def multiple?
  name.to_s.pluralize == name.to_s
end

#progress_templateObject



59
60
61
62
63
64
# File 'app/models/effective/form_inputs/file_field.rb', line 59

def progress_template
  (:div, class: 'direct-upload direct-upload--pending', 'data-direct-upload-id': '$ID$') do
    (:div, '', class: 'direct-upload__progress', style: 'width: 0%') +
    (:span, '$FILENAME$', class: 'direct-upload__filename')
  end
end