Skip to content

Commit dbd64b9

Browse files
Merge pull request #128 from MITLibraries/gdt-128-filters-list
Expand available filters, give applications the ability to filter and order via ENV
2 parents 0cc75ed + 6363a55 commit dbd64b9

26 files changed

+358
-220
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ change as part of the work.
5151

5252
- `ABOUT_APP`: If populated, an 'about' partial containing the contents of this variable will render on
5353
`basic_search#index`.
54+
- `ACTIVE_FILTERS`: If populated, this list of strings defines which filters are shown to the user, and the order in which they appear. Values are case sensitive, and must match those used in the TIMDEX GraphQL query. Extraneous values will be ignored. If not populated, all filters will be shown.
55+
- `FILTER_CONTENT_TYPE`: The name to use instead of "Content type" for that filter / aggregation.
56+
- `FILTER_CONTRIBUTOR`: The name to use instead of "Contributor" for that filter / aggregation.
57+
- `FILTER_FORMAT`: The name to use instead of "Format" for that filter / aggregation.
58+
- `FILTER_LANGUAGE`: The name to use instead of "Language" for that filter / aggregation.
59+
- `FILTER_LITERARY_FORM`: The name to use instead of "Literary form" for that filter / aggregation.
60+
- `FILTER_SOURCE`: The name to use instead of "Source" for that filter / aggregation.
61+
- `FILTER_SUBJECT`: The name to use instead of "Subject" for that filter / aggregation.
5462
- `GDT`: Enables features related to geospatial data discovery. Setting this variable with any value will trigger GDT
5563
mode (e.g., `GDT=false` will still enable GDT features). Note that this is currently intended _only_ for the GDT app and
5664
may have unexpected consequences if applied to other TIMDEX UI apps.

app/controllers/search_controller.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def results
2525

2626
private
2727

28+
def active_filters
29+
ENV.fetch('ACTIVE_FILTERS', '').split(',').map(&:strip)
30+
end
31+
2832
def extract_errors(response)
2933
response&.errors&.details&.to_h&.dig('data')
3034
end
@@ -33,17 +37,27 @@ def extract_filters(response)
3337
aggs = response&.data&.search&.to_h&.dig('aggregations')
3438
return if aggs.blank?
3539

40+
aggs = reorder_filters(aggs, active_filters) unless active_filters.blank?
41+
3642
# We use aggregations to determine which terms can be filtered. However, agg names do not include 'filter', whereas
3743
# our filter fields do (e.g., 'source' vs 'sourceFilter'). Because of this mismatch, we need to modify the
3844
# aggregation key names before collecting them as filters, so that when a filter is applied, it searches the
3945
# correct field name.
40-
aggs.select { |_, agg_values| agg_values.present? }.transform_keys { |key| (key.dup << 'Filter').to_sym }
46+
aggs
47+
.select { |_, agg_values| agg_values.present? }
48+
.transform_keys { |key| (key.dup << 'Filter').to_sym }
4149
end
4250

4351
def extract_results(response)
4452
response&.data&.search&.to_h&.dig('records')
4553
end
4654

55+
def reorder_filters(aggs, active_filters)
56+
aggs
57+
.select { |key, _| active_filters.include?(key) }
58+
.sort_by { |key, _| active_filters.index(key) }.to_h
59+
end
60+
4761
def validate_q!
4862
return if params[:advanced]&.strip.present?
4963
return if params[:q]&.strip.present?

app/helpers/filter_helper.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ def add_filter(query, filter, term)
2121

2222
def nice_labels
2323
{
24-
contentTypeFilter: 'Content type',
25-
sourceFilter: 'Source'
24+
contentTypeFilter: ENV.fetch('FILTER_CONTENT_TYPE', 'Content type'),
25+
contributorsFilter: ENV.fetch('FILTER_CONTRIBUTOR', 'Contributor'),
26+
formatFilter: ENV.fetch('FILTER_FORMAT', 'Format'),
27+
languagesFilter: ENV.fetch('FILTER_LANGUAGE', 'Language'),
28+
literaryFormFilter: ENV.fetch('FILTER_LITERARY_FORM', 'Literary form'),
29+
sourceFilter: ENV.fetch('FILTER_SOURCE', 'Source'),
30+
subjectsFilter: ENV.fetch('FILTER_SUBJECT', 'Subject')
2631
}
2732
end
2833

app/models/timdex_search.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,30 @@ class TimdexSearch < TimdexBase
6060
key
6161
docCount
6262
}
63+
contributors {
64+
key
65+
docCount
66+
}
67+
format {
68+
key
69+
docCount
70+
}
71+
languages {
72+
key
73+
docCount
74+
}
75+
literaryForm {
76+
key
77+
docCount
78+
}
6379
source {
6480
key
6581
docCount
6682
}
83+
subjects {
84+
key
85+
docCount
86+
}
6787
}
6888
}
6989
}

test/controllers/search_controller_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,42 @@ def source_filter_count(controller)
417417
assert(source_filter_count(@controller) == 2)
418418
end
419419
end
420+
421+
test 'applications can customize the displayed filters via ENV' do
422+
VCR.use_cassette('data basic controller',
423+
allow_playback_repeats: true,
424+
match_requests_on: %i[method uri body]) do
425+
# Our standard test ENV does not define ACTIVE_FILTERS, but this confirms
426+
# the behavior when it is not defined.
427+
ClimateControl.modify ACTIVE_FILTERS: '' do
428+
get '/results?q=data'
429+
assert_response :success
430+
assert_select '#filters .category .filter-label', { minimum: 1 }
431+
end
432+
433+
# Ask for a single filter, get that filter.
434+
ClimateControl.modify ACTIVE_FILTERS: 'subjects' do
435+
get '/results?q=data'
436+
assert_response :success
437+
assert_select '#filters .category .filter-label', { count: 1 }
438+
assert_select '#filters .category:first-of-type .filter-label', 'Subject'
439+
end
440+
441+
# The order of the terms matter, so now Format should be first.
442+
ClimateControl.modify ACTIVE_FILTERS: 'format, contentType, source' do
443+
get '/results?q=data'
444+
assert_response :success
445+
assert_select '#filters .category .filter-label', { count: 3 }
446+
assert_select '#filters .category:first-of-type .filter-label', 'Format'
447+
end
448+
449+
# Including extra values does not affect anything - "nonsense" is extraneous.
450+
ClimateControl.modify ACTIVE_FILTERS: 'contentType, nonsense, source' do
451+
get '/results?q=data'
452+
assert_response :success
453+
assert_select '#filters .category .filter-label', { count: 2 }
454+
assert_select '#filters .category:first-of-type .filter-label', 'Content type'
455+
end
456+
end
457+
end
420458
end

test/helpers/filter_helper_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ class FilterHelperTest < ActionView::TestCase
6767
assert_nil nice_labels[needle]
6868
end
6969

70+
test 'nice_labels will use a value from ENV instead of the default if provided' do
71+
label = 'Content type'
72+
ClimateControl.modify FILTER_CONTENT_TYPE: nil do
73+
needle = :contentTypeFilter
74+
assert_equal label, nice_labels[needle]
75+
end
76+
label = 'Custom label'
77+
ClimateControl.modify FILTER_CONTENT_TYPE: label do
78+
needle = :contentTypeFilter
79+
assert_equal label, nice_labels[needle]
80+
end
81+
end
82+
7083
test 'remove_filter will remove a specific filter parameter from a search URL' do
7184
original_query = {
7285
page: 1,

test/vcr_cassettes/advanced.yml

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/vcr_cassettes/advanced_all_spaces.yml

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/vcr_cassettes/advanced_citation_asdf.yml

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/vcr_cassettes/advanced_source_defaults_to_all.yml

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)