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::DEFAULT_FEEDBACK_OPTIONS, Effective::FormInput::DEFAULT_INPUT_GROUP_OPTIONS, Effective::FormInput::EMPTY_HASH, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES, Effective::FormInput::HORIZONTAL_LABEL_OPTIONS, Effective::FormInput::HORIZONTAL_WRAPPER_OPTIONS, Effective::FormInput::INLINE_LABEL_OPTIONS, Effective::FormInput::VERTICAL_WRAPPER_OPTIONS

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

#attachments_styleObject



144
145
146
# File 'app/models/effective/form_inputs/file_field.rb', line 144

def attachments_style
  @attachments_style ||= (options[:input].delete(:attachments_style) || options[:input].delete(:attachment_style) || :card)
end

#build_attachmentsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/effective/form_inputs/file_field.rb', line 45

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)]

  case attachments_style
  when :card
    build_card_attachments(attachments)
  when :table, :ck_assets
    build_table_attachments(attachments)
  else
    raise('unsupported attachments_style, try :card or :table')
  end
end

#build_card_attachment(attachment) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/effective/form_inputs/file_field.rb', line 101

def build_card_attachment(attachment)
  url = (@template.url_for(attachment) rescue false)
  url ||= (Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true) rescue false)

  return unless url

  (:div, class: 'col') do
    (:div, class: 'card mb-3') do
      if attachment.image?
        (:div, class: 'card-body') do
          image_tag(url, alt: attachment.filename.to_s, class: 'img-fluid') +
          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_card_attachments(attachments) ⇒ Object



97
98
99
# File 'app/models/effective/form_inputs/file_field.rb', line 97

def build_card_attachments(attachments)
  (:div, attachments.map { |attachment| build_card_attachment(attachment) }.join.html_safe, class: 'effective_file_attachments row')
end

#build_existing_attachmentsObject



35
36
37
38
39
40
41
42
43
# File 'app/models/effective/form_inputs/file_field.rb', line 35

def build_existing_attachments
  return ''.html_safe unless multiple?

  attachments = object.send(name)

  attachments.map.with_index do |attachment, index|
    @builder.hidden_field(name, multiple: true, id: (tag_id + "_#{index}"), value: attachment.signed_id)
  end.join.html_safe
end

#build_input(&block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'app/models/effective/form_inputs/file_field.rb', line 5

def build_input(&block)
  case attachments_style
  when :card
    build_existing_attachments + build_attachments + build_uploads + super
  when :table, :ck_assets
    super + build_existing_attachments + build_uploads + build_attachments
  else
    raise('unsupported attachments_style, try :card or :table')
  end
end

#build_table_attachment(attachment) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/effective/form_inputs/file_field.rb', line 76

def build_table_attachment(attachment)
  url = (@template.url_for(attachment) rescue false)
  url ||= (Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true) rescue false)

  return unless url

  image_tag = (:img, '', class: '', src: url, alt: attachment.filename.to_s) if attachment.image?
  link_tag = link_to(attachment.filename, url)
  size_tag = (attachment.content_type + '<br>' + @template.number_to_human_size(attachment.byte_size)).html_safe

  (:td, image_tag) +
  (:td, link_tag) +
  (:td, size_tag) +

  (:td) do
    if attachments_style == :ck_assets
      link_to('Attach', url, class: 'btn btn-primary', 'data-insert-ck-asset': true, alt: attachment.filename.to_s)
    end
  end
end

#build_table_attachments(attachments) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/effective/form_inputs/file_field.rb', line 60

def build_table_attachments(attachments)
  (:table, class: 'table table-hover effective_file_attachments') do
    (:thead) do
      (:tr) do
        (:th, 'Image') +
        (:th, 'Title') +
        (:th, 'Size') +
        (:th, '')
      end
    end +
    (:tbody) do
      attachments.map { |attachment| (:tr, build_table_attachment(attachment)) }.join.html_safe
    end
  end
end

#build_uploadsObject



128
129
130
# File 'app/models/effective/form_inputs/file_field.rb', line 128

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

#click_submit?Boolean

Returns:

  • (Boolean)


139
140
141
142
# File 'app/models/effective/form_inputs/file_field.rb', line 139

def click_submit?
  return @click_submit unless @click_submit.nil?
  @click_submit ||= (options.delete(:click_submit) || false)
end

#input_html_optionsObject



16
17
18
19
20
21
22
23
24
25
# File 'app/models/effective/form_inputs/file_field.rb', line 16

def input_html_options
  {
    id: tag_id,
    class: 'form-control form-control-file btn-outline-secondary',
    multiple: multiple?,
    direct_upload: true,
    'data-progress-template': progress_template,
    'data-click-submit': (true if click_submit?),
  }.compact
end

#multiple?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/models/effective/form_inputs/file_field.rb', line 27

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

#progress_templateObject



132
133
134
135
136
137
# File 'app/models/effective/form_inputs/file_field.rb', line 132

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

#required_presence?(obj, name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/models/effective/form_inputs/file_field.rb', line 31

def required_presence?(obj, name)
  super(obj, name) && Array(object.public_send(name)).length == 0
end