Sha256: 6aed1a0d644dc1cf31f0558c7d322ab3602d2efccc81ab47857fecc33e4bbcb4

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module Solder
  module ApplicationHelper
    def solder_onto(name, touch: [], attribute_safelist: ["class"], &block)
      soldered_html = capture(&block).to_s.strip
      fragment = Nokogiri::HTML.fragment(soldered_html)

      first_fragment_child = fragment.first_element_child

      # rehydrate
      ui_state = Rails.cache.read "solder/#{solder_key(name)}"

      ui_state&.select { attribute_safelist.include?(_1) }&.each do |attribute_name, value|
        first_fragment_child[attribute_name] = sanitize value
      end

      # add stimulus controller and create unique key
      first_fragment_child["data-controller"] = "#{first_fragment_child["data-controller"]} solder".strip
      first_fragment_child["data-solder-key-value"] = solder_key(name)

      first_fragment_child["data-solder-touch"] ||= Array(touch).map(&:to_sgid).map(&:to_s).join(":")

      first_fragment_child.to_html.html_safe
    end

    def solder_key(name)
      key = cache_fragment_name(name, skip_digest: true)
        .flatten
        .compact
        .map(&:cache_key)
        .join(":")

      key += ":#{caller.find { _1 =~ /html/ }}"

      ActiveSupport::Digest.hexdigest(key)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solder-0.2.0 app/helpers/solder/application_helper.rb~