Sha256: 202b20787146c4dae5def6c674289d0985213e0607a8705dfa1c2bfc0d437775

Contents?: true

Size: 1.39 KB

Versions: 48

Compression:

Stored size: 1.39 KB

Contents

module Coco
  module Concerns
    module AcceptsTagAttributes
      extend ActiveSupport::Concern

      TAG_ATTRIBUTE_NAMES = %i[id class data aria test_data x].freeze

      class_methods do
        def tag_name(name = nil)
          @_tag_name = name if name
          @_tag_name
        end

        def tag_attr(*)
          tag_attr_names.push(*)
        end

        def tag_attr_names
          @_tag_attr_names ||= [*TAG_ATTRIBUTE_NAMES]
        end
      end

      def tag_attrs
        @_tag_attrs ||= {}
      end

      protected

      def tag_name(name = nil)
        @tag_name = name if name
        @tag_name ||= self.class.tag_name
      end

      def tag_name=(name)
        @tag_name = name
      end

      def set_tag_attr(attr_name, value = "")
        tag_attrs[attr_name] = value
      end

      def set_tag_data_attr(attr_name, value = "")
        tag_attrs[:data] ||= {}
        tag_attrs[:data][attr_name] = value
      end

      def tag_attr?(attr_name)
        tag_attrs.key?(attr_name)
      end

      def tag_attr(attr_name, fallback = nil)
        tag_attrs.fetch(attr_name, fallback)
      end

      private

      def process_tag_attrs(args)
        @_tag_attrs ||= {}
        @_tag_attrs.merge!(args[:html].to_h)
        @_tag_attrs.merge!(args.slice(*self.class.tag_attr_names))
        @_component_args = args.except(*self.class.tag_attr_names, :html)
      end
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
coveragebook_components-0.19.8 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.19.7 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.19.6 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.19.5 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.19.4 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.19.3 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.19.2 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.19.1 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.19.0 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.18.8 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.18.7 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.18.0 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.17.7 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.17.6 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.17.5 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.17.4 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.17.3 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.17.2 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.17.1 app/components/coco/concerns/accepts_tag_attributes.rb
coveragebook_components-0.17.0 app/components/coco/concerns/accepts_tag_attributes.rb