Sha256: 2834427d847951f22c897fa7e26f4f5b0fbce0cecc05556cfc3f3a42772b4739

Contents?: true

Size: 1.05 KB

Versions: 147

Compression:

Stored size: 1.05 KB

Contents

module GovukPublishingComponents
  module Presenters
    class SelectHelper
      attr_reader :options, :option_markup, :selected_option

      def initialize(options)
        @options = options
        @option_markup = get_options
      end

      def data_tracking?
        @options.any? { |item| item[:data_attributes] && item[:data_attributes][:track_category] && item[:data_attributes][:track_action] }
      end

    private

      def get_options
        return if options.nil?

        options.map { |option|
          @selected_option = option[:value] if option[:selected]
          [
            option[:text],
            option[:value],
            options_data_attribute(option[:data_attributes])
          ]
        }
      end

      def options_data_attribute(attributes)
        return {} if attributes.nil?

        attrs = {}
        attributes.each do |key, value|
          key_name = "data-#{key.to_s.split('_').join('-')}"
          attrs[key_name] = value.is_a?(Hash) ? value.to_json : value
        end

        attrs
      end
    end
  end
end

Version data entries

147 entries across 147 versions & 1 rubygems

Version Path
govuk_publishing_components-17.13.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.12.2 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.12.1 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.12.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.11.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.10.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.9.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.8.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.7.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.6.1 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.6.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.5.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.4.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.3.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.2.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.1.1 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.1.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-17.0.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-16.29.0 lib/govuk_publishing_components/presenters/select.rb
govuk_publishing_components-16.28.0 lib/govuk_publishing_components/presenters/select.rb