From 1e13fc462102378f88963bf723ad55bad5a7d550 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 23 May 2024 17:47:02 -0300 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20no=20repetir=20c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/application_helper.rb | 10 ++++++++++ app/views/posts/attributes/_new_array.haml | 2 +- app/views/posts/attributes/_new_has_many.haml | 2 +- app/views/posts/attributes/_new_predefined_array.haml | 3 +-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fcbd4074..c83b2894 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -2,6 +2,8 @@ # Helpers module ApplicationHelper + BRACKETS = /[\[\]]/ + # Devuelve el atributo name de un campo anidado en el formato que # esperan los helpers *_field # @@ -19,6 +21,14 @@ module ApplicationHelper [root, name] end + # Obtiene un ID + # + # @param base [String] + # @param attribute [String, Symbol] + def id_for(base, attribute) + "#{base.gsub(BRACKETS, '_')}_#{attribute}".squeeze('_') + end + def plain_field_name_for(*names) root, name = field_name_for(*names) diff --git a/app/views/posts/attributes/_new_array.haml b/app/views/posts/attributes/_new_array.haml index 08fc6dee..b4407c0d 100644 --- a/app/views/posts/attributes/_new_array.haml +++ b/app/views/posts/attributes/_new_array.haml @@ -1,7 +1,7 @@ -# Genera un listado de checkboxes entre los que se puede elegir para guardar :ruby - id = "#{base.gsub(/[\[\]]/, '_')}_#{attribute}".squeeze('_') + id = id_for(base, attribute) name = "#{base}[#{attribute}][]" form_id = "form-#{Nanoid.generate}" diff --git a/app/views/posts/attributes/_new_has_many.haml b/app/views/posts/attributes/_new_has_many.haml index 48230759..9382e2af 100644 --- a/app/views/posts/attributes/_new_has_many.haml +++ b/app/views/posts/attributes/_new_has_many.haml @@ -8,7 +8,7 @@ del formulario principal porque no se pueden anidar. :ruby - id = "#{base.gsub(/[\[\]]/, '_')}_#{attribute}".squeeze('_') + id = id_for(base, attribute) name = "#{base}[#{attribute}][]" form_id = "form-#{Nanoid.generate}" modal_id = "modal-#{Nanoid.generate}" diff --git a/app/views/posts/attributes/_new_predefined_array.haml b/app/views/posts/attributes/_new_predefined_array.haml index 218a2cc2..36f1ae2f 100644 --- a/app/views/posts/attributes/_new_predefined_array.haml +++ b/app/views/posts/attributes/_new_predefined_array.haml @@ -3,8 +3,7 @@ guardar, pero no se pueden agregar nuevos. :ruby - # @todo Convertir en un helper - id = "#{base.gsub(/[\[\]]/, '_')}_#{attribute}".squeeze('_') + id = id_for(base, attribute) name = "#{base}[#{attribute}][]" form_id = "form-#{Nanoid.generate}" From d5d31a6d947950cfb6239cdd25148897db5e8248 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 23 May 2024 17:47:21 -0300 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20bot=C3=B3n=20de=20guardar=20en=20el?= =?UTF-8?q?=20pie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/posts/_htmx_form.haml | 7 +------ app/views/posts/attributes/_new_has_many.haml | 2 ++ config/locales/en.yml | 1 + config/locales/es.yml | 1 + 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/views/posts/_htmx_form.haml b/app/views/posts/_htmx_form.haml index 25c45aad..cd482b35 100644 --- a/app/views/posts/_htmx_form.haml +++ b/app/views/posts/_htmx_form.haml @@ -7,6 +7,7 @@ @param :dir [Symbol, String] :ruby options = { + id: base, multipart: true, class: 'form post ', 'hx-swap': params.require(:swap), @@ -29,13 +30,7 @@ %input{ type: 'hidden', name: 'name', value: params.require(:name) } %input{ type: 'hidden', name: 'base', value: params.require(:base) } - -# Botones de guardado - = render 'posts/submit', site: site, post: post - = hidden_field_tag "#{base}[layout]", post.layout.name -# Dibuja cada atributo = render 'posts/attributes', site: site, post: post, dir: dir, base: base, locale: locale - - -# Botones de guardado - = render 'posts/submit', site: site, post: post diff --git a/app/views/posts/attributes/_new_has_many.haml b/app/views/posts/attributes/_new_has_many.haml index 9382e2af..275d0335 100644 --- a/app/views/posts/attributes/_new_has_many.haml +++ b/app/views/posts/attributes/_new_has_many.haml @@ -90,3 +90,5 @@ = render 'bootstrap/modal', id: post_id, modal_content_attributes: { class: 'h-100' } do - content_for :"#{post_id}_body" do %div{ id: post_form_loaded_id } + - content_for :"#{post_id}_footer" do + = render 'bootstrap/btn', form: id, content: t('.save'), type: 'submit' diff --git a/config/locales/en.yml b/config/locales/en.yml index 9f4f7493..8fbfd536 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -944,3 +944,4 @@ en: cancel: "Cancel" edit: "Edit" filter: "Start typing to filter..." + save: "Save" diff --git a/config/locales/es.yml b/config/locales/es.yml index b85ac397..52ade5b2 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -952,3 +952,4 @@ es: cancel: "Cancelar" edit: "Editar" filter: "Empezá a escribir para filtrar..." + save: "Guardar"