From 5f3bbbfb1f1259e25eda03744141edb6abbac40f Mon Sep 17 00:00:00 2001 From: f Date: Mon, 30 Sep 2019 15:20:23 -0300 Subject: [PATCH] hacer funcionar los metadatos booleanos de yapa, eliminar casos especiales en el formulario --- app/models/metadata_boolean.rb | 17 ++++++++++++++++- app/views/posts/_form.haml | 3 --- app/views/posts/_submit.haml | 5 ----- app/views/posts/attributes/_boolean.haml | 3 ++- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/models/metadata_boolean.rb b/app/models/metadata_boolean.rb index c4b4af00..f0c64c4f 100644 --- a/app/models/metadata_boolean.rb +++ b/app/models/metadata_boolean.rb @@ -6,8 +6,23 @@ class MetadataBoolean < MetadataTemplate false end - # Convertir el valor que pasemos a boolean + # Los checkboxes son especiales porque la especificación de HTML + # indica que no se envían aquellos checkboxes con valores vacíos, con + # lo que nunca sabemos cuándo se los está deshabilitando. Rails + # subsana esto enviando un valor vacío bajo el mismo nombre + # (generalmente '0'). + # + # En este caso, queremos priorizar el dato enviado por le usuarie + # antes que el generado internamente. def value + return false if false? + self[:value].present? || document.data.fetch(name.to_s, default_value) end + + private + + def false? + self[:value] == '0' + end end diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index 26b4e183..09136f3e 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -21,9 +21,6 @@ -# Dibuja cada atributo - post.attributes.each do |attribute| - -# El borrador se muestra distinto - - next if attribute == :draft - - metadata = post.send(attribute) - type = metadata.type diff --git a/app/views/posts/_submit.haml b/app/views/posts/_submit.haml index 4346c7c1..b3879fd5 100644 --- a/app/views/posts/_submit.haml +++ b/app/views/posts/_submit.haml @@ -1,10 +1,5 @@ .form-group = submit_tag t('.save'), class: 'btn submit-post' - -# Permitir guardar como borrador - - if post.attributes.include? :draft - = submit_tag t('.save_draft'), - class: 'btn submit-post-draft', - name: 'post[draft]' .invalid_help.alert.alert-danger.d-none = site.config.fetch('invalid_help', t('.invalid_help')) .sending_help.alert.alert-success.d-none diff --git a/app/views/posts/attributes/_boolean.haml b/app/views/posts/attributes/_boolean.haml index 976c4b86..7814ebbe 100644 --- a/app/views/posts/attributes/_boolean.haml +++ b/app/views/posts/attributes/_boolean.haml @@ -1,5 +1,6 @@ .form-check - = check_box_tag "post[#{attribute}", metadata.value, metadata.value, + = hidden_field_tag "post[#{attribute}]", '0' + = check_box_tag "post[#{attribute}]", metadata.value, metadata.value, class: "form-check-input #{invalid(post, attribute)}", aria: { describedby: id_for_help(attribute) }, autofocus: autofocus