Skip to content

Commit

Permalink
Clean up Customizable module
Browse files Browse the repository at this point in the history
  • Loading branch information
josefarias committed Mar 9, 2024
1 parent 6a5c98d commit e265999
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
24 changes: 5 additions & 19 deletions app/presenters/hotwire_combobox/component/customizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/presenters/hotwire_combobox/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e265999

Please sign in to comment.