diff --git a/app/controllers/template_folders_controller.rb b/app/controllers/template_folders_controller.rb index a3418015..05b19814 100644 --- a/app/controllers/template_folders_controller.rb +++ b/app/controllers/template_folders_controller.rb @@ -5,8 +5,9 @@ class TemplateFoldersController < ApplicationController def show @templates = @template_folder.templates.active.accessible_by(current_ability) - .preload(:author, :template_accesses).order(id: :desc) + .preload(:author, :template_accesses) @templates = Templates.search(@templates, params[:q]) + @templates = Templates::Order.call(@templates, cookies.permanent[:dashboard_templates_order]) @pagy, @templates = pagy(@templates, limit: 12) end diff --git a/app/controllers/templates_dashboard_controller.rb b/app/controllers/templates_dashboard_controller.rb index ad8d049a..f6964432 100644 --- a/app/controllers/templates_dashboard_controller.rb +++ b/app/controllers/templates_dashboard_controller.rb @@ -7,18 +7,12 @@ class TemplatesDashboardController < ApplicationController SHOW_TEMPLATES_FOLDERS_THRESHOLD = 9 TEMPLATES_PER_PAGE = 12 FOLDERS_PER_PAGE = 18 - LAST_USED_SQL = <<~SQL.squish - GREATEST( - COALESCE(MAX(templates.updated_at), '1970-01-01'), - COALESCE(MAX(submissions.created_at), '1970-01-01') - ) - SQL def index @template_folders = @template_folders.where(id: @templates.active.select(:folder_id)) @template_folders = TemplateFolders.search(@template_folders, params[:q]) - @template_folders = sort_template_folders(@template_folders) + @template_folders = sort_template_folders(@template_folders, cookies.permanent[:dashboard_templates_order]) @pagy, @template_folders = pagy( @template_folders, @@ -31,7 +25,7 @@ class TemplatesDashboardController < ApplicationController else @template_folders = @template_folders.reject { |e| e.name == TemplateFolder::DEFAULT_NAME } @templates = filter_templates(@templates) - @templates = sort_templates(@templates) + @templates = Templates::Order.call(@templates, cookies.permanent[:dashboard_templates_order]) limit = if @template_folders.size < 4 @@ -63,41 +57,36 @@ class TemplatesDashboardController < ApplicationController Templates.search(rel, params[:q]) end - def sort_template_folders(template_folders) - return template_folders.order(id: :desc) if params[:q].present? - - case cookies.permanent[:dashboard_templates_order] - when 'recently_used' - sorted_folders = - template_folders.left_joins(templates: :submissions) - .select("template_folders.*, #{LAST_USED_SQL} AS last_used_at") - .group('template_folders.id') - .order(Arel.sql("#{LAST_USED_SQL} DESC NULLS LAST")) - - TemplateFolder.from(sorted_folders, :template_folders) + def sort_template_folders(template_folders, order) + case order + when 'used_at' + subquery = + Template.left_joins(:submissions) + .group(:folder_id) + .select( + :folder_id, + Template.arel_table[:updated_at].maximum.as('updated_at_max'), + Submission.arel_table[:created_at].maximum.as('submission_created_at_max') + ) + + template_folders = template_folders.joins( + Template.arel_table + .join(subquery.arel.as('templates'), Arel::Nodes::OuterJoin) + .on(TemplateFolder.arel_table[:id].eq(Template.arel_table[:folder_id])) + .join_sources + ) + + template_folders.order( + Arel::Nodes::Case.new + .when(Template.arel_table[:submission_created_at_max].gt(Template.arel_table[:updated_at_max])) + .then(Template.arel_table[:submission_created_at_max]) + .else(Template.arel_table[:updated_at_max]) + .desc + ) when 'name' template_folders.order(name: :asc) else template_folders.order(id: :desc) end end - - def sort_templates(templates) - return templates.order(id: :desc) if params[:q].present? - - case cookies.permanent[:dashboard_templates_order] - when 'recently_used' - sorted_templates = - templates.left_joins(:submissions) - .select("templates.*, #{LAST_USED_SQL} AS last_used_at") - .group('templates.id') - .order(Arel.sql("#{LAST_USED_SQL} DESC NULLS LAST")) - - Template.from(sorted_templates, :templates) - when 'name' - templates.order(name: :asc) - else - templates.order(id: :desc) - end - end end diff --git a/app/views/shared/_pagination.html.erb b/app/views/shared/_pagination.html.erb index 2c365542..8eecc313 100644 --- a/app/views/shared/_pagination.html.erb +++ b/app/views/shared/_pagination.html.erb @@ -5,7 +5,7 @@ <%= @pagy.from %>-<%= local_assigns.fetch(:to, @pagy.to) %> of <%= local_assigns.fetch(:count, @pagy.count) %> <%= local_assigns[:items_name] || 'items' %> <%= local_assigns[:left_additional_html] %> -
+
<%= local_assigns[:right_additional_html] %>
<% if @pagy.prev %> @@ -13,7 +13,7 @@ <% else %> « <% end %> - + <%= t('page_number', number: @pagy.page) %> <% if @pagy.next %> diff --git a/app/views/shared/_templates_order_select.html.erb b/app/views/shared/_templates_order_select.html.erb index 62c63803..520ecae1 100644 --- a/app/views/shared/_templates_order_select.html.erb +++ b/app/views/shared/_templates_order_select.html.erb @@ -1,30 +1,30 @@ -<% dashboard_templates_order = cookies.permanent[:dashboard_templates_order] || 'creation_date' %> -