Skip to content

Commit

Permalink
feature: customizable first sorting option
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Oct 5, 2024
1 parent 10b2903 commit 1ecf863
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
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

0 comments on commit 1ecf863

Please sign in to comment.