From e265999bc7ac1a9b788870fcf8d97bf274443da6 Mon Sep 17 00:00:00 2001 From: Jose Farias Date: Fri, 8 Mar 2024 18:02:04 -0600 Subject: [PATCH] Clean up Customizable module --- .../component/customizable.rb | 24 ++++--------------- .../hotwire_combobox/component_test.rb | 4 ++-- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/app/presenters/hotwire_combobox/component/customizable.rb b/app/presenters/hotwire_combobox/component/customizable.rb index 7593db4..142a189 100644 --- a/app/presenters/hotwire_combobox/component/customizable.rb +++ b/app/presenters/hotwire_combobox/component/customizable.rb @@ -33,30 +33,16 @@ def custom_attrs end def customize(element, **attrs) - element = element.to_sym.presence_in(CUSTOMIZABLE_ELEMENTS) || - raise(ArgumentError, <<~MSG) - [ACTION NEEDED] – Message from HotwireCombobox: + element = element.to_sym.presence_in(CUSTOMIZABLE_ELEMENTS) + sanitized_attrs = attrs.deep_symbolize_keys.except(*PROTECTED_ATTRS) - You tried to customize an element called `#{element}`, but - HotwireCombobox does not recognize that element. - - Please choose one of the valid elements: #{CUSTOMIZABLE_ELEMENTS.join(", ")}. - MSG - - custom_attrs[element] = attrs.deep_symbolize_keys.delete_if do |key, _| - PROTECTED_ATTRS.include? key - end + custom_attrs.store element, sanitized_attrs end def apply_customizations_to(element, base: {}) custom = custom_attrs[element] - default = base.deep_symbolize_keys.map do |key, value| - if value.is_a?(String) || value.is_a?(Symbol) - [ key, view.token_list(value.to_s, custom.delete(key)) ] - else - [ key, value ] - end - end.to_h + coalesce = ->(k, v) { v.is_a?(String) ? view.token_list(v, custom.delete(k)) : v } + default = base.map { |k, v| [ k, coalesce.(k, v) ] }.to_h custom.deep_merge default end diff --git a/test/presenters/hotwire_combobox/component_test.rb b/test/presenters/hotwire_combobox/component_test.rb index 27140e3..e8218ae 100644 --- a/test/presenters/hotwire_combobox/component_test.rb +++ b/test/presenters/hotwire_combobox/component_test.rb @@ -3,13 +3,13 @@ class HotwireCombobox::ComponentTest < ApplicationViewTestCase test "native html autocomplete is off by default" do HotwireCombobox::Component.new(view, :foo).tap do |component| - assert_equal "off", component.input_attrs[:autocomplete] + assert_equal "off", component.input_attrs[:autocomplete].to_s end end test "native html autocomplete can be turned on" do HotwireCombobox::Component.new(view, :foo, input: { autocomplete: :on }).tap do |component| - assert_equal "on", component.input_attrs[:autocomplete] + assert_equal "on", component.input_attrs[:autocomplete].to_s end end