diff --git a/app/views/avo/partials/_table_header.html.erb b/app/views/avo/partials/_table_header.html.erb index 411524fca..32ff1e622 100644 --- a/app/views/avo/partials/_table_header.html.erb +++ b/app/views/avo/partials/_table_header.html.erb @@ -33,8 +33,11 @@ <% end %> <% fields.each_with_index do |field, index| %> <% + first_option = Avo.configuration.first_sorting_option.to_s + second_option = first_option == "desc" ? "asc" : "desc" + if params[:sort_by] == field.id.to_s - if params[:sort_direction] == 'asc' + if params[:sort_direction] == second_option sort_by = nil else sort_by = field.id @@ -43,10 +46,10 @@ if sort_by.present? case params[:sort_direction] when nil - sort_direction = 'desc' - when 'desc' - sort_direction = 'asc' - when 'asc' + sort_direction = first_option + when first_option + sort_direction = second_option + when second_option sort_direction = nil end else @@ -54,7 +57,7 @@ end else sort_by = field.id - sort_direction = 'desc' + sort_direction = first_option end text_classes = "text-gray-600 tracking-tight leading-tight text-xs font-semibold" %> diff --git a/lib/avo/configuration.rb b/lib/avo/configuration.rb index c578c896c..269d45179 100644 --- a/lib/avo/configuration.rb +++ b/lib/avo/configuration.rb @@ -54,6 +54,7 @@ class Configuration attr_accessor :is_admin_method attr_accessor :is_developer_method attr_accessor :search_results_count + attr_accessor :first_sorting_option def initialize @root_path = "/avo" @@ -115,6 +116,7 @@ def initialize @is_admin_method = :is_admin? @is_developer_method = :is_developer? @search_results_count = 8 + @first_sorting_option = :desc # :desc or :asc end def current_user_method(&block) diff --git a/lib/generators/avo/templates/initializer/avo.tt b/lib/generators/avo/templates/initializer/avo.tt index 05310d564..ec6fa82f4 100644 --- a/lib/generators/avo/templates/initializer/avo.tt +++ b/lib/generators/avo/templates/initializer/avo.tt @@ -115,6 +115,7 @@ Avo.configure do |config| # config.buttons_on_form_footers = true # config.field_wrapper_layout = true # config.resource_parent_controller = "Avo::ResourcesController" + # config.first_sorting_option = :desc # :desc or :asc ## == Branding == # config.branding = { diff --git a/spec/features/avo/default_sort_column_and_direction_spec.rb b/spec/features/avo/default_sort_column_and_direction_spec.rb index fa75be96f..5df62ead1 100644 --- a/spec/features/avo/default_sort_column_and_direction_spec.rb +++ b/spec/features/avo/default_sort_column_and_direction_spec.rb @@ -84,4 +84,22 @@ include_examples "sorts by", :country, :desc end end + + context "first_sorting_option" do + let!(:project) { create :project } + + it "desc is the default sort_direction" do + visit avo.resources_projects_path + + expect(page).to have_link href: avo.resources_projects_path(sort_by: :name, sort_direction: :desc) + end + + it "asc is the default sort_direction if configured" do + with_temporary_class_option(Avo.configuration, :first_sorting_option, :asc) do + visit avo.resources_projects_path + + expect(page).to have_link href: avo.resources_projects_path(sort_by: :name, sort_direction: :asc) + end + end + end end