From 1e13fc462102378f88963bf723ad55bad5a7d550 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 23 May 2024 17:47:02 -0300 Subject: [PATCH] =?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}"