lib/blacklight/configuration/fields.rb in blacklight-3.2.1 vs lib/blacklight/configuration/fields.rb in blacklight-3.2.2

- old
+ new

@@ -26,117 +26,114 @@ end END_EVAL end end - module InstanceMethods - # 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_solr_field :index_field, 'format', :label => 'Format' - # - # Alternative formats include: - # - # * a field name and block format: - # - # add_solr_field :index_field, 'format' do |field| - # field.label = 'Format' - # end - # - # * a plain block: - # - # add_solr_field :index_field do |field| - # field.field = 'format' - # field.label = 'Format' - # end - # - # * a configuration hash: - # - # add_solr_field :index_field, :field => 'format', :label => 'Format' - # - # * a Field instance: - # - # add_solr_field :index_field, IndexField.new(:field => 'format', :label => 'Format') - # - # * an array of hashes: - # - # add_solr_field :index_field, [{:field => 'format', :label => 'Format'}, IndexField.new(:field => 'date', :label => 'Date')] - # - # - # @param String config_key - # @param Array *args - # @para - # - def add_solr_field config_key, *args, &block - field_config = case args.first - when String - field_config_from_key_and_hash(config_key, *args) - when Symbol - args.first = args.first.to_s - field_config_from_key_and_hash(config_key, *args) - when Array - field_config_from_array(config_key, *args) - else - field_config_from_field_or_hash(config_key, *args) - 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_solr_field :index_field, 'format', :label => 'Format' + # + # Alternative formats include: + # + # * a field name and block format: + # + # add_solr_field :index_field, 'format' do |field| + # field.label = 'Format' + # end + # + # * a plain block: + # + # add_solr_field :index_field do |field| + # field.field = 'format' + # field.label = 'Format' + # end + # + # * a configuration hash: + # + # add_solr_field :index_field, :field => 'format', :label => 'Format' + # + # * a Field instance: + # + # add_solr_field :index_field, IndexField.new(:field => 'format', :label => 'Format') + # + # * an array of hashes: + # + # add_solr_field :index_field, [{:field => 'format', :label => 'Format'}, IndexField.new(:field => 'date', :label => 'Date')] + # + # + # @param String config_key + # @param Array *args + # @para + # + def add_solr_field config_key, *args, &block + field_config = case args.first + when String + field_config_from_key_and_hash(config_key, *args) + when Symbol + args.first = args.first.to_s + field_config_from_key_and_hash(config_key, *args) + when Array + field_config_from_array(config_key, *args) + else + field_config_from_field_or_hash(config_key, *args) + end - return if field_config.is_a? Array - - if block_given? - yield field_config - end - - field_config.normalize!(self) - field_config.validate! - - self[config_key.pluralize][ field_config.field ] = field_config + return if field_config.is_a? Array + + if block_given? + yield field_config end + + field_config.normalize!(self) + field_config.validate! - protected - # Add a solr field by a solr field name and hash - def field_config_from_key_and_hash config_key, solr_field, field_or_hash = {} - field_config = field_config_from_field_or_hash(config_key, field_or_hash) - field_config.field = solr_field + self[config_key.pluralize][ field_config.field ] = field_config + end - field_config - end + protected + # Add a solr field by a solr field name and hash + def field_config_from_key_and_hash config_key, solr_field, field_or_hash = {} + field_config = field_config_from_field_or_hash(config_key, field_or_hash) + field_config.field = solr_field - # Add multiple solr fields using a hash or Field instance - def field_config_from_array config_key, array_of_fields_or_hashes - array_of_fields_or_hashes.map do |field_or_hash| - add_solr_field(config_key, field_or_hash) - end - end + field_config + end - # Add a solr field using a hash or Field instance - def field_config_from_field_or_hash config_key, field_or_hash = {} - hash_arg_to_config(field_or_hash, field_class_from_key(config_key)) + # Add multiple solr fields using a hash or Field instance + def field_config_from_array config_key, array_of_fields_or_hashes + array_of_fields_or_hashes.map do |field_or_hash| + add_solr_field(config_key, field_or_hash) end + end - # 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. - def hash_arg_to_config(hash_arg, klass) - case hash_arg - when Hash - klass.new(hash_arg) - when NilClass - klass.new - 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. - hash_arg - end - end + # Add a solr field using a hash or Field instance + def field_config_from_field_or_hash config_key, field_or_hash = {} + hash_arg_to_config(field_or_hash, field_class_from_key(config_key)) + end - private - # convert a config key to the appropriate Field class - def field_class_from_key key - "Blacklight::Configuration::#{key.camelcase}".constantize + # 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. + def hash_arg_to_config(hash_arg, klass) + case hash_arg + when Hash + klass.new(hash_arg) + when NilClass + klass.new + 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. + 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 end end end