From f61bbac193821b57a469e9d175ec8eaa30f0d02d Mon Sep 17 00:00:00 2001 From: Brychu Date: Sun, 19 Nov 2023 00:51:50 +0100 Subject: [PATCH] Refactor - lower method complexity in HasHTMLAttributes --- lib/avo/concerns/has_html_attributes.rb | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/avo/concerns/has_html_attributes.rb b/lib/avo/concerns/has_html_attributes.rb index c35290e183..88ca19f61c 100644 --- a/lib/avo/concerns/has_html_attributes.rb +++ b/lib/avo/concerns/has_html_attributes.rb @@ -27,7 +27,10 @@ def get_html(name = nil, element:, view:) default_attribute_value name end - add_default_data_attributes attributes, name, element, view + add_action_data_attributes(attributes, name, element) + add_resource_data_attributes(attributes, name, element, view) + + attributes end private @@ -47,14 +50,17 @@ def default_attribute_value(name) name == :data ? {} : "" end - def add_default_data_attributes(attributes, name, element, view) - if !attributes.nil? && name == :data && element == :input && view.in?([:edit, :new]) && resource.present? && resource.respond_to?(:get_stimulus_controllers) + def add_action_data_attributes(attributes, name, element) + if can_add_stimulus_attributes_for?(action, attributes, name, element) + attributes.merge!(stimulus_attributes_for(action)) + end + end + + def add_resource_data_attributes(attributes, name, element, view) + if can_add_stimulus_attributes_for?(resource, attributes, name, element) && view.in?([:edit, :new]) resource_stimulus_attributes = stimulus_attributes_for(resource) - action_stimulus_attributes = action ? stimulus_attributes_for(action) : {} - attributes.merge(resource_stimulus_attributes, action_stimulus_attributes) - else - attributes + attributes.merge!(resource_stimulus_attributes) end end @@ -100,6 +106,10 @@ def merge_values_as(as: :array, values: []) result if result.present? end + def can_add_stimulus_attributes_for?(entity, attributes, name, element) + !attributes.nil? && name == :data && element == :input && entity.present? && entity.respond_to?(:get_stimulus_controllers) + end + def stimulus_attributes_for(entity) entity.get_stimulus_controllers .split(" ")