Sha256: 1d5339af159ac49fb3f6324c5c95e0c7706b06a9184c5f8fb2d3f10078a4bea5

Contents?: true

Size: 1.93 KB

Versions: 21

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module CCS
  # Components for the GOV.UK Frontend and CCS Frontend using an Object Oriented pattern
  # These are then used in the {CCS::FrontendHelpers FrontendHelpers}
  # to create view helpers that can be used in a Ruby on Rails project

  module Components
    # Component base class that all components will inherit from.
    #
    # It's main purpose is to handle the initialisation of the options
    # which is very similar between components
    #
    # @!attribute [r] options
    #   @return [Hash] Options for the component which defaults to +{ attributes: {} }+
    #                  where +attributes+ are HTML attributes
    # @!attribute [r] context
    #   @return [ActionView::Base] View context where the components are being rendered

    class Base
      private

      attr_accessor :options, :context

      public

      delegate :button_tag, :concat, :label_tag, :link_to, :tag, to: :context

      # @param context [ActionView::Base] the view context where the components are being rendered
      # @param options [Hash] options for the component

      def initialize(context:, **options)
        options[:attributes] ||= {}
        options[:attributes][:class] = "#{default_attributes[:class]} #{options[:classes]}".strip if default_attributes[:class] || options[:classes]
        (options[:attributes][:data] ||= {})[:module] = default_attributes[:data][:module] if default_attributes.dig(:data, :module)

        @context = context
        @options = options
      end

      # The default attributes for the component

      DEFAULT_ATTRIBUTES = {}.freeze

      private

      # The default attributes for the component
      #
      # @return [Hash]

      def default_attributes
        self.class::DEFAULT_ATTRIBUTES
      end

      # Sanitizes string used as HTML tag id
      #
      # @return [String]

      def sanitize_to_id(...)
        @context.send(:sanitize_to_id, ...)
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
ccs-frontend_helpers-2.1.0 lib/ccs/components/base.rb
ccs-frontend_helpers-2.0.0 lib/ccs/components/base.rb
ccs-frontend_helpers-1.2.0 lib/ccs/components/base.rb
ccs-frontend_helpers-1.1.2 lib/ccs/components/base.rb
ccs-frontend_helpers-1.1.1 lib/ccs/components/base.rb
ccs-frontend_helpers-1.1.0 lib/ccs/components/base.rb
ccs-frontend_helpers-1.1.0.beta0 lib/ccs/components/base.rb
ccs-frontend_helpers-1.0.0 lib/ccs/components/base.rb
ccs-frontend_helpers-0.5.0.beta1 lib/ccs/components/base.rb
ccs-frontend_helpers-0.5.0.beta0 lib/ccs/components/base.rb
ccs-frontend_helpers-0.3.0 lib/ccs/components/base.rb
ccs-frontend_helpers-0.2.0 lib/ccs/components/base.rb
ccs-frontend_helpers-0.1.2 lib/ccs/components/base.rb
ccs-frontend_helpers-0.1.1 lib/ccs/components/base.rb
ccs-frontend_helpers-0.1.1.rc.1 lib/ccs/components/base.rb
ccs-frontend_helpers-0.1.0.rc.7 lib/ccs/components/base.rb
ccs-frontend_helpers-0.1.0.rc.6 lib/ccs/components/base.rb
ccs-frontend_helpers-0.1.0.rc.5 lib/ccs/components/base.rb
ccs-frontend_helpers-0.1.0.rc.4 lib/ccs/components/base.rb
ccs-frontend_helpers-0.1.0.rc.3 lib/ccs/components/base.rb