From 514cb89b5fca67df33c1a35856ce807e10894396 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 1 Sep 2020 18:33:10 -0300 Subject: [PATCH] poder borrar archivos e imagenes closes #172 closes #182 --- app/models/metadata_file.rb | 16 +++++++++++----- app/views/posts/attributes/_file.haml | 3 +++ app/views/posts/attributes/_image.haml | 8 +++++--- config/locales/en.yml | 3 +++ config/locales/es.yml | 3 +++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/models/metadata_file.rb b/app/models/metadata_file.rb index a4cf8d1..6b43608 100644 --- a/app/models/metadata_file.rb +++ b/app/models/metadata_file.rb @@ -24,23 +24,24 @@ class MetadataFile < MetadataTemplate # Determina si necesitamos la imagen pero no la tenemos def path_missing? - required && !value['path'].blank? + required && !path? end # Determina si el archivo ya fue subido def uploaded? - value['path'].is_a?(String) + value['path'].is_a?(String) && path? end # Determina si la ruta es opcional pero deja pasar si la ruta se # especifica def path_optional? - !required && value['path'].blank? + !required && !path? end # Asociar la imagen subida al sitio y obtener la ruta def save - value['description'] = sanitize(value['description']) + value['description'] = sanitize value['description'] + value['path'] = nil unless path? return true if uploaded? return true if path_optional? @@ -57,6 +58,7 @@ class MetadataFile < MetadataTemplate # @return ActiveStorage::Attachment def static_file return @static_file if @static_file + return unless path? ActiveRecord::Base.connection_pool.with_connection do if uploaded? @@ -70,6 +72,10 @@ class MetadataFile < MetadataTemplate private + def path? + !value['path'].blank? + end + def filemagic @filemagic ||= FileMagic.new(FileMagic::MAGIC_MIME) end @@ -82,7 +88,7 @@ class MetadataFile < MetadataTemplate # # @return [String|Nil] def file - return unless value['path'] + return unless path? if value['path'].is_a? ActionDispatch::Http::UploadedFile value['path'].tempfile.path diff --git a/app/views/posts/attributes/_file.haml b/app/views/posts/attributes/_file.haml index 2d857d3..ee46eae 100644 --- a/app/views/posts/attributes/_file.haml +++ b/app/views/posts/attributes/_file.haml @@ -14,6 +14,9 @@ - else = link_to t('posts.attribute_ro.file.download'), 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 = file_field(*field_name_for('post', attribute, :path), diff --git a/app/views/posts/attributes/_image.haml b/app/views/posts/attributes/_image.haml index 2accd10..e8034dd 100644 --- a/app/views/posts/attributes/_image.haml +++ b/app/views/posts/attributes/_image.haml @@ -5,10 +5,11 @@ class: 'img-fluid', id: "#{attribute}-preview" - -# - Mantener el valor si no enviamos ninguna imagen - TODO: Agregar checkbox para eliminarla + -# Mantener el valor si no enviamos ninguna imagen = 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 = file_field(*field_name_for('post', attribute, :path), @@ -29,3 +30,4 @@ **field_options(attribute, metadata)) = render 'posts/attribute_feedback', post: post, attribute: [attribute, :description], metadata: metadata + diff --git a/config/locales/en.yml b/config/locales/en.yml index b6e81cc..5f16dd3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -463,8 +463,11 @@ en: locales: mapable: help: You can link to the article's translation by looking up its title here and pressing Add + file: + destroy: Remove file image: label: Imagen + destroy: Remove image reorder: 'Reorder posts' sort: by: 'Sort by' diff --git a/config/locales/es.yml b/config/locales/es.yml index 5e73321..97cf0a3 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -472,8 +472,11 @@ es: locales: mapable: help: Puedes vincular traducciones buscando el título aquí y presionando Agregar + file: + destroy: 'Eliminar archivo' image: label: Imagen + destroy: 'Eliminar imagen' reorder: 'Reordenar artículos' sort: by: 'Ordenar por'