From 732374d1d82d718d00ccca2fa66f1b5d660c8024 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Thu, 12 Dec 2024 13:58:37 -0500 Subject: [PATCH 01/36] move override file --- .../facet_field_pagination_component.html.erb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 app/components/blacklight/facet_field_pagination_component.html.erb diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb new file mode 100644 index 000000000..b8a549a91 --- /dev/null +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -0,0 +1,19 @@ + + +
+ <% if @facet_field.paginator.total_pages > 1 %> + + <% end %> +
+ +
+ <% if @facet_field.paginator.sort == 'index' -%> + <%= t('blacklight.search.facets.sort.index') %> + <%= helpers.link_to(t('blacklight.search.facets.sort.count'), sort_facet_url('count'), class: "sort_change numeric btn btn-outline-secondary", data: { blacklight_modal: "preserve" }) %> + <% elsif @facet_field.paginator.sort == 'count' -%> + <%= helpers.link_to(t('blacklight.search.facets.sort.index'), sort_facet_url('index'), class: "sort_change az btn btn-outline-secondary", data: { blacklight_modal: "preserve" }) %> + <%= t('blacklight.search.facets.sort.count') %> + <% end -%> +
\ No newline at end of file From c6956afbb02db2381c563411a674eaf7066b9292 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Thu, 12 Dec 2024 17:42:44 -0500 Subject: [PATCH 02/36] override ruby component --- .../facet_field_pagination_component.html.erb | 15 ++++++++----- ...cet_field_pagination_component_override.rb | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 app/overrides/components/blacklight/facet_field_pagination_component_override.rb diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index b8a549a91..27afd7732 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -1,12 +1,15 @@ -
- <% if @facet_field.paginator.total_pages > 1 %> - + <% end %>
<% if @facet_field.paginator.sort == 'index' -%> diff --git a/app/overrides/components/blacklight/facet_field_pagination_component_override.rb b/app/overrides/components/blacklight/facet_field_pagination_component_override.rb new file mode 100644 index 000000000..60cc3246e --- /dev/null +++ b/app/overrides/components/blacklight/facet_field_pagination_component_override.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true +# [hyc-override] https://github.com/projectblacklight/blacklight/blob/v7.33.1/app/components/blacklight/facet_field_pagination_component.rb +Blacklight::FacetFieldPaginationComponent.class_eval do + def dept_paginate(facet_paginator, rows: 10, page_param: :page) + return if facet_paginator.nil? + Rails.logger.info('D12 - Not Nil Paginator') + + items = facet_paginator.instance_variable_get(:@all) + return if items.nil? || items.empty? + Rails.logger.info('D12 - Not Nil Items') + + total_count = items.size + total_pages = (total_count.to_f / rows.to_f).ceil + page = params[page_param].presence&.to_i || 1 + current_page = [page, total_pages].min + Rails.logger.info("tc: #{total_count}") + Rails.logger.info("tp: #{total_pages}") + Rails.logger.info("p: #{current_page}") + + Kaminari.paginate_array(items, total_count: total_count).page(current_page).per(rows) + end +end \ No newline at end of file From d93702c38b2dd6121292e61e17c181933b9cea7c Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 13:03:08 -0500 Subject: [PATCH 03/36] comments --- .../facet_field_pagination_component.html.erb | 4 +- ...cet_field_pagination_component_override.rb | 40 ++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 27afd7732..6a903a1e9 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -1,10 +1,10 @@ - + <% paginator = @facet_field.paginator %> <% if paginator.present? %> <% items = paginator.instance_variable_get(:@all) %> <% rows = paginator.instance_variable_get(:@limit) %> - <% paginated_items = dept_paginate(paginator, rows: rows) %> + <% paginated_items = convert_facet_to_paginated_array(paginator, rows: rows) %>
<%= paginate paginated_items, theme: 'blacklight', param_name: :page %> diff --git a/app/overrides/components/blacklight/facet_field_pagination_component_override.rb b/app/overrides/components/blacklight/facet_field_pagination_component_override.rb index 60cc3246e..ffde03c11 100644 --- a/app/overrides/components/blacklight/facet_field_pagination_component_override.rb +++ b/app/overrides/components/blacklight/facet_field_pagination_component_override.rb @@ -1,22 +1,26 @@ # frozen_string_literal: true # [hyc-override] https://github.com/projectblacklight/blacklight/blob/v7.33.1/app/components/blacklight/facet_field_pagination_component.rb Blacklight::FacetFieldPaginationComponent.class_eval do - def dept_paginate(facet_paginator, rows: 10, page_param: :page) - return if facet_paginator.nil? - Rails.logger.info('D12 - Not Nil Paginator') + # Converts a `Blacklight::Solr::FacetPaginator` to `Kaminari::PaginatableArray` to enable pagination utilizing Kaminari gem + # Based on `paginate` in Hyrax::Admin::Analytics::AnalyticsController + # https://github.com/samvera/hyrax/blob/hyrax-v4.0.0/app/controllers/hyrax/admin/analytics/analytics_controller.rb + def convert_facet_to_paginated_array(facet_paginator, rows: 10, page_param: :page) + return if facet_paginator.nil? + # Retrieve all items + items = facet_paginator.instance_variable_get(:@all) + return if items.nil? || items.empty? - items = facet_paginator.instance_variable_get(:@all) - return if items.nil? || items.empty? - Rails.logger.info('D12 - Not Nil Items') - - total_count = items.size - total_pages = (total_count.to_f / rows.to_f).ceil - page = params[page_param].presence&.to_i || 1 - current_page = [page, total_pages].min - Rails.logger.info("tc: #{total_count}") - Rails.logger.info("tp: #{total_pages}") - Rails.logger.info("p: #{current_page}") - - Kaminari.paginate_array(items, total_count: total_count).page(current_page).per(rows) - end -end \ No newline at end of file + total_count = items.size + # Calculate the total number of pages based on the number of rows per page + total_pages = (total_count.to_f / rows.to_f).ceil + # Extract the current page number from the response params, defaulting to 1 if not present + page = params[page_param].presence&.to_i || 1 + # Ensure the current page does not exceed the total number of pages + current_page = [page, total_pages].min + + # Use Kaminari to create a paginated collection from the items array + # `.page(current_page)` sets the current page + # `.per(rows)` sets the number of items to display per page + Kaminari.paginate_array(items, total_count: total_count).page(current_page).per(rows) + end +end From c766b9fdfac64bbbdc6bf48c038bbf3de21f7945 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 14:27:33 -0500 Subject: [PATCH 04/36] paginate without katamari gem --- .../facet_field_pagination_component.html.erb | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 6a903a1e9..e8b987111 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -1,16 +1,49 @@ - - <% paginator = @facet_field.paginator %> - <% if paginator.present? %> - <% items = paginator.instance_variable_get(:@all) %> - <% rows = paginator.instance_variable_get(:@limit) %> - <% paginated_items = convert_facet_to_paginated_array(paginator, rows: rows) %> + +
<% if @facet_field.paginator.sort == 'index' -%> <%= t('blacklight.search.facets.sort.index') %> From 7f6903b03ad67bcb2c320d197ceed7009a1f6e65 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 15:28:53 -0500 Subject: [PATCH 05/36] kanamari --- .../facet_field_pagination_component.html.erb | 54 ++++--------------- 1 file changed, 11 insertions(+), 43 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index e8b987111..57a507625 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -1,48 +1,16 @@ - +
+ <%= paginate solr_response, outer_window: 4, theme: 'blacklight', param_name: page_param_name %> +
+
<% if @facet_field.paginator.sort == 'index' -%> From 113fc243528f19ea4f8b44e00de4d1e81903ca70 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 17:23:07 -0500 Subject: [PATCH 06/36] update component override --- .../facet_field_pagination_component.html.erb | 55 +++++++++++++++---- .../helpers/helper_methods_override.rb | 22 ++++++++ 2 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 app/overrides/lib/kanamari/helpers/helper_methods_override.rb diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 57a507625..6405072c0 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -1,16 +1,49 @@ - - <% solr_response ||= @response %> - <% page_param_name ||= :page %> - +
<% if @facet_field.paginator.sort == 'index' -%> diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb new file mode 100644 index 000000000..f01bf0af8 --- /dev/null +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true +# [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb +Kaminari::Helpers::HelperMethods.module_eval do + # Helper to generate a link to a specific page + def link_to_specific_page(scope, page, name, **options) + specific_page_path = path_to_specific_page(scope, page, options) + # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant + options.except! :params, :param_name + # Setting aria instead of rel for accessibility + options[:aria] ||= { label: "Go to page #{page}" } + if specific_page_path + link_to(name || page, specific_page_path, options) + elsif block_given? + yield + end + end + + # Helper to generate the path for a specific page + def path_to_specific_page(scope, page, options = {}) + Kaminari::Helpers::Page.new(self, **options.reverse_merge(current_page: page)).url if page > 0 && page <= scope.total_pages + end +end \ No newline at end of file From aef7a02c16957723a13db56d8009ac44d0e000fc Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 17:29:16 -0500 Subject: [PATCH 07/36] rubocop --- .../helpers/helper_methods_override.rb | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index f01bf0af8..244b664a1 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -1,22 +1,25 @@ # frozen_string_literal: true # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do - # Helper to generate a link to a specific page - def link_to_specific_page(scope, page, name, **options) - specific_page_path = path_to_specific_page(scope, page, options) - # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant - options.except! :params, :param_name - # Setting aria instead of rel for accessibility - options[:aria] ||= { label: "Go to page #{page}" } - if specific_page_path - link_to(name || page, specific_page_path, options) - elsif block_given? - yield - end - end + # Helper to generate a link to a specific page + def link_to_specific_page(scope, page, name, **options) + specific_page_path = path_to_specific_page(scope, page, options) - # Helper to generate the path for a specific page - def path_to_specific_page(scope, page, options = {}) - Kaminari::Helpers::Page.new(self, **options.reverse_merge(current_page: page)).url if page > 0 && page <= scope.total_pages - end -end \ No newline at end of file + # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant + options.except! :params, :param_name + + # Setting aria instead of rel for accessibility + options[:aria] ||= { label: "Go to page #{page}" } + + if specific_page_path + link_to(name || page, specific_page_path, options) + elsif block_given? + yield + end + end + + # Helper to generate the path for a specific page + def path_to_specific_page(scope, page, options = {}) + Kaminari::Helpers::Page.new(self, **options.reverse_merge(current_page: page)).url if page.positive? && page <= scope.total_pages + end +end From 0e59dd66886ab89095473bac752d7f50cbcd09bb Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 17:48:13 -0500 Subject: [PATCH 08/36] error handling --- .../helpers/helper_methods_override.rb | 64 +++++++++++++------ 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 244b664a1..70887b633 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,24 +2,52 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, page, name, **options) - specific_page_path = path_to_specific_page(scope, page, options) - - # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant - options.except! :params, :param_name - - # Setting aria instead of rel for accessibility - options[:aria] ||= { label: "Go to page #{page}" } - - if specific_page_path - link_to(name || page, specific_page_path, options) - elsif block_given? - yield + def link_to_specific_page(scope, page, name, **options) + begin + # Validate inputs + raise ArgumentError, "Scope is required and must respond to :total_pages" unless scope&.respond_to?(:total_pages) + raise ArgumentError, "Page number must be a positive integer" unless page.is_a?(Integer) && page.positive? + + specific_page_path = path_to_specific_page(scope, page, options) + + # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant + options.except! :params, :param_name + + # Setting aria instead of rel for accessibility + options[:aria] ||= { label: "Go to page #{page}" } + + if specific_page_path + link_to(name || page, specific_page_path, options) + elsif block_given? + yield + else + Rails.logger.warn "Specific page path could not be generated for page: #{page}" + nil + end + rescue ArgumentError => e + Rails.logger.error "Error in link_to_specific_page: #{e.message}" + nil + rescue StandardError => e + Rails.logger.error "Unexpected error in link_to_specific_page: #{e.message}" + nil + end end - end - + # Helper to generate the path for a specific page - def path_to_specific_page(scope, page, options = {}) - Kaminari::Helpers::Page.new(self, **options.reverse_merge(current_page: page)).url if page.positive? && page <= scope.total_pages + def path_to_specific_page(scope, page, options = {}) + begin + # Validate inputs + raise ArgumentError, "Page number must be a positive integer" unless page.is_a?(Integer) && page.positive? + raise ArgumentError, "Scope must respond to :total_pages" unless scope&.respond_to?(:total_pages) + + Kaminari::Helpers::Page.new(self, **options.reverse_merge(current_page: page)).url if page <= scope.total_pages + rescue ArgumentError => e + Rails.logger.error "Error in path_to_specific_page: #{e.message}" + nil + rescue StandardError => e + Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}" + nil + end + end end -end + \ No newline at end of file From 91032ef0db7c5793a98a1c7a3dadfbae3cef561c Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 18:10:46 -0500 Subject: [PATCH 09/36] add default for link_to_specific_page, type cast --- .../blacklight/facet_field_pagination_component.html.erb | 2 +- app/overrides/lib/kanamari/helpers/helper_methods_override.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 6405072c0..bab71c93f 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -34,7 +34,7 @@ <% if page == current_page %> <%= page %> <% else %> - <%= helpers.link_to_specific_page @facet_field.paginator, "Page: #{page}", page, params: @facet_field.search_state.to_h.merge(page: page), class: "page-link" %> + <%= helpers.link_to_specific_page @facet_field.paginator, "Page: #{page}", page.to_i, params: @facet_field.search_state.to_h.merge(page: page), class: "page-link" %> <% end %> diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 70887b633..5eead5c6c 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,7 +2,7 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, page, name, **options) + def link_to_specific_page(scope, page = 1, name, **options) begin # Validate inputs raise ArgumentError, "Scope is required and must respond to :total_pages" unless scope&.respond_to?(:total_pages) From 0fb385720f62718247bd835e8231f405de58e400 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 18:25:36 -0500 Subject: [PATCH 10/36] argument order --- ..._field_pagination_component_defnc.html.erb | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/components/blacklight/facet_field_pagination_component_defnc.html.erb diff --git a/app/components/blacklight/facet_field_pagination_component_defnc.html.erb b/app/components/blacklight/facet_field_pagination_component_defnc.html.erb new file mode 100644 index 000000000..6a903a1e9 --- /dev/null +++ b/app/components/blacklight/facet_field_pagination_component_defnc.html.erb @@ -0,0 +1,22 @@ + + + <% paginator = @facet_field.paginator %> + <% if paginator.present? %> + <% items = paginator.instance_variable_get(:@all) %> + <% rows = paginator.instance_variable_get(:@limit) %> + <% paginated_items = convert_facet_to_paginated_array(paginator, rows: rows) %> + +
+ <%= paginate paginated_items, theme: 'blacklight', param_name: :page %> +
+ <% end %> + +
+ <% if @facet_field.paginator.sort == 'index' -%> + <%= t('blacklight.search.facets.sort.index') %> + <%= helpers.link_to(t('blacklight.search.facets.sort.count'), sort_facet_url('count'), class: "sort_change numeric btn btn-outline-secondary", data: { blacklight_modal: "preserve" }) %> + <% elsif @facet_field.paginator.sort == 'count' -%> + <%= helpers.link_to(t('blacklight.search.facets.sort.index'), sort_facet_url('index'), class: "sort_change az btn btn-outline-secondary", data: { blacklight_modal: "preserve" }) %> + <%= t('blacklight.search.facets.sort.count') %> + <% end -%> +
\ No newline at end of file From 44b6a6475db41cd658024651bb47b5b5cfff38b9 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 18:37:53 -0500 Subject: [PATCH 11/36] reorder arguments --- .../helpers/helper_methods_override.rb | 85 +++++++++---------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 5eead5c6c..f231f184f 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,52 +2,51 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, page = 1, name, **options) - begin - # Validate inputs - raise ArgumentError, "Scope is required and must respond to :total_pages" unless scope&.respond_to?(:total_pages) - raise ArgumentError, "Page number must be a positive integer" unless page.is_a?(Integer) && page.positive? - - specific_page_path = path_to_specific_page(scope, page, options) - - # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant - options.except! :params, :param_name - - # Setting aria instead of rel for accessibility - options[:aria] ||= { label: "Go to page #{page}" } - - if specific_page_path - link_to(name || page, specific_page_path, options) - elsif block_given? - yield - else - Rails.logger.warn "Specific page path could not be generated for page: #{page}" - nil - end - rescue ArgumentError => e - Rails.logger.error "Error in link_to_specific_page: #{e.message}" - nil - rescue StandardError => e - Rails.logger.error "Unexpected error in link_to_specific_page: #{e.message}" + def link_to_specific_page(scope, name, page = 1 **options) + begin + # Validate inputs + raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages) + raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive? + + specific_page_path = path_to_specific_page(scope, page, options) + + # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant + options.except! :params, :param_name + + # Setting aria instead of rel for accessibility + options[:aria] ||= { label: "Go to page #{page}" } + + if specific_page_path + link_to(name || page, specific_page_path, options) + elsif block_given? + yield + else + Rails.logger.warn "Specific page path could not be generated for page: #{page}" nil end + rescue ArgumentError => e + Rails.logger.error "Error in link_to_specific_page: #{e.message}" + nil + rescue StandardError => e + Rails.logger.error "Unexpected error in link_to_specific_page: #{e.message}" + nil end - + end + # Helper to generate the path for a specific page - def path_to_specific_page(scope, page, options = {}) - begin - # Validate inputs - raise ArgumentError, "Page number must be a positive integer" unless page.is_a?(Integer) && page.positive? - raise ArgumentError, "Scope must respond to :total_pages" unless scope&.respond_to?(:total_pages) - - Kaminari::Helpers::Page.new(self, **options.reverse_merge(current_page: page)).url if page <= scope.total_pages - rescue ArgumentError => e - Rails.logger.error "Error in path_to_specific_page: #{e.message}" - nil - rescue StandardError => e - Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}" - nil - end + def path_to_specific_page(scope, page, options = {}) + begin + # Validate inputs + raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive? + raise ArgumentError, 'Scope must respond to :total_pages' unless scope&.respond_to?(:total_pages) + + Kaminari::Helpers::Page.new(self, **options.reverse_merge(current_page: page)).url if page <= scope.total_pages + rescue ArgumentError => e + Rails.logger.error "Error in path_to_specific_page: #{e.message}" + nil + rescue StandardError => e + Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}" + nil end end - \ No newline at end of file +end From fe39396418aa78f1b990e2aefb95794411986da6 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 16 Dec 2024 18:40:09 -0500 Subject: [PATCH 12/36] logging --- app/overrides/lib/kanamari/helpers/helper_methods_override.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index f231f184f..6cb50f1d2 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -6,7 +6,7 @@ def link_to_specific_page(scope, name, page = 1 **options) begin # Validate inputs raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages) - raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive? + raise ArgumentError, "Page number must be a positive integer - got #{page}" unless page.is_a?(Integer) && page.positive? specific_page_path = path_to_specific_page(scope, page, options) From 16076cffc80e33b144ff92d986f6da0f1d5b7741 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Tue, 17 Dec 2024 09:51:04 -0500 Subject: [PATCH 13/36] syntax --- .../blacklight/facet_field_pagination_component.html.erb | 2 +- app/overrides/lib/kanamari/helpers/helper_methods_override.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index bab71c93f..7d9e658b1 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -34,7 +34,7 @@ <% if page == current_page %> <%= page %> <% else %> - <%= helpers.link_to_specific_page @facet_field.paginator, "Page: #{page}", page.to_i, params: @facet_field.search_state.to_h.merge(page: page), class: "page-link" %> + <%= helpers.link_to_specific_page @facet_field.paginator, page.to_i, "Page: #{page}", class: "page-link" %> <% end %> diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 6cb50f1d2..6aa92c094 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,7 +2,7 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, name, page = 1 **options) + def link_to_specific_page(scope, page = 1, name, **options) begin # Validate inputs raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages) From 073383806b3e6155f062863a6e5bfe78b03910f6 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Tue, 17 Dec 2024 10:07:22 -0500 Subject: [PATCH 14/36] syntax --- .../blacklight/facet_field_pagination_component.html.erb | 2 +- app/overrides/lib/kanamari/helpers/helper_methods_override.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 7d9e658b1..41c3b52b8 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -34,7 +34,7 @@ <% if page == current_page %> <%= page %> <% else %> - <%= helpers.link_to_specific_page @facet_field.paginator, page.to_i, "Page: #{page}", class: "page-link" %> + <%= helpers.link_to_specific_page @facet_field.paginator, "Page: #{page}", page.to_i, class: "page-link" %> <% end %> diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 6aa92c094..ec0b71ded 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,7 +2,7 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, page = 1, name, **options) + def link_to_specific_page(scope, name, page = 1, **options) begin # Validate inputs raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages) From 8b7e3e28188fa0b72aebef71189f01acb51254fa Mon Sep 17 00:00:00 2001 From: David Campbell Date: Tue, 17 Dec 2024 12:26:29 -0500 Subject: [PATCH 15/36] manually calculate total pages --- .../facet_field_pagination_component.html.erb | 3 +-- .../helpers/helper_methods_override.rb | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 41c3b52b8..c5321efd1 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -20,7 +20,6 @@ <% surrounding_pages = 4 %> <% (1..total_pages).each do |page| %> - <% page_display = number_with_delimiter(page) %> <% within_range = (page >= current_page - surrounding_pages && page <= current_page + surrounding_pages) %> <% show_page = ( @@ -34,7 +33,7 @@ <% if page == current_page %> <%= page %> <% else %> - <%= helpers.link_to_specific_page @facet_field.paginator, "Page: #{page}", page.to_i, class: "page-link" %> + <%= helpers.link_to_specific_page @facet_field.paginator,"#{page}", page.to_i, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> <% end %> diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index ec0b71ded..4c07bbdc5 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,7 +2,7 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, name, page = 1, **options) + def link_to_specific_page(scope, name, page, **options) begin # Validate inputs raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages) @@ -36,16 +36,23 @@ def link_to_specific_page(scope, name, page = 1, **options) # Helper to generate the path for a specific page def path_to_specific_page(scope, page, options = {}) begin + # Calculate total pages manually + total_items = scope.instance_variable_get(:@all).size + limit = scope.instance_variable_get(:@limit) + total_pages = (total_items.to_f / limit).ceil + + Rails.logger.info "path_to_specific_page: total_items=#{total_items}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}" + # Validate inputs raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive? - raise ArgumentError, 'Scope must respond to :total_pages' unless scope&.respond_to?(:total_pages) - - Kaminari::Helpers::Page.new(self, **options.reverse_merge(current_page: page)).url if page <= scope.total_pages + raise ArgumentError, "Page number exceeds total pages (#{total_pages})" if page > total_pages + # Generate URL using Kaminari's Page helper + Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page)).url rescue ArgumentError => e - Rails.logger.error "Error in path_to_specific_page: #{e.message}" + Rails.logger.info "Error in path_to_specific_page: #{e.message}" nil rescue StandardError => e - Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}" + Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}\n#{e.backtrace.join("\n")}" nil end end From 80eca6a3ac77ea4f6f3bc778b871cd09fa6f5369 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Tue, 17 Dec 2024 12:28:35 -0500 Subject: [PATCH 16/36] delete file --- ..._field_pagination_component_defnc.html.erb | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 app/components/blacklight/facet_field_pagination_component_defnc.html.erb diff --git a/app/components/blacklight/facet_field_pagination_component_defnc.html.erb b/app/components/blacklight/facet_field_pagination_component_defnc.html.erb deleted file mode 100644 index 6a903a1e9..000000000 --- a/app/components/blacklight/facet_field_pagination_component_defnc.html.erb +++ /dev/null @@ -1,22 +0,0 @@ - - - <% paginator = @facet_field.paginator %> - <% if paginator.present? %> - <% items = paginator.instance_variable_get(:@all) %> - <% rows = paginator.instance_variable_get(:@limit) %> - <% paginated_items = convert_facet_to_paginated_array(paginator, rows: rows) %> - -
- <%= paginate paginated_items, theme: 'blacklight', param_name: :page %> -
- <% end %> - -
- <% if @facet_field.paginator.sort == 'index' -%> - <%= t('blacklight.search.facets.sort.index') %> - <%= helpers.link_to(t('blacklight.search.facets.sort.count'), sort_facet_url('count'), class: "sort_change numeric btn btn-outline-secondary", data: { blacklight_modal: "preserve" }) %> - <% elsif @facet_field.paginator.sort == 'count' -%> - <%= helpers.link_to(t('blacklight.search.facets.sort.index'), sort_facet_url('index'), class: "sort_change az btn btn-outline-secondary", data: { blacklight_modal: "preserve" }) %> - <%= t('blacklight.search.facets.sort.count') %> - <% end -%> -
\ No newline at end of file From 7e1f60f084366afa6d67bc77ad4fce92bfabef62 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 14:53:22 -0500 Subject: [PATCH 17/36] generate skip ahead urls based on facet total --- app/controllers/catalog_controller.rb | 52 +++++++++++++++++++ .../helpers/helper_methods_override.rb | 14 ++--- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index 36c8b9d03..0f4d6ca68 100755 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -37,6 +37,58 @@ def single_item_search_builder(id) single_item_search_builder_class.new(self, id).with(params.except(:q, :page)) end + def facet_total_count(field_name) + query = '*:*' + params = { + facet: true, + 'facet.field': field_name, + rows: 0, + 'facet.limit': -1, # Retrieve all facets + wt: 'json' + } + + # Perform the Solr query using Hyrax::SolrService + # Using spread operator to convert object into keyword arguments + response = Hyrax::SolrService.get(query, **params) + + # Parse the response to extract the total unique facets + facet_values = response.dig('facet_counts', 'facet_fields', field_name) + # Facet counts are included with names in the list, so divide by 2 + total_unique_facets = facet_values ? (facet_values.length / 2) : 0 + + Rails.logger.info("Total unique facets for '#{field_name}': #{total_unique_facets}") + total_unique_facets + rescue StandardError => e + Rails.logger.error("Error retrieving facets for '#{field_name}': #{e.message}") + Rails.logger.error(e.backtrace.join("\n")) + 0 + end + + def facet + Rails.logger.info('FACET ACTION STARTED') + Rails.logger.info("Request Parameters: #{params.inspect}") + + begin + facet_field = params[:id] + Rails.logger.info("Facet Field: #{facet_field}") + super + # Calculate the total unique facet count and append it to the response + total_facet_count = facet_total_count(facet_field) + @response[:facet_total_count] = total_facet_count + # Log the response object if available + Rails.logger.info("Facet Response: #{@response.inspect}") if @response + rescue StandardError => e + # Capture any errors that occur and log them + Rails.logger.error("Error during facet action: #{e.message}") + Rails.logger.error(e.backtrace.join("\n")) + # Optionally, re-raise the error or render a fallback response + raise e + ensure + Rails.logger.info('FACET ACTION COMPLETED') + end + end + + configure_blacklight do |config| # Advanced search configuration config.advanced_search ||= Blacklight::OpenStructWithHashAccess.new diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 4c07bbdc5..98eadec85 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,13 +2,13 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, name, page, **options) + def link_to_specific_page(scope, name, page, total_pages, **options) begin # Validate inputs - raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages) + # raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages) raise ArgumentError, "Page number must be a positive integer - got #{page}" unless page.is_a?(Integer) && page.positive? - specific_page_path = path_to_specific_page(scope, page, options) + specific_page_path = path_to_specific_page(scope, page, total_pages, options) # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant options.except! :params, :param_name @@ -34,12 +34,12 @@ def link_to_specific_page(scope, name, page, **options) end # Helper to generate the path for a specific page - def path_to_specific_page(scope, page, options = {}) + def path_to_specific_page(scope, page, total_pages, options = {}) begin # Calculate total pages manually - total_items = scope.instance_variable_get(:@all).size - limit = scope.instance_variable_get(:@limit) - total_pages = (total_items.to_f / limit).ceil + # total_items = scope.instance_variable_get(:@all).size + # limit = scope.instance_variable_get(:@limit) + # total_pages = (total_items.to_f / limit).ceil Rails.logger.info "path_to_specific_page: total_items=#{total_items}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}" From d6ba7e93f7975d0bcbcd5f01dbce3347ff35789f Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 15:10:23 -0500 Subject: [PATCH 18/36] temp log change --- app/controllers/catalog_controller.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index 0f4d6ca68..8883a21ac 100755 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -59,8 +59,9 @@ def facet_total_count(field_name) Rails.logger.info("Total unique facets for '#{field_name}': #{total_unique_facets}") total_unique_facets rescue StandardError => e - Rails.logger.error("Error retrieving facets for '#{field_name}': #{e.message}") - Rails.logger.error(e.backtrace.join("\n")) + # WIP: Change Later + Rails.logger.info("Error retrieving facets for '#{field_name}': #{e.message}") + Rails.logger.info(e.backtrace.join("\n")) 0 end From 4533f2d28bc5b925836178895b4a2d15048a46c3 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 15:29:37 -0500 Subject: [PATCH 19/36] total count instance variable --- .../blacklight/facet_field_pagination_component.html.erb | 2 +- app/controllers/catalog_controller.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index c5321efd1..46ded57e9 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -33,7 +33,7 @@ <% if page == current_page %> <%= page %> <% else %> - <%= helpers.link_to_specific_page @facet_field.paginator,"#{page}", page.to_i, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> + <%= helpers.link_to_specific_page @facet_field.paginator,"#{page}", page.to_i, @facet_total_count, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> <% end %> diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index 8883a21ac..b20f02aad 100755 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -74,8 +74,8 @@ def facet Rails.logger.info("Facet Field: #{facet_field}") super # Calculate the total unique facet count and append it to the response - total_facet_count = facet_total_count(facet_field) - @response[:facet_total_count] = total_facet_count + @facet_total_count = facet_total_count(facet_field) + # @response[:facet_total_count] = total_facet_count # Log the response object if available Rails.logger.info("Facet Response: #{@response.inspect}") if @response rescue StandardError => e From d027a5127d377ab2d5fcd1da14227e66f951e7d9 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 15:47:41 -0500 Subject: [PATCH 20/36] log update --- .../blacklight/facet_field_pagination_component.html.erb | 5 ++--- app/controllers/catalog_controller.rb | 2 +- .../lib/kanamari/helpers/helper_methods_override.rb | 8 ++++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 46ded57e9..dbd6794f8 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -10,11 +10,10 @@
    - <% total_items = @facet_field.paginator.instance_variable_get(:@all).size %> <% limit = @facet_field.paginator.instance_variable_get(:@limit) %> <% offset = @facet_field.paginator.instance_variable_get(:@offset) %> <% current_page = (offset / limit) + 1 %> - <% total_pages = (total_items.to_f / limit).ceil %> + <% total_pages = (@total_unique_facets.to_f / limit).ceil %> <% first_pages_count = 2 %> <% last_pages_count = 2 %> <% surrounding_pages = 4 %> @@ -33,7 +32,7 @@ <% if page == current_page %> <%= page %> <% else %> - <%= helpers.link_to_specific_page @facet_field.paginator,"#{page}", page.to_i, @facet_total_count, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> + <%= helpers.link_to_specific_page @facet_field.paginator,"#{page}", page.to_i, @total_unique_facets, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> <% end %> diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index b20f02aad..08f3865a1 100755 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -74,7 +74,7 @@ def facet Rails.logger.info("Facet Field: #{facet_field}") super # Calculate the total unique facet count and append it to the response - @facet_total_count = facet_total_count(facet_field) + @total_unique_facets = facet_total_count(facet_field) # @response[:facet_total_count] = total_facet_count # Log the response object if available Rails.logger.info("Facet Response: #{@response.inspect}") if @response diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 98eadec85..30d00a91c 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,13 +2,13 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, name, page, total_pages, **options) + def link_to_specific_page(scope, name, page, total_entries, **options) begin # Validate inputs # raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages) raise ArgumentError, "Page number must be a positive integer - got #{page}" unless page.is_a?(Integer) && page.positive? - specific_page_path = path_to_specific_page(scope, page, total_pages, options) + specific_page_path = path_to_specific_page(scope, page, total_entries, options) # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant options.except! :params, :param_name @@ -34,14 +34,14 @@ def link_to_specific_page(scope, name, page, total_pages, **options) end # Helper to generate the path for a specific page - def path_to_specific_page(scope, page, total_pages, options = {}) + def path_to_specific_page(scope, page, total_entries, options = {}) begin # Calculate total pages manually # total_items = scope.instance_variable_get(:@all).size # limit = scope.instance_variable_get(:@limit) # total_pages = (total_items.to_f / limit).ceil - Rails.logger.info "path_to_specific_page: total_items=#{total_items}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}" + Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}" # Validate inputs raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive? From 3c31474bf8e741fdf9b321336821080f5527a91b Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 16:24:18 -0500 Subject: [PATCH 21/36] update FacetFieldPaginationComponent override --- .../facet_field_pagination_component.html.erb | 1 + ...cet_field_pagination_component_override.rb | 40 +++++++++++-------- app/views/catalog/_facet_pagination.html.erb | 1 + 3 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 app/views/catalog/_facet_pagination.html.erb diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index dbd6794f8..b20a00859 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -17,6 +17,7 @@ <% first_pages_count = 2 %> <% last_pages_count = 2 %> <% surrounding_pages = 4 %> + <% Rails.logger.info("Limit: #{limit}, Offset: #{offset}, Total Pages: #{total_pages}, Total Unique Facets: #{@total_unique_facets}")%> <% (1..total_pages).each do |page| %> diff --git a/app/overrides/components/blacklight/facet_field_pagination_component_override.rb b/app/overrides/components/blacklight/facet_field_pagination_component_override.rb index ffde03c11..d2bb014f0 100644 --- a/app/overrides/components/blacklight/facet_field_pagination_component_override.rb +++ b/app/overrides/components/blacklight/facet_field_pagination_component_override.rb @@ -1,26 +1,32 @@ # frozen_string_literal: true # [hyc-override] https://github.com/projectblacklight/blacklight/blob/v7.33.1/app/components/blacklight/facet_field_pagination_component.rb Blacklight::FacetFieldPaginationComponent.class_eval do + attr_reader :facet_field, :total_unique_facets + def initialize(facet_field:, total_unique_facets: nil) + @facet_field = facet_field + # Integrate total unique facets as an attribute for pagination + @total_unique_facets = total_unique_facets + end # Converts a `Blacklight::Solr::FacetPaginator` to `Kaminari::PaginatableArray` to enable pagination utilizing Kaminari gem # Based on `paginate` in Hyrax::Admin::Analytics::AnalyticsController # https://github.com/samvera/hyrax/blob/hyrax-v4.0.0/app/controllers/hyrax/admin/analytics/analytics_controller.rb - def convert_facet_to_paginated_array(facet_paginator, rows: 10, page_param: :page) - return if facet_paginator.nil? - # Retrieve all items - items = facet_paginator.instance_variable_get(:@all) - return if items.nil? || items.empty? + # def convert_facet_to_paginated_array(facet_paginator, rows: 10, page_param: :page) + # return if facet_paginator.nil? + # # Retrieve all items + # items = facet_paginator.instance_variable_get(:@all) + # return if items.nil? || items.empty? - total_count = items.size - # Calculate the total number of pages based on the number of rows per page - total_pages = (total_count.to_f / rows.to_f).ceil - # Extract the current page number from the response params, defaulting to 1 if not present - page = params[page_param].presence&.to_i || 1 - # Ensure the current page does not exceed the total number of pages - current_page = [page, total_pages].min + # total_count = items.size + # # Calculate the total number of pages based on the number of rows per page + # total_pages = (total_count.to_f / rows.to_f).ceil + # # Extract the current page number from the response params, defaulting to 1 if not present + # page = params[page_param].presence&.to_i || 1 + # # Ensure the current page does not exceed the total number of pages + # current_page = [page, total_pages].min - # Use Kaminari to create a paginated collection from the items array - # `.page(current_page)` sets the current page - # `.per(rows)` sets the number of items to display per page - Kaminari.paginate_array(items, total_count: total_count).page(current_page).per(rows) - end + # # Use Kaminari to create a paginated collection from the items array + # # `.page(current_page)` sets the current page + # # `.per(rows)` sets the number of items to display per page + # Kaminari.paginate_array(items, total_count: total_count).page(current_page).per(rows) + # end end diff --git a/app/views/catalog/_facet_pagination.html.erb b/app/views/catalog/_facet_pagination.html.erb new file mode 100644 index 000000000..516ecc64a --- /dev/null +++ b/app/views/catalog/_facet_pagination.html.erb @@ -0,0 +1 @@ +<%= render(Blacklight::FacetFieldPaginationComponent.new(facet_field: facet_field_presenter(@facet, @display_facet), total_unique_facets: @total_unique_facets)) %> \ No newline at end of file From 77f57aaa8d4ed6261d3cbf192230abf81cb62292 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 16:37:13 -0500 Subject: [PATCH 22/36] remove log --- app/overrides/lib/kanamari/helpers/helper_methods_override.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 30d00a91c..10aec59c7 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -41,7 +41,7 @@ def path_to_specific_page(scope, page, total_entries, options = {}) # limit = scope.instance_variable_get(:@limit) # total_pages = (total_items.to_f / limit).ceil - Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}" + # Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}" # Validate inputs raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive? From d34ef2114ef8029c1ae653d8e721af321ab89d1e Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 17:04:02 -0500 Subject: [PATCH 23/36] syntax --- .../lib/kanamari/helpers/helper_methods_override.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 10aec59c7..f28760843 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -37,11 +37,10 @@ def link_to_specific_page(scope, name, page, total_entries, **options) def path_to_specific_page(scope, page, total_entries, options = {}) begin # Calculate total pages manually - # total_items = scope.instance_variable_get(:@all).size - # limit = scope.instance_variable_get(:@limit) - # total_pages = (total_items.to_f / limit).ceil + limit = scope.instance_variable_get(:@limit) + total_pages = (total_entries.to_f / limit).ceil - # Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}" + Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}" # Validate inputs raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive? From 92f9bf0ac1161a67f6dfe9da9ebdcc5daec785dd Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 17:28:51 -0500 Subject: [PATCH 24/36] cleanup --- app/controllers/catalog_controller.rb | 26 +++++-------------- ...cet_field_pagination_component_override.rb | 22 ---------------- .../helpers/helper_methods_override.rb | 12 +++------ 3 files changed, 9 insertions(+), 51 deletions(-) diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index 08f3865a1..1838154c9 100755 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -55,37 +55,23 @@ def facet_total_count(field_name) facet_values = response.dig('facet_counts', 'facet_fields', field_name) # Facet counts are included with names in the list, so divide by 2 total_unique_facets = facet_values ? (facet_values.length / 2) : 0 - - Rails.logger.info("Total unique facets for '#{field_name}': #{total_unique_facets}") - total_unique_facets + total_unique_facet rescue StandardError => e - # WIP: Change Later - Rails.logger.info("Error retrieving facets for '#{field_name}': #{e.message}") - Rails.logger.info(e.backtrace.join("\n")) + Rails.logger.error("Error retrieving facets for '#{field_name}': #{e.message}") + Rails.logger.error(e.backtrace.join("\n")) 0 end def facet - Rails.logger.info('FACET ACTION STARTED') - Rails.logger.info("Request Parameters: #{params.inspect}") - begin - facet_field = params[:id] - Rails.logger.info("Facet Field: #{facet_field}") + facet_field_name = params[:id] super - # Calculate the total unique facet count and append it to the response - @total_unique_facets = facet_total_count(facet_field) - # @response[:facet_total_count] = total_facet_count - # Log the response object if available - Rails.logger.info("Facet Response: #{@response.inspect}") if @response + # Calculate the total unique facet count + @total_unique_facets = facet_total_count(facet_field_name) rescue StandardError => e # Capture any errors that occur and log them Rails.logger.error("Error during facet action: #{e.message}") Rails.logger.error(e.backtrace.join("\n")) - # Optionally, re-raise the error or render a fallback response - raise e - ensure - Rails.logger.info('FACET ACTION COMPLETED') end end diff --git a/app/overrides/components/blacklight/facet_field_pagination_component_override.rb b/app/overrides/components/blacklight/facet_field_pagination_component_override.rb index d2bb014f0..e9ee1132b 100644 --- a/app/overrides/components/blacklight/facet_field_pagination_component_override.rb +++ b/app/overrides/components/blacklight/facet_field_pagination_component_override.rb @@ -7,26 +7,4 @@ def initialize(facet_field:, total_unique_facets: nil) # Integrate total unique facets as an attribute for pagination @total_unique_facets = total_unique_facets end - # Converts a `Blacklight::Solr::FacetPaginator` to `Kaminari::PaginatableArray` to enable pagination utilizing Kaminari gem - # Based on `paginate` in Hyrax::Admin::Analytics::AnalyticsController - # https://github.com/samvera/hyrax/blob/hyrax-v4.0.0/app/controllers/hyrax/admin/analytics/analytics_controller.rb - # def convert_facet_to_paginated_array(facet_paginator, rows: 10, page_param: :page) - # return if facet_paginator.nil? - # # Retrieve all items - # items = facet_paginator.instance_variable_get(:@all) - # return if items.nil? || items.empty? - - # total_count = items.size - # # Calculate the total number of pages based on the number of rows per page - # total_pages = (total_count.to_f / rows.to_f).ceil - # # Extract the current page number from the response params, defaulting to 1 if not present - # page = params[page_param].presence&.to_i || 1 - # # Ensure the current page does not exceed the total number of pages - # current_page = [page, total_pages].min - - # # Use Kaminari to create a paginated collection from the items array - # # `.page(current_page)` sets the current page - # # `.per(rows)` sets the number of items to display per page - # Kaminari.paginate_array(items, total_count: total_count).page(current_page).per(rows) - # end end diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index f28760843..e90825102 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -5,12 +5,11 @@ def link_to_specific_page(scope, name, page, total_entries, **options) begin # Validate inputs - # raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages) raise ArgumentError, "Page number must be a positive integer - got #{page}" unless page.is_a?(Integer) && page.positive? specific_page_path = path_to_specific_page(scope, page, total_entries, options) - # Remove the :params and :param_name keys from the options hash before generating the link since they are irrelevant + # Remove unnecessary keys :params and :param_name from the options hash before generating the link options.except! :params, :param_name # Setting aria instead of rel for accessibility @@ -18,19 +17,15 @@ def link_to_specific_page(scope, name, page, total_entries, **options) if specific_page_path link_to(name || page, specific_page_path, options) - elsif block_given? - yield else Rails.logger.warn "Specific page path could not be generated for page: #{page}" - nil end rescue ArgumentError => e Rails.logger.error "Error in link_to_specific_page: #{e.message}" - nil rescue StandardError => e Rails.logger.error "Unexpected error in link_to_specific_page: #{e.message}" - nil end + nil end # Helper to generate the path for a specific page @@ -49,10 +44,9 @@ def path_to_specific_page(scope, page, total_entries, options = {}) Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page)).url rescue ArgumentError => e Rails.logger.info "Error in path_to_specific_page: #{e.message}" - nil rescue StandardError => e Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}\n#{e.backtrace.join("\n")}" - nil end + nil end end From 2e84dc6f308f63657d8700cafbbf399be5352c0b Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 17:34:54 -0500 Subject: [PATCH 25/36] move typecast into helper --- .../blacklight/facet_field_pagination_component.html.erb | 2 +- app/overrides/lib/kanamari/helpers/helper_methods_override.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index b20a00859..8d0c56e5d 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -33,7 +33,7 @@ <% if page == current_page %> <%= page %> <% else %> - <%= helpers.link_to_specific_page @facet_field.paginator,"#{page}", page.to_i, @total_unique_facets, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> + <%= helpers.link_to_specific_page @facet_field.paginator, page, @total_unique_facets, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> <% end %> diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index e90825102..97cba7208 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,7 +2,7 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, name, page, total_entries, **options) + def link_to_specific_page(scope, page, total_entries, **options) begin # Validate inputs raise ArgumentError, "Page number must be a positive integer - got #{page}" unless page.is_a?(Integer) && page.positive? @@ -16,7 +16,7 @@ def link_to_specific_page(scope, name, page, total_entries, **options) options[:aria] ||= { label: "Go to page #{page}" } if specific_page_path - link_to(name || page, specific_page_path, options) + link_to("#{page}" || page, specific_page_path, options) else Rails.logger.warn "Specific page path could not be generated for page: #{page}" end From b53c24b5ef260127a22c0b8ecc4305c184fd12ea Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 17:43:58 -0500 Subject: [PATCH 26/36] syntax --- app/controllers/catalog_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index 1838154c9..d565ade4e 100755 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -55,7 +55,7 @@ def facet_total_count(field_name) facet_values = response.dig('facet_counts', 'facet_fields', field_name) # Facet counts are included with names in the list, so divide by 2 total_unique_facets = facet_values ? (facet_values.length / 2) : 0 - total_unique_facet + total_unique_facets rescue StandardError => e Rails.logger.error("Error retrieving facets for '#{field_name}': #{e.message}") Rails.logger.error(e.backtrace.join("\n")) From dacff3d4a88a4b0037aa091d5852f9b1b075b92b Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 18:28:31 -0500 Subject: [PATCH 27/36] remove log, typecasting --- .../blacklight/facet_field_pagination_component.html.erb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 8d0c56e5d..3b738e22c 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -17,7 +17,6 @@ <% first_pages_count = 2 %> <% last_pages_count = 2 %> <% surrounding_pages = 4 %> - <% Rails.logger.info("Limit: #{limit}, Offset: #{offset}, Total Pages: #{total_pages}, Total Unique Facets: #{@total_unique_facets}")%> <% (1..total_pages).each do |page| %> @@ -33,7 +32,7 @@ <% if page == current_page %> <%= page %> <% else %> - <%= helpers.link_to_specific_page @facet_field.paginator, page, @total_unique_facets, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> + <%= helpers.link_to_specific_page @facet_field.paginator, page.to_i, @total_unique_facets, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> <% end %> From abae3cb84af159afd1eb4f238f29134516af2c02 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 18:55:28 -0500 Subject: [PATCH 28/36] argument adjustment --- .../facet_field_pagination_component.html.erb | 9 +++++++- .../helpers/helper_methods_override.rb | 23 ++++++++----------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 3b738e22c..626ab9d38 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -32,7 +32,14 @@ <% if page == current_page %> <%= page %> <% else %> - <%= helpers.link_to_specific_page @facet_field.paginator, page.to_i, @total_unique_facets, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %> + + <%= helpers.link_to_specific_page( + @facet_field.paginator, + { integer: page.to_i, string: "#{page}" }, + @total_unique_facets, + params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), + class: "page-link" + ) %> <% end %> diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 97cba7208..f83ff360d 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,23 +2,20 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, page, total_entries, **options) + def link_to_specific_page(scope, page_hash, total_entries, **options) begin - # Validate inputs - raise ArgumentError, "Page number must be a positive integer - got #{page}" unless page.is_a?(Integer) && page.positive? - - specific_page_path = path_to_specific_page(scope, page, total_entries, options) + specific_page_path = path_to_specific_page(scope, page_hash[:integer], total_entries, options) # Remove unnecessary keys :params and :param_name from the options hash before generating the link options.except! :params, :param_name # Setting aria instead of rel for accessibility - options[:aria] ||= { label: "Go to page #{page}" } + options[:aria] ||= { label: "Go to page #{page_hash[:string]}" } if specific_page_path - link_to("#{page}" || page, specific_page_path, options) + link_to(page_hash[:string] || page, specific_page_path, options) else - Rails.logger.warn "Specific page path could not be generated for page: #{page}" + Rails.logger.warn "Specific page path could not be generated for page: #{page_hash[:string]}" end rescue ArgumentError => e Rails.logger.error "Error in link_to_specific_page: #{e.message}" @@ -29,19 +26,19 @@ def link_to_specific_page(scope, page, total_entries, **options) end # Helper to generate the path for a specific page - def path_to_specific_page(scope, page, total_entries, options = {}) + def path_to_specific_page(scope, page_integer, total_entries, options = {}) begin # Calculate total pages manually limit = scope.instance_variable_get(:@limit) total_pages = (total_entries.to_f / limit).ceil - Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}" + Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page_integer}" # Validate inputs - raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive? - raise ArgumentError, "Page number exceeds total pages (#{total_pages})" if page > total_pages + raise ArgumentError, 'Page number must be a positive integer' unless page_integer.positive? + raise ArgumentError, "Page number exceeds total pages (#{total_pages})" if page_integer > total_pages # Generate URL using Kaminari's Page helper - Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page)).url + Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page_integer)).url rescue ArgumentError => e Rails.logger.info "Error in path_to_specific_page: #{e.message}" rescue StandardError => e From b3e1afdce13cf3ec4a62147167c1c417f505b5d6 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 19:11:13 -0500 Subject: [PATCH 29/36] syntax --- .../lib/kanamari/helpers/helper_methods_override.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index f83ff360d..7fe45ad78 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -10,12 +10,12 @@ def link_to_specific_page(scope, page_hash, total_entries, **options) options.except! :params, :param_name # Setting aria instead of rel for accessibility - options[:aria] ||= { label: "Go to page #{page_hash[:string]}" } + options[:aria] ||= { label: "Go to page #{page_hash[:integer]}" } if specific_page_path - link_to(page_hash[:string] || page, specific_page_path, options) + link_to(page_hash[:string] || page_hash[:integer], specific_page_path, options) else - Rails.logger.warn "Specific page path could not be generated for page: #{page_hash[:string]}" + Rails.logger.warn "Specific page path could not be generated for page: #{page_hash[:integer]}" end rescue ArgumentError => e Rails.logger.error "Error in link_to_specific_page: #{e.message}" From 9ce5cbeb7d7dfde5dce2ab24b85d8e6ed309ede4 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Fri, 3 Jan 2025 22:43:51 -0500 Subject: [PATCH 30/36] arguments, nil returns --- .../facet_field_pagination_component.html.erb | 2 +- .../kanamari/helpers/helper_methods_override.rb | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 626ab9d38..343094a0f 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -35,7 +35,7 @@ <%= helpers.link_to_specific_page( @facet_field.paginator, - { integer: page.to_i, string: "#{page}" }, + page, @total_unique_facets, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 7fe45ad78..a6f1fb7de 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -2,27 +2,29 @@ # [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page - def link_to_specific_page(scope, page_hash, total_entries, **options) + def link_to_specific_page(scope, page, total_entries, **options) begin - specific_page_path = path_to_specific_page(scope, page_hash[:integer], total_entries, options) + specific_page_path = path_to_specific_page(scope, page.to_i, total_entries, options) # Remove unnecessary keys :params and :param_name from the options hash before generating the link options.except! :params, :param_name # Setting aria instead of rel for accessibility - options[:aria] ||= { label: "Go to page #{page_hash[:integer]}" } + options[:aria] ||= { label: "Go to page #{page.to_i}" } if specific_page_path - link_to(page_hash[:string] || page_hash[:integer], specific_page_path, options) + link_to("#{page.to_i}" || page.to_i, specific_page_path, options) else - Rails.logger.warn "Specific page path could not be generated for page: #{page_hash[:integer]}" + Rails.logger.warn "Specific page path could not be generated for page: #{page.to_i}" + nil end rescue ArgumentError => e Rails.logger.error "Error in link_to_specific_page: #{e.message}" + nil rescue StandardError => e Rails.logger.error "Unexpected error in link_to_specific_page: #{e.message}" + nil end - nil end # Helper to generate the path for a specific page @@ -41,9 +43,10 @@ def path_to_specific_page(scope, page_integer, total_entries, options = {}) Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page_integer)).url rescue ArgumentError => e Rails.logger.info "Error in path_to_specific_page: #{e.message}" + nil rescue StandardError => e Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}\n#{e.backtrace.join("\n")}" + nil end - nil end end From 51b606455b946f315603b6ed993414d5f4bfd3b5 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 6 Jan 2025 15:31:47 -0500 Subject: [PATCH 31/36] pagination component tests --- .../facet_field_pagination_component_spec.rb | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spec/components/blacklight/facet_field_pagination_component_spec.rb diff --git a/spec/components/blacklight/facet_field_pagination_component_spec.rb b/spec/components/blacklight/facet_field_pagination_component_spec.rb new file mode 100644 index 000000000..28c6e1fd4 --- /dev/null +++ b/spec/components/blacklight/facet_field_pagination_component_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Blacklight::FacetFieldPaginationComponent, type: :component do + let(:facet_field) { double('facet_field') } + let(:total_unique_facets) { 42 } + + describe '#initialize' do + context 'when total_unique_facets is provided' do + it 'sets the facet_field and total_unique_facets attributes' do + component = Blacklight::FacetFieldPaginationComponent.new(facet_field: facet_field, total_unique_facets: total_unique_facets) + + expect(component.facet_field).to eq(facet_field) + expect(component.total_unique_facets).to eq(total_unique_facets) + end + end + + context 'when total_unique_facets is not provided' do + it 'sets the total_unique_facets attribute to nil' do + component = Blacklight::FacetFieldPaginationComponent.new(facet_field: facet_field) + + expect(component.facet_field).to eq(facet_field) + expect(component.total_unique_facets).to be_nil + end + end + end +end From 9f27743f72c153ca851e811f46e021cd17473ad1 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 6 Jan 2025 16:26:18 -0500 Subject: [PATCH 32/36] update error handling, tests --- .../helpers/helper_methods_override.rb | 13 ++---- .../kanamari/helpers/helper_methods_spec.rb | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 spec/lib/kanamari/helpers/helper_methods_spec.rb diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index a6f1fb7de..671d4e097 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -18,11 +18,8 @@ def link_to_specific_page(scope, page, total_entries, **options) Rails.logger.warn "Specific page path could not be generated for page: #{page.to_i}" nil end - rescue ArgumentError => e - Rails.logger.error "Error in link_to_specific_page: #{e.message}" - nil - rescue StandardError => e - Rails.logger.error "Unexpected error in link_to_specific_page: #{e.message}" + rescue => e + Rails.logger.error "#{e.message}" nil end end @@ -34,7 +31,7 @@ def path_to_specific_page(scope, page_integer, total_entries, options = {}) limit = scope.instance_variable_get(:@limit) total_pages = (total_entries.to_f / limit).ceil - Rails.logger.info "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page_integer}" + Rails.logger.debug "path_to_specific_page: total_entries=#{total_entries}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page_integer}" # Validate inputs raise ArgumentError, 'Page number must be a positive integer' unless page_integer.positive? @@ -42,9 +39,9 @@ def path_to_specific_page(scope, page_integer, total_entries, options = {}) # Generate URL using Kaminari's Page helper Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page_integer)).url rescue ArgumentError => e - Rails.logger.info "Error in path_to_specific_page: #{e.message}" + Rails.logger.error "Error in path_to_specific_page: #{e.message}" nil - rescue StandardError => e + rescue => e Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}\n#{e.backtrace.join("\n")}" nil end diff --git a/spec/lib/kanamari/helpers/helper_methods_spec.rb b/spec/lib/kanamari/helpers/helper_methods_spec.rb new file mode 100644 index 000000000..3d1805930 --- /dev/null +++ b/spec/lib/kanamari/helpers/helper_methods_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Kaminari::Helpers::HelperMethods do + let(:dummy_class) { Class.new { extend Kaminari::Helpers::HelperMethods } } + let(:scope) { double('FacetPaginator', instance_variable_get: 20) } + let(:valid_page) { 1 } + let(:total_entries) { 100 } + let(:options) { { some_option: 'value' } } + + describe '#link_to_specific_page' do + before do + # Stub Kaminari::Helpers::Page.new to return a mock URL + allow(Kaminari::Helpers::Page).to receive(:new).and_return(double(url: '/some_path')) + end + + it 'generates a valid link for correct input' do + result = dummy_class.path_to_specific_page(scope, valid_page, total_entries, options) + expect(Rails.logger).to_not receive(:error) + # Expect the method to return the mocked URL + expect(result).to eq('/some_path') + end + + + it 'logs and returns nil for invalid page input' do + invalid_page = -1 + expect(Rails.logger).to receive(:error).with(/Page number must be a positive integer/) + expect(dummy_class.link_to_specific_page(scope, invalid_page, total_entries, **options)) + .to be_nil + end + + it 'logs and returns nil if page exceeds total pages' do + invalid_page = 999 + expect(Rails.logger).to receive(:error).with(/Page number exceeds total pages/) + expect(dummy_class.link_to_specific_page(scope, invalid_page, total_entries, **options)) + .to be_nil + end + + it 'logs and returns nil if an unexpected error occurs' do + allow(Kaminari::Helpers::Page).to receive(:new).and_raise(StandardError, 'Simulated Kaminari Error') + expect(Rails.logger).to receive(:error).with(/Unexpected error in path_to_specific_page/) + expect(dummy_class.link_to_specific_page(scope, valid_page, total_entries, **options)) + .to be_nil + end + end +end From 50795332b153cd0556f90535d4c4a3d7d60442b1 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Mon, 6 Jan 2025 17:28:18 -0500 Subject: [PATCH 33/36] modified error handling --- .../helpers/helper_methods_override.rb | 19 +++++++------------ .../kanamari/helpers/helper_methods_spec.rb | 16 ++++++++++------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb index 671d4e097..0dea36a15 100644 --- a/app/overrides/lib/kanamari/helpers/helper_methods_override.rb +++ b/app/overrides/lib/kanamari/helpers/helper_methods_override.rb @@ -3,23 +3,18 @@ Kaminari::Helpers::HelperMethods.module_eval do # Helper to generate a link to a specific page def link_to_specific_page(scope, page, total_entries, **options) - begin - specific_page_path = path_to_specific_page(scope, page.to_i, total_entries, options) + specific_page_path = path_to_specific_page(scope, page.to_i, total_entries, options) # Remove unnecessary keys :params and :param_name from the options hash before generating the link - options.except! :params, :param_name + options.except! :params, :param_name # Setting aria instead of rel for accessibility - options[:aria] ||= { label: "Go to page #{page.to_i}" } + options[:aria] ||= { label: "Go to page #{page.to_i}" } - if specific_page_path - link_to("#{page.to_i}" || page.to_i, specific_page_path, options) - else - Rails.logger.warn "Specific page path could not be generated for page: #{page.to_i}" - nil - end - rescue => e - Rails.logger.error "#{e.message}" + if specific_page_path + link_to("#{page.to_i}", specific_page_path, options) + else + Rails.logger.warn "Specific page path could not be generated for page: #{page.to_i}" nil end end diff --git a/spec/lib/kanamari/helpers/helper_methods_spec.rb b/spec/lib/kanamari/helpers/helper_methods_spec.rb index 3d1805930..c1d53b55e 100644 --- a/spec/lib/kanamari/helpers/helper_methods_spec.rb +++ b/spec/lib/kanamari/helpers/helper_methods_spec.rb @@ -14,17 +14,19 @@ allow(Kaminari::Helpers::Page).to receive(:new).and_return(double(url: '/some_path')) end - it 'generates a valid link for correct input' do - result = dummy_class.path_to_specific_page(scope, valid_page, total_entries, options) - expect(Rails.logger).to_not receive(:error) - # Expect the method to return the mocked URL - expect(result).to eq('/some_path') - end + # it 'generates a valid link for correct input' do + # expect(Rails.logger).to_not receive(:error) + # allow(dummy_class).to receive(:link_to).and_return('link') + # # Mock link_to to check its arguments + # dummy_class.link_to_specific_page(scope, valid_page, total_entries, **options) + # # Expect the method to return the mocked URL + # end it 'logs and returns nil for invalid page input' do invalid_page = -1 expect(Rails.logger).to receive(:error).with(/Page number must be a positive integer/) + expect(Rails.logger).to receive(:warn).with(/Specific page path could not be generated for page/) expect(dummy_class.link_to_specific_page(scope, invalid_page, total_entries, **options)) .to be_nil end @@ -32,6 +34,7 @@ it 'logs and returns nil if page exceeds total pages' do invalid_page = 999 expect(Rails.logger).to receive(:error).with(/Page number exceeds total pages/) + expect(Rails.logger).to receive(:warn).with(/Specific page path could not be generated for page/) expect(dummy_class.link_to_specific_page(scope, invalid_page, total_entries, **options)) .to be_nil end @@ -39,6 +42,7 @@ it 'logs and returns nil if an unexpected error occurs' do allow(Kaminari::Helpers::Page).to receive(:new).and_raise(StandardError, 'Simulated Kaminari Error') expect(Rails.logger).to receive(:error).with(/Unexpected error in path_to_specific_page/) + expect(Rails.logger).to receive(:warn).with(/Specific page path could not be generated for page/) expect(dummy_class.link_to_specific_page(scope, valid_page, total_entries, **options)) .to be_nil end From cec941c4372eeac9695bb79e42beb9f181b5488b Mon Sep 17 00:00:00 2001 From: David Campbell Date: Tue, 7 Jan 2025 13:00:52 -0500 Subject: [PATCH 34/36] styling --- .../blacklight/facet_field_pagination_component.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index 343094a0f..ab167197b 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -50,7 +50,7 @@
-
+
<% if @facet_field.paginator.sort == 'index' -%> <%= t('blacklight.search.facets.sort.index') %> <%= helpers.link_to(t('blacklight.search.facets.sort.count'), sort_facet_url('count'), class: "sort_change numeric btn btn-outline-secondary", data: { blacklight_modal: "preserve" }) %> From b49d514ccc237cf1017c867054335e7605179563 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Wed, 8 Jan 2025 10:30:09 -0500 Subject: [PATCH 35/36] Only calculate total unique facets for facets in the target list --- .../blacklight/facet_field_pagination_component.html.erb | 2 +- app/controllers/catalog_controller.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/components/blacklight/facet_field_pagination_component.html.erb b/app/components/blacklight/facet_field_pagination_component.html.erb index ab167197b..ba97dbb22 100644 --- a/app/components/blacklight/facet_field_pagination_component.html.erb +++ b/app/components/blacklight/facet_field_pagination_component.html.erb @@ -13,7 +13,7 @@ <% limit = @facet_field.paginator.instance_variable_get(:@limit) %> <% offset = @facet_field.paginator.instance_variable_get(:@offset) %> <% current_page = (offset / limit) + 1 %> - <% total_pages = (@total_unique_facets.to_f / limit).ceil %> + <% total_pages = @total_unique_facets ? (@total_unique_facets.to_f / limit).ceil : 0 %> <% first_pages_count = 2 %> <% last_pages_count = 2 %> <% surrounding_pages = 4 %> diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index d565ade4e..b2882d0a3 100755 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -65,9 +65,10 @@ def facet_total_count(field_name) def facet begin facet_field_name = params[:id] + targeted_facet_list = ['affiliation_label_sim'] super - # Calculate the total unique facet count - @total_unique_facets = facet_total_count(facet_field_name) + # Only calculate total unique facets for facets in the target list + @total_unique_facets = targeted_facet_list.include?(facet_field_name) ? facet_total_count(facet_field_name) : 0 rescue StandardError => e # Capture any errors that occur and log them Rails.logger.error("Error during facet action: #{e.message}") From b4e8d836f0b9934cba8e906ae94eb295b4f10fc8 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Wed, 8 Jan 2025 13:24:20 -0500 Subject: [PATCH 36/36] valid link test --- .../kanamari/helpers/helper_methods_spec.rb | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/spec/lib/kanamari/helpers/helper_methods_spec.rb b/spec/lib/kanamari/helpers/helper_methods_spec.rb index c1d53b55e..85c675e9d 100644 --- a/spec/lib/kanamari/helpers/helper_methods_spec.rb +++ b/spec/lib/kanamari/helpers/helper_methods_spec.rb @@ -2,7 +2,12 @@ require 'rails_helper' RSpec.describe Kaminari::Helpers::HelperMethods do - let(:dummy_class) { Class.new { extend Kaminari::Helpers::HelperMethods } } + let(:dummy_class) do + Class.new do + extend ActionView::Helpers::UrlHelper + extend Kaminari::Helpers::HelperMethods + end + end let(:scope) { double('FacetPaginator', instance_variable_get: 20) } let(:valid_page) { 1 } let(:total_entries) { 100 } @@ -14,13 +19,13 @@ allow(Kaminari::Helpers::Page).to receive(:new).and_return(double(url: '/some_path')) end - # it 'generates a valid link for correct input' do - # expect(Rails.logger).to_not receive(:error) - # allow(dummy_class).to receive(:link_to).and_return('link') - # # Mock link_to to check its arguments - # dummy_class.link_to_specific_page(scope, valid_page, total_entries, **options) - # # Expect the method to return the mocked URL - # end + it 'generates a valid link for correct input' do + expect(Rails.logger).to_not receive(:error) + allow(dummy_class).to receive(:link_to).and_return('link') + # Mock link_to to check its arguments + dummy_class.link_to_specific_page(scope, valid_page, total_entries, **options) + # Expect the method to return the mocked URL + end it 'logs and returns nil for invalid page input' do