Skip to content
Snippets Groups Projects
Unverified Commit 060537ca authored by Arnaud Levy's avatar Arnaud Levy Committed by GitHub
Browse files

Order by slug (#2511)

parent 0e57af99
No related branches found
No related tags found
No related merge requests found
module LocalizableOrderBySlugScope
extend ActiveSupport::Concern
# Cf LocalizableOrderByNameScope
included do
scope :ordered, -> (language) {
localization_name_select = <<-SQL
COALESCE(
MAX(CASE WHEN localizations.language_id = '#{language.id}' THEN TRIM(LOWER(UNACCENT(localizations.slug))) END),
MAX(TRIM(LOWER(UNACCENT(localizations.slug)))) FILTER (WHERE localizations.rank = 1)
) AS localization_slug
SQL
joins(sanitize_sql_array([<<-SQL
LEFT JOIN (
SELECT
localizations.*,
ROW_NUMBER() OVER(PARTITION BY localizations.about_id ORDER BY localizations.created_at ASC) as rank
FROM
#{table_name.singularize}_localizations as localizations
) localizations ON #{table_name}.id = localizations.about_id
SQL
]))
.select("#{table_name}.*", localization_name_select)
.group("#{table_name}.id")
.order("localization_slug ASC")
}
end
end
......@@ -30,7 +30,7 @@ class Education::Program < ApplicationRecord
include AsIndirectObject
include Filterable
include Localizable
include LocalizableOrderByNameScope
include LocalizableOrderBySlugScope
include Sanitizable
include WebsitesLinkable
include WithAlumni
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment