diff --git a/app/controllers/index_pages_controller.rb b/app/controllers/index_pages_controller.rb index a4808466..30c1c084 100644 --- a/app/controllers/index_pages_controller.rb +++ b/app/controllers/index_pages_controller.rb @@ -133,10 +133,10 @@ def show starting_letters(reg_e_ids) end else - @count = 0 - @current_page = 1 - @terms = [] - @starting_letters = [] + @count = 0 + @current_page = 1 + @terms = [] + @starting_chars = [] end @breadcrumbable = @index_page end @@ -180,20 +180,25 @@ def assign_elements end end - def starting_letters(reg_e_ids) + def starting_chars(reg_e_ids) # This query doesn't take embargoes into account because doing so would # make it a lot slower. We assume that there are so few embargoed items - # with distinct starting letters that it's hardly ever going to matter. - sql = "SELECT DISTINCT(UNACCENT(UPPER(SUBSTR(string, 1, 1)))) AS alpha, COUNT(ae.id) - FROM ascribed_elements ae - INNER JOIN items i ON i.id = ae.item_id - WHERE i.institution_id = #{current_institution.id} - AND i.stage = #{Item::Stages::APPROVED} - AND ae.registered_element_id IN (#{reg_e_ids.join(",")}) - GROUP BY alpha - ORDER BY alpha;" - results = ActiveRecord::Base.connection.execute(sql) - results.select{ |row| row['count'] > 0 } + # with distinct starting characters that it will practically never matter. + sql = "SELECT alpha, count + FROM ( + SELECT DISTINCT(UNACCENT(UPPER(SUBSTR(string, 1, 1)))) AS alpha, + COUNT(ae.id) AS count + FROM ascribed_elements ae + INNER JOIN items i ON i.id = ae.item_id + WHERE i.institution_id = $1 + AND i.stage = $2 + AND ae.registered_element_id IN (#{reg_e_ids.join(",")}) + GROUP BY alpha + ) t + WHERE count > 0 + ORDER BY (alpha ~ '\\d')::int, alpha;" + values = [current_institution.id, Item::Stages::APPROVED] + ActiveRecord::Base.connection.exec_query(sql, "SQL", values) end end diff --git a/app/views/index_pages/show.html.haml b/app/views/index_pages/show.html.haml index a06d4eef..5d7cf806 100644 --- a/app/views/index_pages/show.html.haml +++ b/app/views/index_pages/show.html.haml @@ -36,7 +36,7 @@ permitted_params: @permitted_params, | max_links: 9) | -- if @starting_letters.any? +- if @starting_chars.any? %ul.nav.nav-tabs{role: "tablist"} %li.nav-item %button#browse-tab.nav-link{"data-bs-toggle": "tab", @@ -64,7 +64,7 @@ %ul.nav.nav-pills.justify-content-md-center.mb-0 %li.nav-item = link_to("All", @index_page, class: "nav-link #{(params[:letter].blank? && params[:q].blank?) ? "active" : ""}") - - @starting_letters.each do |row| + - @starting_chars.each do |row| %li.nav-item = link_to(row['alpha'], index_page_path(@index_page, letter: row['alpha']), class: "nav-link #{params[:letter]&.upcase == row['alpha'] ? "active" : ""}")