Skip to content

Commit

Permalink
starting_chars() sorts numbers last
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Dolski committed Jan 25, 2024
1 parent a004d24 commit 856027a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
37 changes: 21 additions & 16 deletions app/controllers/index_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/views/index_pages/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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" : ""}")
Expand Down

0 comments on commit 856027a

Please sign in to comment.