Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Jan 3, 2025
1 parent d34ef21 commit 92f9bf0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 51 deletions.
26 changes: 6 additions & 20 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 3 additions & 9 deletions app/overrides/lib/kanamari/helpers/helper_methods_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,27 @@
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
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
nil
end

# Helper to generate the path for a specific page
Expand All @@ -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

0 comments on commit 92f9bf0

Please sign in to comment.