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

View file

@ -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),

View file

@ -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

View file

@ -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'

View file

@ -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'