poder borrar archivos e imagenes closes #172 closes #182

This commit is contained in:
f 2020-09-01 18:33:10 -03:00
parent 26bd7540ce
commit 514cb89b5f
5 changed files with 25 additions and 8 deletions

View file

@ -24,23 +24,24 @@ class MetadataFile < MetadataTemplate
# Determina si necesitamos la imagen pero no la tenemos # Determina si necesitamos la imagen pero no la tenemos
def path_missing? def path_missing?
required && !value['path'].blank? required && !path?
end end
# Determina si el archivo ya fue subido # Determina si el archivo ya fue subido
def uploaded? def uploaded?
value['path'].is_a?(String) value['path'].is_a?(String) && path?
end end
# Determina si la ruta es opcional pero deja pasar si la ruta se # Determina si la ruta es opcional pero deja pasar si la ruta se
# especifica # especifica
def path_optional? def path_optional?
!required && value['path'].blank? !required && !path?
end end
# Asociar la imagen subida al sitio y obtener la ruta # Asociar la imagen subida al sitio y obtener la ruta
def save def save
value['description'] = sanitize(value['description']) value['description'] = sanitize value['description']
value['path'] = nil unless path?
return true if uploaded? return true if uploaded?
return true if path_optional? return true if path_optional?
@ -57,6 +58,7 @@ class MetadataFile < MetadataTemplate
# @return ActiveStorage::Attachment # @return ActiveStorage::Attachment
def static_file def static_file
return @static_file if @static_file return @static_file if @static_file
return unless path?
ActiveRecord::Base.connection_pool.with_connection do ActiveRecord::Base.connection_pool.with_connection do
if uploaded? if uploaded?
@ -70,6 +72,10 @@ class MetadataFile < MetadataTemplate
private private
def path?
!value['path'].blank?
end
def filemagic def filemagic
@filemagic ||= FileMagic.new(FileMagic::MAGIC_MIME) @filemagic ||= FileMagic.new(FileMagic::MAGIC_MIME)
end end
@ -82,7 +88,7 @@ class MetadataFile < MetadataTemplate
# #
# @return [String|Nil] # @return [String|Nil]
def file def file
return unless value['path'] return unless path?
if value['path'].is_a? ActionDispatch::Http::UploadedFile if value['path'].is_a? ActionDispatch::Http::UploadedFile
value['path'].tempfile.path value['path'].tempfile.path

View file

@ -14,6 +14,9 @@
- else - else
= link_to t('posts.attribute_ro.file.download'), = link_to t('posts.attribute_ro.file.download'),
url_for(metadata.static_file) url_for(metadata.static_file)
.custom-control.custom-switch
= check_box_tag "post[#{attribute}][path]", '', false, id: "post_#{attribute}_destroy", class: 'custom-control-input'
= label_tag "post_#{attribute}_destroy", t('posts.attributes.file.destroy'), class: 'custom-control-label'
.custom-file .custom-file
= file_field(*field_name_for('post', attribute, :path), = file_field(*field_name_for('post', attribute, :path),

View file

@ -5,10 +5,11 @@
class: 'img-fluid', class: 'img-fluid',
id: "#{attribute}-preview" id: "#{attribute}-preview"
-# -# Mantener el valor si no enviamos ninguna imagen
Mantener el valor si no enviamos ninguna imagen
TODO: Agregar checkbox para eliminarla
= hidden_field_tag "post[#{attribute}][path]", metadata.value['path'] = hidden_field_tag "post[#{attribute}][path]", metadata.value['path']
.custom-control.custom-switch
= check_box_tag "post[#{attribute}][path]", '', false, id: "post_#{attribute}_destroy", class: 'custom-control-input'
= label_tag "post_#{attribute}_destroy", t('posts.attributes.image.destroy'), class: 'custom-control-label'
.custom-file .custom-file
= file_field(*field_name_for('post', attribute, :path), = file_field(*field_name_for('post', attribute, :path),
@ -29,3 +30,4 @@
**field_options(attribute, metadata)) **field_options(attribute, metadata))
= render 'posts/attribute_feedback', = render 'posts/attribute_feedback',
post: post, attribute: [attribute, :description], metadata: metadata post: post, attribute: [attribute, :description], metadata: metadata

View file

@ -463,8 +463,11 @@ en:
locales: locales:
mapable: mapable:
help: You can link to the article's translation by looking up its title here and pressing Add help: You can link to the article's translation by looking up its title here and pressing Add
file:
destroy: Remove file
image: image:
label: Imagen label: Imagen
destroy: Remove image
reorder: 'Reorder posts' reorder: 'Reorder posts'
sort: sort:
by: 'Sort by' by: 'Sort by'

View file

@ -472,8 +472,11 @@ es:
locales: locales:
mapable: mapable:
help: Puedes vincular traducciones buscando el título aquí y presionando Agregar help: Puedes vincular traducciones buscando el título aquí y presionando Agregar
file:
destroy: 'Eliminar archivo'
image: image:
label: Imagen label: Imagen
destroy: 'Eliminar imagen'
reorder: 'Reordenar artículos' reorder: 'Reordenar artículos'
sort: sort:
by: 'Ordenar por' by: 'Ordenar por'