From 5b9cd27a79b6305b169cd0f77338f72067bf1dee Mon Sep 17 00:00:00 2001 From: f Date: Wed, 10 Jul 2024 17:08:47 -0300 Subject: [PATCH] refactor: convertir lista de errores en un componente --- app/views/posts/_errors.haml | 19 +++++++++++++++++++ app/views/posts/_form.haml | 28 +++++++++------------------- app/views/posts/_htmx_form.haml | 20 +------------------- 3 files changed, 29 insertions(+), 38 deletions(-) create mode 100644 app/views/posts/_errors.haml diff --git a/app/views/posts/_errors.haml b/app/views/posts/_errors.haml new file mode 100644 index 00000000..3b0a89dd --- /dev/null +++ b/app/views/posts/_errors.haml @@ -0,0 +1,19 @@ +- unless post.errors.empty? + - title = t('.errors.title') + - help = t('.errors.help') + = render 'bootstrap/alert' do + %h4= title + %p= help + + %ul + - post.errors.each do |attribute, errors| + - if errors.size > 1 + %li + %strong= post_label_t attribute, post: post + %ul + - errors.each do |error| + %li= error + - else + %li + %strong= post_label_t attribute, post: post + = errors.first diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index 3e09cb72..1c9c623e 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -1,22 +1,4 @@ -- unless post.errors.empty? - - title = t('.errors.title') - - help = t('.errors.help') - = render 'bootstrap/alert' do - %h4= title - %p= help - - %ul - - post.errors.each do |attribute, errors| - - if errors.size > 1 - %li - %strong= post_label_t attribute, post: post - %ul - - errors.each do |error| - %li= error - - else - %li - %strong= post_label_t attribute, post: post - = errors.first += render 'errors', post: post -# TODO: habilitar form_for :ruby @@ -55,3 +37,11 @@ -# Formularios usados por los modales = yield(:post_form) + +-# + Acumulador de formularios dinĂ¡micos, se van cargando a medida que se + necesitan en lugar de recursivamente. + + Nunca se eliminan los modales una vez que se cargan para poder tener + historial de cambios. +%div{ data: { controller: 'htmx', action: 'htmx:getUrl@window->htmx#beforeend' } } diff --git a/app/views/posts/_htmx_form.haml b/app/views/posts/_htmx_form.haml index bd786cc6..75967a40 100644 --- a/app/views/posts/_htmx_form.haml +++ b/app/views/posts/_htmx_form.haml @@ -47,25 +47,7 @@ end = form_tag url, **options do - - unless post.errors.empty? - - title = t('.errors.title') - - help = t('.errors.help') - = render 'bootstrap/alert' do - %h4= title - %p= help - - %ul - - post.errors.each do |attribute, errors| - - if errors.size > 1 - %li - %strong= post_label_t attribute, post: post - %ul - - errors.each do |error| - %li= error - - else - %li - %strong= post_label_t attribute, post: post - = errors.first + = render 'errors', post: post -# ParĂ¡metros para HTMX %input{ type: 'hidden', name: 'hide', value: pluck_param((post.errors.empty? ? :show : :hide), optional: true) }