Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: customizable first sorting option #3309

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions app/views/avo/partials/_table_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,18 +46,18 @@
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
sort_direction = nil
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"
%>
Expand Down
2 changes: 2 additions & 0 deletions lib/avo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/initializer/avo.tt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
18 changes: 18 additions & 0 deletions spec/features/avo/default_sort_column_and_direction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading