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..492bd26e9 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, only: [:index, :count] 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..dcd7bd6ea 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,14 @@ def filter_link(name, value, count, html_options = {}, &block) content_tag(:span, "#{count}", class: 'facet-count') end end + + if filter_limit_reached? + html_options[:class] = [html_options[:class], 'filter-limit-reached'].compact.join(' ') + + content_tag(:span, html_options, &content) + else + link_to parameters, html_options, &content + end end def remove_filter_link(name, value, html_options = {}, &block) @@ -75,4 +83,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 @@