Module: StimulusPlumbers::Plumber::HtmlOptions
- Extended by:
- ActiveSupport::Concern
- Included in:
- Form::Builder, Base
- Defined in:
- lib/stimulus_plumbers/plumber/html_options.rb
Constant Summary collapse
- STIMULUS_SPACEJOIN_KEYS =
%i[controller action].freeze
Instance Method Summary collapse
- #merge_data_options(*hashes, spacejoin: STIMULUS_SPACEJOIN_KEYS) ⇒ Object
- #merge_html_options(*hashes) ⇒ Object
- #merge_string_option(*parts, delimiter: " ") ⇒ Object
- #normalize_part(value, delimiter) ⇒ Object
Instance Method Details
#merge_data_options(*hashes, spacejoin: STIMULUS_SPACEJOIN_KEYS) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/stimulus_plumbers/plumber/html_options.rb', line 24 def (*hashes, spacejoin: STIMULUS_SPACEJOIN_KEYS) hashes.reduce({}) do |acc, d| acc.merge(d) do |key, old_val, new_val| if spacejoin.include?(key.to_sym) merge_string_option(old_val, new_val).presence || new_val else new_val end end end end |
#merge_html_options(*hashes) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/stimulus_plumbers/plumber/html_options.rb', line 10 def (*hashes) classes = hashes.flat_map { |h| [h[:class], h[:classes]] } data_hashes = hashes.map { |h| h[:data] || {} } rest = hashes.map { |h| h.except(:class, :classes, :data) }.reduce({}, :deep_merge) class_value = merge_string_option(*classes).presence merged_data = (*data_hashes) result = class_value ? rest.merge(class: class_value) : rest merged_data.present? ? result.merge(data: merged_data) : result end |
#merge_string_option(*parts, delimiter: " ") ⇒ Object
36 37 38 39 |
# File 'lib/stimulus_plumbers/plumber/html_options.rb', line 36 def merge_string_option(*parts, delimiter: " ") tokens = parts.flat_map { |part| normalize_part(part, delimiter) } tokens.compact.uniq.join(delimiter) end |
#normalize_part(value, delimiter) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/stimulus_plumbers/plumber/html_options.rb', line 41 def normalize_part(value, delimiter) case value when String then value.present? ? value.split(delimiter) : [] when Hash then value.filter_map { |key, val| key if val } when Array then [merge_string_option(*value).presence] else [] end end |