Class: Effective::FormInputs::FileField
Constant Summary
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
#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_blank? ⇒ Boolean
43
44
45
|
# File 'app/models/effective/form_inputs/file_field.rb', line 43
def attachments_blank?
Array(object.public_send(name)).length == 0
end
|
#attachments_present? ⇒ Boolean
39
40
41
|
# File 'app/models/effective/form_inputs/file_field.rb', line 39
def attachments_present?
Array(object.public_send(name)).length > 0
end
|
#attachments_style ⇒ Object
186
187
188
|
# File 'app/models/effective/form_inputs/file_field.rb', line 186
def attachments_style
@attachments_style ||= (options[:input].delete(:attachments_style) || options[:input].delete(:attachment_style) || :card)
end
|
#build_attachments ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'app/models/effective/form_inputs/file_field.rb', line 60
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'app/models/effective/form_inputs/file_field.rb', line 116
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
content_tag(:div, class: 'col') do
content_tag(:div, class: 'card mb-3') do
if attachment.image?
content_tag(: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
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
112
113
114
|
# File 'app/models/effective/form_inputs/file_field.rb', line 112
def build_card_attachments(attachments)
content_tag(:div, attachments.map { |attachment| build_card_attachment(attachment) }.join.html_safe, class: 'effective_file_attachments row')
end
|
#build_existing_attachments ⇒ Object
This has the affect of appending files to the has_many. Which usually isnt what we want
48
49
50
51
52
53
54
55
56
57
58
|
# File 'app/models/effective/form_inputs/file_field.rb', line 48
def build_existing_attachments
attachments = Array(object.send(name))
attachments.map.with_index do |attachment, index|
if multiple?
@builder.hidden_field(name, multiple: true, id: (tag_id + "_#{index}"), value: attachment.signed_id)
else
@builder.hidden_field(name, id: tag_id, value: attachment.signed_id)
end
end.join.html_safe
end
|
7
8
9
10
11
12
13
14
15
16
|
# File 'app/models/effective/form_inputs/file_field.rb', line 7
def build_input(&block)
case attachments_style
when :card
build_existing_attachments + build_attachments + build_uploads_and_purge(super)
when :table, :ck_assets
build_existing_attachments + build_uploads_and_purge(super) + build_attachments
else
raise('unsupported attachments_style, try :card or :table')
end
end
|
#build_purge ⇒ Object
164
165
166
167
168
169
|
# File 'app/models/effective/form_inputs/file_field.rb', line 164
def build_purge
return ''.html_safe unless purge? && !disabled?
label = (multiple? ? 'Delete existing files on save' : 'Delete existing file on save')
@builder.check_box('_purge', multiple: true, label: label, id: "#{tag_id}_purge", checked_value: name)
end
|
#build_table_attachment(attachment) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'app/models/effective/form_inputs/file_field.rb', line 91
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 = 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'app/models/effective/form_inputs/file_field.rb', line 75
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
153
154
155
|
# File 'app/models/effective/form_inputs/file_field.rb', line 153
def build_uploads
content_tag(:div, '', class: 'uploads')
end
|
#build_uploads_and_purge(super_file_field) ⇒ Object
142
143
144
145
146
147
148
149
150
151
|
# File 'app/models/effective/form_inputs/file_field.rb', line 142
def build_uploads_and_purge(super_file_field)
if purge? && attachments_present?
content_tag(:div, class: 'd-flex align-items-center') do
content_tag(:div, (build_uploads + super_file_field), class: 'flex-grow-1 mr-3') +
content_tag(:div, build_purge)
end
else
build_uploads + super_file_field
end
end
|
#click_submit? ⇒ Boolean
181
182
183
184
|
# File 'app/models/effective/form_inputs/file_field.rb', line 181
def click_submit?
return @click_submit unless @click_submit.nil?
@click_submit ||= (options.delete(:click_submit) || false)
end
|
18
19
20
21
22
23
24
25
26
27
28
|
# File 'app/models/effective/form_inputs/file_field.rb', line 18
def input_html_options
{
id: tag_id,
class: 'form-control form-control-file btn-outline-secondary',
multiple: multiple?,
direct_upload: true,
'data-direct-upload-url': @template.main_app.rails_direct_uploads_url,
'data-progress-template': progress_template,
'data-click-submit': (true if click_submit?),
}.compact
end
|
#multiple? ⇒ Boolean
30
31
32
33
|
# File 'app/models/effective/form_inputs/file_field.rb', line 30
def multiple?
return @multiple unless @multiple.nil?
@multiple = options.key?(:multiple) ? options.delete(:multiple) : (name.to_s.pluralize == name.to_s)
end
|
#progress_template ⇒ Object
157
158
159
160
161
162
|
# File 'app/models/effective/form_inputs/file_field.rb', line 157
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
|
#purgable? ⇒ Boolean
176
177
178
179
|
# File 'app/models/effective/form_inputs/file_field.rb', line 176
def purgable?
return false unless object.class.try(:has_many_purgable?)
object.has_many_purgable_names.include?(name.to_sym)
end
|
#purge? ⇒ Boolean
171
172
173
174
|
# File 'app/models/effective/form_inputs/file_field.rb', line 171
def purge?
return @purge unless @purge.nil?
@purge = options[:input].key?(:purge) ? (options[:input].delete(:purge) && purgable?) : purgable?
end
|
#required_presence?(obj, name) ⇒ Boolean
35
36
37
|
# File 'app/models/effective/form_inputs/file_field.rb', line 35
def required_presence?(obj, name)
super(obj, name) && attachments_blank?
end
|