Sha256: 1234d9d62e1113d7c72b2e2a1cd236d7e9825a59ea45f3060363044c53adc176

Contents?: true

Size: 1.94 KB

Versions: 21

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Blacklight
  class Configuration
    # This class helps determine whether a specific field/tool should display for a
    # particular controller.  This is used when the field/tool is configured with an
    # _if_ or _unless_ argument.
    #
    # e.g.
    #   config.add_results_document_tool(:bookmark,
    #                                    partial: 'bookmark_control',
    #                                    if: :render_bookmarks_control?)
    #
    # The context points at the scope for where to evaluate the method _render_bookmarks_control?_
    class Context
      attr_reader :context

      def initialize(context)
        @context = context
      end

      ##
      # Evaluate conditionals for a configuration with if/unless attributes
      #
      # @param [#if,#unless] config an object that responds to if/unless
      # @return [Boolean]
      def evaluate_if_unless_configuration(config, *args)
        return config if config == true || config == false

        if_value = !config.respond_to?(:if) ||
                   config.if.nil? ||
                   evaluate_configuration_conditional(config.if, config, *args)

        unless_value = !config.respond_to?(:unless) ||
                       config.unless.nil? ||
                       !evaluate_configuration_conditional(config.unless, config, *args)

        if_value && unless_value
      end

      def evaluate_configuration_conditional(proc_helper_or_boolean, *args_for_procs_and_methods)
        case proc_helper_or_boolean
        when Symbol
          arity = context.method(proc_helper_or_boolean).arity

          if arity.zero?
            context.send(proc_helper_or_boolean)
          else
            context.send(proc_helper_or_boolean, *args_for_procs_and_methods)
          end
        when Proc
          proc_helper_or_boolean.call context, *args_for_procs_and_methods
        else
          proc_helper_or_boolean
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
blacklight-8.6.1 lib/blacklight/configuration/context.rb
blacklight-8.6.0 lib/blacklight/configuration/context.rb
blacklight-8.5.1 lib/blacklight/configuration/context.rb
blacklight-8.5.0 lib/blacklight/configuration/context.rb
blacklight-8.4.0 lib/blacklight/configuration/context.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/blacklight-8.3.0/lib/blacklight/configuration/context.rb
blacklight-8.3.0 lib/blacklight/configuration/context.rb
blacklight-8.2.2 lib/blacklight/configuration/context.rb
blacklight-8.2.1 lib/blacklight/configuration/context.rb
blacklight-8.2.0 lib/blacklight/configuration/context.rb
blacklight-8.1.0 lib/blacklight/configuration/context.rb
blacklight-8.0.1 lib/blacklight/configuration/context.rb
blacklight-8.0.0 lib/blacklight/configuration/context.rb
blacklight-8.0.0.beta8 lib/blacklight/configuration/context.rb
blacklight-8.0.0.beta7 lib/blacklight/configuration/context.rb
blacklight-8.0.0.beta6 lib/blacklight/configuration/context.rb
blacklight-8.0.0.beta5 lib/blacklight/configuration/context.rb
blacklight-8.0.0.beta4 lib/blacklight/configuration/context.rb
blacklight-8.0.0.beta3 lib/blacklight/configuration/context.rb
blacklight-8.0.0.beta2 lib/blacklight/configuration/context.rb