Skip to content

Commit

Permalink
Add support for dynamic field configuration for index, show and facet
Browse files Browse the repository at this point in the history
fields, allowing configurations like:

```ruby
config.add_facet_field '*_facet'
config.add_show_field '*_display'
config.add_index_field 'title_*'
```
  • Loading branch information
cbeer committed Feb 10, 2014
1 parent 60c7ee6 commit 0827368
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
36 changes: 35 additions & 1 deletion lib/blacklight/configuration/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,29 @@ def add_solr_field config_key, *args, &block
field_config_from_key_and_hash(config_key, *args)
when Array
field_config_from_array(config_key, *args)
return # we've iterated over the array above.
else
field_config_from_field_or_hash(config_key, *args)
end

return if field_config.is_a? Array
# look up any dynamic fields
if field_config.field.to_s =~ /\*/ and luke_fields
salient_fields = luke_fields.select do |k,v|
k =~ Regexp.new("^" + field_config.field.to_s.gsub('*', '.+') + "$")
end

salient_fields.each do |field, luke_config|
config = field_config.dup
config.field = field
if self[config_key.pluralize][ config.field ]
self[config_key.pluralize][ config.field ] = config.merge(self[config_key.pluralize][ config.field ])
else
add_solr_field(config_key, config)
end
end

return
end

if block_given?
yield field_config
Expand All @@ -94,6 +112,22 @@ def add_solr_field config_key, *args, &block
end

protected
def luke_fields
if @table[:luke_fields] === false
return nil
end

@table[:luke_fields] ||= begin
if has_key? :blacklight_solr
blacklight_solr.get('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
end
rescue
false
end

@table[:luke_fields] || nil
end

# 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)
Expand Down
35 changes: 34 additions & 1 deletion spec/lib/blacklight/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,18 @@
it "should raise on nil solr field name" do
expect { @config.add_facet_field(nil) }.to raise_error ArgumentError
end


it "should take wild-carded field names and dereference them to solr fields" do
@config.stub(luke_fields: {
"some_field_facet" => {},
"another_field_facet" => {},
"a_facet_field" => {},
})
@config.add_index_field "*_facet"

expect(@config.index_fields.keys).to eq ["some_field_facet", "another_field_facet"]
end

end

describe "add_index_field" do
Expand Down Expand Up @@ -201,6 +212,17 @@
expect { @config.add_index_field(nil) }.to raise_error ArgumentError
end

it "should take wild-carded field names and dereference them to solr fields" do
@config.stub(luke_fields: {
"some_field_display" => {},
"another_field_display" => {},
"a_facet_field" => {},
})
@config.add_index_field "*_display"

expect(@config.index_fields.keys).to eq ["some_field_display", "another_field_display"]
end

end

describe "add_show_field" do
Expand Down Expand Up @@ -235,6 +257,17 @@
expect { @config.add_show_field(nil) }.to raise_error ArgumentError
end

it "should take wild-carded field names and dereference them to solr fields" do
@config.stub(luke_fields: {
"some_field_display" => {},
"another_field_display" => {},
"a_facet_field" => {},
})
@config.add_show_field "*_display"

expect(@config.show_fields.keys).to eq ["some_field_display", "another_field_display"]
end

end


Expand Down

0 comments on commit 0827368

Please sign in to comment.