lib/blacklight/configuration/fields.rb in blacklight-6.18.0 vs lib/blacklight/configuration/fields.rb in blacklight-6.19.0

- old
+ new

@@ -3,10 +3,12 @@ class Configuration # This mixin provides Blacklight::Configuration with generic # solr fields configuration module Fields extend ActiveSupport::Concern + extend Deprecation + self.deprecation_horizon = 'blacklight version 8.0.0' module ClassMethods # Add a configuration block for a collection of solr fields def define_field_access(key, options = {}) key = key.to_s if respond_to? :to_s @@ -18,11 +20,11 @@ unless self.const_defined? key.camelcase class_eval <<-END_EVAL, __FILE__, __LINE__ + 1 class #{key.camelcase} < #{base_class_name}; end END_EVAL end - + class_eval <<-END_EVAL, __FILE__, __LINE__ + 1 def add_#{key}(*args, &block) add_blacklight_field("#{key}", *args, &block) end END_EVAL @@ -30,11 +32,11 @@ end # Add a solr field configuration to the given configuration key # # The recommended and strongly encouraged format is a field name, configuration pair, e.g.: - # add_blacklight_field :index_field, 'format', :label => 'Format' + # add_blacklight_field :index_field, 'format', :label => 'Format' # # Alternative formats include: # # * a field name and block format: # @@ -54,29 +56,29 @@ # # add_blacklight_field :index_field do |field| # field.field = 'format' # field.label = 'Format' # end - # + # # * a configuration hash: # # @overload add_blacklight_field(config_key, options) # @param [Symbol] config_key # @param [Hash] options # # add_blacklight_field :index_field, :field => 'format', :label => 'Format' - # - # * a Field instance: # + # * a Field instance: + # # @overload add_blacklight_field(config_key, field) # @param [Symbol] config_key # @param [Blacklight::Configuration::Field] field # # # add_blacklight_field :index_field, IndexField.new(:field => 'format', :label => 'Format') # - # * an array of hashes: + # * an array of hashes: # # @overload add_blacklight_field(config_key, fields) # @param [Symbol] config_key # @param [Array<Blacklight::Configuration::Field, Hash>] fields # @@ -100,75 +102,78 @@ field_config.match = Regexp.new("^" + (field_config.field || field_config.key).to_s.gsub('*', '.+') + "$") end # look up any dynamic fields if field_config.match - - salient_fields = luke_fields.select do |k,v| - k =~ field_config.match - end - - salient_fields.each do |field, luke_config| - config = field_config.dup - config.match = nil - config.field = field - config.key = field - - if self[config_key.pluralize][ config.key ] - self[config_key.pluralize][ config.key ] = config.merge(self[config_key.pluralize][ config.key ]) - else - add_blacklight_field(config_key, config, &block) - end - end - + handle_matching_fields(config_key, field_config, &block) return end - + if block_given? yield field_config end - + field_config.normalize!(self) field_config.validate! raise "A #{config_key} with the key #{field_config.key} already exists." if self[config_key.pluralize][field_config.key].present? self[config_key.pluralize][ field_config.key ] = field_config end protected - def luke_fields - if @table[:luke_fields] == false + ## + # Using reflection into the index, add any fields in the index that match the field_config + def handle_matching_fields(config_key, field_config, &block) + salient_fields = reflected_fields.select do |k, _v| + k =~ field_config.match + end + + salient_fields.each_key do |field| + config = field_config.dup + config.match = nil + config.field = field + config.key = field + if self[config_key.pluralize][config.key] + self[config_key.pluralize][config.key] = config.merge(self[config_key.pluralize][config.key]) + else + add_blacklight_field(config_key, config, &block) + end + end + end + + def reflected_fields + if @table[:reflected_fields] == false return nil end - @table[:luke_fields] ||= Rails.cache.fetch("blacklight_configuration/admin/luke", expires_in: 1.hour) do + @table[:reflected_fields] ||= Rails.cache.fetch("blacklight_configuration/admin/reflected_fields", expires_in: 1.hour) do begin - if repository_class <= Blacklight::Solr::Repository - repository = repository_class.new(self) - repository.send_and_receive('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields'] - end + repository = repository_class.new(self) + repository.reflect_fields rescue => e Blacklight.logger.warn "Error retrieving field metadata: #{e}" false end end - @table[:luke_fields] || {} + @table[:reflected_fields] || {} end + alias luke_fields reflected_fields + deprecation_deprecate luke_fields: 'use reflected_fields instead' - # Add a solr field by a solr field name and hash + # Add a solr field by a solr field name and hash def field_config_from_key_and_hash config_key, field_name, field_or_hash = {} field_config = field_config_from_field_or_hash(config_key, field_or_hash) field_config.key = field_name field_config end # Add multiple solr fields using a hash or Field instance def field_config_from_array config_key, array_of_fields_or_hashes, &block - array_of_fields_or_hashes.map do |field_or_hash| + array_of_fields_or_hashes.map do |field_or_hash| add_blacklight_field(config_key, field_or_hash, &block) end end # Add a solr field using a hash or Field instance @@ -178,19 +183,19 @@ # for our add_* methods, takes the optional hash param, # and makes it into a specific config OpenStruct, like # FacetField or SearchField. Or if the param already was # one, that's cool. Or if the param is nil, make - # an empty one. Second argument is an actual class object. + # an empty one. Second argument is an actual class object. def hash_arg_to_config(hash_arg, klass) case hash_arg - when Hash + when Hash klass.new(hash_arg) - when NilClass + when NilClass klass.new - else + else # this assumes it already is an element of klass, or acts like one, - # if not something bad will happen later, that's your problem. + # if not something bad will happen later, that's your problem. hash_arg end end private