Skip to content

Commit

Permalink
Refactor how filters are loaded from ENV
Browse files Browse the repository at this point in the history
** Why are these changes being introduced:

The prior commit was getting flagged by CodeClimate for the complexity
of the extract_filters method.

** Relevant ticket(s):

* https://mitlibraries.atlassian.net/browse/gdt-128

** How does this address that need:

* This tries taking the processing of the ENV variable into its own
  method, to hopefully make extract_filters easier to read. The call to
  this processing is made on its own line, to only perform that task
  once (rather than doing it twice, on the following line of the method)

** Document any side effects to this change:

I may be overthinking this.
  • Loading branch information
matt-bernhardt committed Mar 4, 2024
1 parent 205db6a commit 49d1d26
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def extract_filters(response)
aggs = response&.data&.search&.to_h&.dig('aggregations')
return if aggs.blank?

active_filters = ENV.fetch('ACTIVE_FILTERS', '').split(',').map(&:strip)
active_filters = load_filters

aggs = reorder_filters(aggs, active_filters) unless active_filters.blank?

Expand All @@ -50,10 +50,14 @@ def extract_results(response)
response&.data&.search&.to_h&.dig('records')
end

def load_filters
ENV.fetch('ACTIVE_FILTERS', '').split(',').map(&:strip)
end

def reorder_filters(aggs, active_filters)
aggs
.select { |key, value| active_filters.include?(key) }
.sort_by { |key, value| active_filters.index(key) }.to_h
.select { |key, _| active_filters.include?(key) }
.sort_by { |key, _| active_filters.index(key) }.to_h
end

def validate_q!
Expand Down

0 comments on commit 49d1d26

Please sign in to comment.