From e396a101902b4fdd1830d9293ae15e4cd4e0ee18 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Wed, 26 Aug 2026 11:37:06 +0100 Subject: [PATCH 1/3] Configurable filter limit for anonymous users --- app/assets/stylesheets/base/_facets.scss | 2 +- app/controllers/concerns/searchable_index.rb | 8 +++ app/helpers/search_helper.rb | 13 ++++- .../_facet_sidebar_boolean_filter.html.erb | 2 +- .../search/common/_search_panel.html.erb | 3 + config/locales/en.yml | 6 +- config/tess.example.yml | 1 + test/controllers/materials_controller_test.rb | 56 +++++++++++++++++++ 8 files changed, 86 insertions(+), 5 deletions(-) diff --git a/app/assets/stylesheets/base/_facets.scss b/app/assets/stylesheets/base/_facets.scss index 3df026424..9ca4e8695 100644 --- a/app/assets/stylesheets/base/_facets.scss +++ b/app/assets/stylesheets/base/_facets.scss @@ -56,7 +56,7 @@ ul.unstyled { width: 100%; } -.nav-item > a { +.nav-item > a, .nav-item > span { position: relative; display: block; color: #333; diff --git a/app/controllers/concerns/searchable_index.rb b/app/controllers/concerns/searchable_index.rb index d99eab3fa..72aec8902 100644 --- a/app/controllers/concerns/searchable_index.rb +++ b/app/controllers/concerns/searchable_index.rb @@ -20,6 +20,7 @@ module SearchableIndex included do attr_reader :facet_fields, :search_params, :facet_params, :page, :sort_by, :index_resources before_action :set_params, only: [:index, :count] + before_action :limit_filters before_action :fetch_resources, only: [:index, :count] helper 'search' @@ -141,4 +142,11 @@ def pagination_params def search_and_facet_params params.permit(*(@model.search_and_facet_keys | [:page_size, :page_number, :page, :per_page])) end + + def limit_filters + if !request.format.json? && !request.format.json_api? && current_user.nil? && @facet_params&.values && + TeSS::Config.filter_limit && @facet_params.values.flatten.length > TeSS::Config.filter_limit + handle_error(400, t('search.errors.filter_limit_reached')) + end + end end \ No newline at end of file diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index e7788b5cc..d6232003e 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -29,7 +29,7 @@ def filter_link(name, value, count, html_options = {}, &block) parameters.delete('page') #remove the page option if it exists html_options.reverse_merge!(title: value.to_s) - link_to parameters, html_options do + content = -> do if block_given? block.call else @@ -37,6 +37,12 @@ def filter_link(name, value, count, html_options = {}, &block) content_tag(:span, "#{count}", class: 'facet-count') end end + + if filter_limit_reached? + content_tag(:span, class: 'facet-option filter-limit-reached', &content) + else + link_to parameters, html_options, &content + end end def remove_filter_link(name, value, html_options = {}, &block) @@ -75,4 +81,9 @@ def toggle_hidden_facet_link facet ".html_safe end + + def filter_limit_reached? + current_user.nil? && @facet_params&.values && TeSS::Config.filter_limit && + @facet_params.values.flatten.length >= TeSS::Config.filter_limit + end end diff --git a/app/views/search/common/_facet_sidebar_boolean_filter.html.erb b/app/views/search/common/_facet_sidebar_boolean_filter.html.erb index 11337528d..7350fcbf2 100644 --- a/app/views/search/common/_facet_sidebar_boolean_filter.html.erb +++ b/app/views/search/common/_facet_sidebar_boolean_filter.html.erb @@ -19,7 +19,7 @@