Sha256: 106177720d6fea5677badb83e426ffd42b6fb5336cd8d15d289d5f46c93156a4

Contents?: true

Size: 1.87 KB

Versions: 4

Compression:

Stored size: 1.87 KB

Contents

# typed: false
# frozen_string_literal: true

require "action_view/helpers/tag_helper"

module ViewComponentContrib
  module HTMLAttrs
    class AttributesHash < ::Hash
      TAG_BUILDER = ActionView::Helpers::TagHelper::TagBuilder.new(nil)
      TAG_OPTIONS = ActionView::Helpers::TagHelper::TagBuilder.instance_method(:tag_options)

      def to_html(nested: false)
        # TODO: implement + change usage by @tag nested-attributes
        tag_options(self)&.html_safe # rubocop:disable Rails/OutputSafety
      end
      private def tag_options(...) = TAG_OPTIONS.bind_call(TAG_BUILDER, ...)
    end

    extend ActiveSupport::Concern

    class_methods do
      def accepts_html_attributes_for(name, **defaults, &block)
        html_attribute_accessors[name] = defaults

        name = name.to_sym
        method_name = :"#{name}_attrs"
        ivar_name = :"@#{method_name}"

        attr_reader(method_name)

        mod = Module.new do
          define_method(:initialize) do |**options|
            value = self.class.default_html_attributes_for(name).deep_merge!(options.delete(method_name).to_h)
            super(**options)
            instance_exec(value, &block) if block
            instance_variable_set(ivar_name, value)
          end
        end

        prepend(mod)
      end

      def accepts_html_attributes(**defaults, &block)
        accepts_html_attributes_for(:html, **defaults, &block)
      end

      def html_attribute_accessors
        @html_attribute_accessors ||=
          if superclass.respond_to?(:html_attribute_accessors)
            superclass.html_attribute_accessors.deep_dup
          else
            {}
          end
      end

      def default_html_attributes_for(name)
        html_attribute_accessors.fetch(name).each_with_object(AttributesHash.new) do |(k, v), acc|
          acc[k] = v.is_a?(Proc) ? v.call : v
          acc
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.67 app/lib/view_component_contrib/html_attrs.rb
ariadne_view_components-0.0.66 app/lib/view_components_contrib/html_attrs.rb
ariadne_view_components-0.0.65 app/lib/view_components_contrib/html_attrs.rb
ariadne_view_components-0.0.64 app/lib/view_components_contrib/html_attrs.rb