Skip to content

Commit

Permalink
Refactor - lower method complexity in HasHTMLAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
bryszard committed Nov 19, 2023
1 parent 8b0b325 commit f61bbac
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/avo/concerns/has_html_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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(" ")
Expand Down

0 comments on commit f61bbac

Please sign in to comment.