lib/blacklight/configuration/fields.rb in blacklight-6.25.0 vs lib/blacklight/configuration/fields.rb in blacklight-7.0.0.rc1

- old
+ new

@@ -3,23 +3,21 @@ 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 - self.default_values[key.pluralize.to_sym] = ActiveSupport::OrderedHash.new + default_values[key.pluralize.to_sym] = ActiveSupport::OrderedHash.new base_class_name = options.fetch(:class, Field) - unless self.const_defined? key.camelcase + unless const_defined? key.camelcase class_eval <<-END_EVAL, __FILE__, __LINE__ + 1 class #{key.camelcase} < #{base_class_name}; end END_EVAL end @@ -84,29 +82,46 @@ # # add_blacklight_field :index_field, [{ :field => 'format', :label => 'Format' }, IndexField.new(:field => 'date', :label => 'Date')] # def add_blacklight_field config_key, *args, &block field_config = case args.first - when String - field_config_from_key_and_hash(config_key, *args) - when Symbol - args[0] = args[0].to_s - field_config_from_key_and_hash(config_key, *args) - when Array - field_config_from_array(config_key, *args, &block) - return # we've iterated over the array above. - else - field_config_from_field_or_hash(config_key, *args) - end + when String + field_config_from_key_and_hash(config_key, *args) + when Symbol + args[0] = args[0].to_s + field_config_from_key_and_hash(config_key, *args) + when Array + field_config_from_array(config_key, *args, &block) + return # we've iterated over the array above. + else + field_config_from_field_or_hash(config_key, *args) + end if (field_config.field || field_config.key).to_s =~ /\*/ field_config.match = Regexp.new("^" + (field_config.field || field_config.key).to_s.gsub('*', '.+') + "$") end # look up any dynamic fields if field_config.match - handle_matching_fields(config_key, field_config, &block) + + salient_fields = luke_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 + return end if block_given? yield field_config @@ -115,54 +130,34 @@ 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 + self[config_key.pluralize][field_config.key] = field_config end - protected + private - ## - # 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 + def luke_fields + if @table[:luke_fields] == false return nil end - @table[:reflected_fields] ||= Rails.cache.fetch("blacklight_configuration/admin/reflected_fields", expires_in: 1.hour) do + @table[:luke_fields] ||= Rails.cache.fetch("blacklight_configuration/admin/luke", expires_in: 1.hour) do begin - repository = repository_class.new(self) - repository.reflect_fields + if repository_class <= Blacklight::Solr::Repository + repository = repository_class.new(self) + repository.send_and_receive('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields'] + end rescue => e Blacklight.logger.warn "Error retrieving field metadata: #{e}" false end end - @table[:reflected_fields] || {} + @table[:luke_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 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 @@ -196,11 +191,9 @@ # this assumes it already is an element of klass, or acts like one, # if not something bad will happen later, that's your problem. hash_arg end end - - private # convert a config key to the appropriate Field class def field_class_from_key key "Blacklight::Configuration::#{key.camelcase}".constantize end