Sha256: 0ea24e1f4966b6a7f3b65d2d508b69c096fd688bee32eff12eabe5e9cc0e025b

Contents?: true

Size: 1.91 KB

Versions: 24

Compression:

Stored size: 1.91 KB

Contents

module Spotlight
  ##
  # Helpers to provide default blacklight configuration values
  module BlacklightConfigurationDefaults
    extend ActiveSupport::Concern

    included do
      before_create :setup_defaults
      attr_accessor :skip_default_configuration
    end

    protected

    def setup_defaults
      return if skip_default_configuration

      default_search_fields
      default_sort_fields
      default_view_types
      set_default_per_page
    end

    def default_search_fields
      return true unless search_fields.empty?

      # can't use default_blacklight_config until after the BlacklightConfiguration
      # is created or we run into a circular dependency.
      default_fields = Spotlight::Engine.blacklight_config.search_fields
      self.search_fields = default_fields.each_with_object({}) do |(k, v), obj|
        obj[k] = { enabled: v.fetch(:enabled, true) }
      end
    end

    def default_sort_fields
      return true unless sort_fields.empty?

      # can't use default_blacklight_config until after the BlacklightConfiguration
      # is created or we run into a circular dependency.
      default_fields = Spotlight::Engine.blacklight_config.sort_fields
      self.sort_fields = default_fields.each_with_object({}) do |(k, v), obj|
        obj[k] = { enabled: v.fetch(:enabled, true) }
      end
    end

    def default_view_types
      return true unless document_index_view_types.empty?

      # can't use default_blacklight_config until after the BlacklightConfiguration
      # is created or we run into a circular dependency.
      self.document_index_view_types = Spotlight::Engine.blacklight_config.view.keys.map(&:to_s)
    end

    def set_default_per_page
      # can't use default_blacklight_config until after the BlacklightConfiguration
      # is created or we run into a circular dependency.
      self.default_per_page ||= Spotlight::Engine.blacklight_config.per_page.first
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
blacklight-spotlight-2.4.1 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.4.0 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.3.3 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.3.2 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.3.1 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.3.0 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.2.1 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.2.0 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.1.0 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.0.2 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.0.1 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.0.0 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.0.0.rc6 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.0.0.rc5 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.0.0.rc4 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.0.0.rc3 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.0.0.rc2 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-2.0.0.rc1 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-1.5.1 app/models/concerns/spotlight/blacklight_configuration_defaults.rb
blacklight-spotlight-1.4.1 app/models/concerns/spotlight/blacklight_configuration_defaults.rb