Module: Ariadne::AttributesHelper

Included in:
BaseComponent
Defined in:
app/lib/ariadne/attributes_helper.rb

Overview

:nodoc:

Constant Summary collapse

PLURAL_ARIA_ATTRIBUTES =
[:describedby, :labelledby].freeze
PLURAL_DATA_ATTRIBUTES =
[:target, :targets].freeze

Instance Method Summary collapse

Instance Method Details

#aria(html_attrs, val) ⇒ Object



23
24
25
# File 'app/lib/ariadne/attributes_helper.rb', line 23

def aria(html_attrs, val)
  html_attrs[:"aria-#{val}"] || html_attrs.dig(:aria, val.to_sym)
end

#data(html_attrs, val) ⇒ Object



27
28
29
# File 'app/lib/ariadne/attributes_helper.rb', line 27

def data(html_attrs, val)
  html_attrs[:"data-#{val}"] || html_attrs.dig(:data, val.to_sym)
end

#merge_aria(*hashes) ⇒ Object

Merges hashes that contain “aria-*” keys and nested aria: hashes. Removes keys from each hash and returns them in the new hash.

Eg. merge_aria({ “aria-disabled”: “true” }, { aria: { invalid: “true” } })

=> { disabled: "true", invalid: "true" }

Certain aria attributes can contain multiple values separated by spaces. merge_aria will combine these plural attributes into a composite string.

Eg. merge_aria({ “aria-labelledby”: “foo” }, { aria: { labelledby: “bar” } })

=> { labelledby: "foo bar" }

It’s designed to be used to normalize and merge aria information from system_arguments hashes. Consider using this pattern in component initializers:

@system_arguments = merge_aria(

@system_arguments,
{ aria: { labelled_by: id } }

)



50
51
52
53
54
# File 'app/lib/ariadne/attributes_helper.rb', line 50

def merge_aria(*hashes)
  merge_prefixed_attribute_hashes(
    *hashes, prefix: :aria, plural_keys: PLURAL_ARIA_ATTRIBUTES
  )
end

#merge_data(*hashes) ⇒ Object

Merges hashes that contain “data-*” keys and nested data: hashes. Removes keys from each hash and returns them in the new hash.

Eg. merge_data({ “data-foo”: “true” }, { data: { bar: “true” } })

=> { foo: "true", bar: "true" }

Certain data attributes can contain multiple values separated by spaces. merge_data will combine these plural attributes into a composite string.

Eg. merge_data({ “data-target”: “foo” }, { data: { target: “bar” } })

=> { target: "foo bar" }

It’s designed to be used to normalize and merge data information from system_arguments hashes. Consider using this pattern in component initializers:

@system_arguments = merge_aria(

@system_arguments,
{ data: { foo: "bar" } }

)



75
76
77
78
79
# File 'app/lib/ariadne/attributes_helper.rb', line 75

def merge_data(*hashes)
  merge_prefixed_attribute_hashes(
    *hashes, prefix: :data, plural_keys: PLURAL_DATA_ATTRIBUTES
  )
end

#merge_prefixed_attribute_hashes(*hashes, prefix:, plural_keys:) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/lib/ariadne/attributes_helper.rb', line 81

def merge_prefixed_attribute_hashes(*hashes, prefix:, plural_keys:)
  {}.tap do |result|
    hashes.each do |hash|
      next unless hash

      prefix_hash = hash.delete(prefix) || {}

      prefix_hash.each_pair do |key, val|
        result[key] =
          if plural_keys.include?(key)
            [*(result[key] || "").split, val].join(" ").strip
          else
            val
          end
      end

      hash.delete_if do |key, val|
        key_s = key.to_s

        if key.start_with?("#{prefix}-")
          bare_key = key_s.sub("#{prefix}-", "").to_sym

          result[bare_key] =
            if plural_keys.include?(bare_key)
              [*(result[bare_key] || "").split, val].join(" ").strip
            else
              val
            end

          true
        else
          false
        end
      end
    end
  end
end

#prepend_action(html_attrs, action) ⇒ Object



13
14
15
# File 'app/lib/ariadne/attributes_helper.rb', line 13

def prepend_action(html_attrs, action)
  prepend_data_attribute(html_attrs, :action, action)
end

#prepend_controller(html_attrs, name = stimulus_name) ⇒ Object



9
10
11
# File 'app/lib/ariadne/attributes_helper.rb', line 9

def prepend_controller(html_attrs, name = stimulus_name)
  prepend_data_attribute(html_attrs, :controller, name)
end

#prepend_data_attribute(html_attrs, attr_name, attr_value) ⇒ Object



17
18
19
20
21
# File 'app/lib/ariadne/attributes_helper.rb', line 17

def prepend_data_attribute(html_attrs, attr_name, attr_value)
  html_attrs[:data] ||= {}
  html_attrs[:data][attr_name] = "#{attr_value} #{html_attrs[:data][attr_name]}".strip
  html_attrs
end