Class: Effective::FormInputs::FileField
Constant Summary
Effective::FormInput::BLANK, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES
Instance Attribute Summary
#name, #options
Instance Method Summary
collapse
#feedback_options, #hint_options, #initialize, #input_group_options, #input_js_options, #label_options, #to_html, #wrapper_options
Instance Method Details
#attachments_style ⇒ Object
125
126
127
|
# File 'app/models/effective/form_inputs/file_field.rb', line 125
def attachments_style
@attachments_style ||= (options[:input].delete(:attachments_style) || options[:input].delete(:attachment_style) || :card)
end
|
#build_attachments ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'app/models/effective/form_inputs/file_field.rb', line 30
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'app/models/effective/form_inputs/file_field.rb', line 84
def build_card_attachment(attachment)
url = (@template.url_for(attachment) rescue false)
return unless url
content_tag(:div, class: 'col') do
content_tag(:div, class: 'card mb-3') do
if attachment.image?
content_tag(:div, class: 'card-body') do
content_tag(:img, '', class: 'img-fluid', src: url, alt: attachment.filename.to_s) +
link_to(attachment.filename, url, class: 'card-link')
end
else
content_tag(:div, class: 'card-body') do
content_tag(:p, link_to(attachment.filename, url, class: 'card-link'), class: 'card-text') +
content_tag(: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
80
81
82
|
# File 'app/models/effective/form_inputs/file_field.rb', line 80
def build_card_attachments(attachments)
content_tag(:div, attachments.map { |attachment| build_card_attachment(attachment) }.join.html_safe, class: 'effective_file_attachments row')
end
|
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_attachments + build_uploads + super
when :table, :ck_assets
super + build_uploads + build_attachments
else
raise('unsupported attachments_style, try :card or :table')
end
end
|
#build_table_attachment(attachment) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'app/models/effective/form_inputs/file_field.rb', line 61
def build_table_attachment(attachment)
url = (@template.url_for(attachment) rescue false)
return unless url
image_tag = content_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
content_tag(:td, image_tag) +
content_tag(:td, link_tag) +
content_tag(:td, size_tag) +
content_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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'app/models/effective/form_inputs/file_field.rb', line 45
def build_table_attachments(attachments)
content_tag(:table, class: 'table table-hover effective_file_attachments') do
content_tag(:thead) do
content_tag(:tr) do
content_tag(:th, 'Image') +
content_tag(:th, 'Title') +
content_tag(:th, 'Size') +
content_tag(:th, '')
end
end +
content_tag(:tbody) do
attachments.map { |attachment| content_tag(:tr, build_table_attachment(attachment)) }.join.html_safe
end
end
end
|
#build_uploads ⇒ Object
109
110
111
|
# File 'app/models/effective/form_inputs/file_field.rb', line 109
def build_uploads
content_tag(:div, '', class: 'uploads')
end
|
#click_submit? ⇒ Boolean
120
121
122
123
|
# File 'app/models/effective/form_inputs/file_field.rb', line 120
def click_submit?
return @click_submit unless @click_submit.nil?
@click_submit ||= (options.delete(:click_submit) || false)
end
|
16
17
18
19
20
21
22
23
24
|
# File 'app/models/effective/form_inputs/file_field.rb', line 16
def input_html_options
{
class: 'form-control form-control-file btn btn-outline-secondary',
multiple: multiple?,
direct_upload: true,
'data-progress-template': progress_template,
'data-click-submit': (true if click_submit?),
}.compact
end
|
#multiple? ⇒ Boolean
26
27
28
|
# File 'app/models/effective/form_inputs/file_field.rb', line 26
def multiple?
name.to_s.pluralize == name.to_s
end
|
#progress_template ⇒ Object
113
114
115
116
117
118
|
# File 'app/models/effective/form_inputs/file_field.rb', line 113
def progress_template
content_tag(:div, class: 'direct-upload direct-upload--pending', 'data-direct-upload-id': '$ID$') do
content_tag(:div, '', class: 'direct-upload__progress', style: 'width: 0%') +
content_tag(:span, '$FILENAME$', class: 'direct-upload__filename')
end
end
|