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

use Ransack to sort collections #2608

Closed
wants to merge 1 commit into from
Closed

use Ransack to sort collections #2608

wants to merge 1 commit into from

Conversation

seanlinsley
Copy link
Contributor

This is a continuation of where @vjt left off in #1994

Added functionality:

  • you can sort by multiple columns

Problems:

  • sorting on a virtual column doesn't work
  • when sorting on multiple columns, how do you tell which column (on an
    index table) is currently being sorted?

@seanlinsley
Copy link
Contributor Author

Maybe the better option would be to always use the foo_and_bar_desc style on the client side, but to parse that back into the array form so Ransack can understand it. That would have the added benefit of giving us a valid string for the CSS class to show which column is being sorted.

Added functionality:
* you can sort by multiple columns
Problems:
* sorting on a virtual column doesn't work
* when sorting on multiple columns, how do you tell which column (on an
index table) is currently being sorted?
@seanlinsley
Copy link
Contributor Author

As I said over on #2844 (comment), I don't think this is the right approach because Ransack can't handle everything that a developer would want to do.

So the question becomes: how can we add the ability to sort by multiple columns (e.g. first & last name) to our current sorting code?

@hirokihokari
Copy link

@seanlinsley Seems like this got all removed on master now?

@senid231
Copy link
Contributor

@seanlinsley what if we just extend AA with some sort of custom sorting option
something like:

ActiveAdmin.register Post do
  custom_sort full_name: ->(records, direction) { records.order([{last_name: direction, first_name: :asc}]) },
                       comments_count: :order_by_comments_count 
                       # shortcut for ->(records, direction) { records.order_by_comments_count(direction) }

  index do
    column :id # sorted by posts.id asc/desc, query param id_asc/id_desc
    column :full_name # sorted by lambda provided above, query param full_name_asc/full_name_desc
    column :reversed_name, sortable: :full_name do |r| # sorted same as :full_name
      r.full_name.reverse
    end
    column :comments, :comments_tag, sortable: :comments_count 
    # sorted by `order_by_comments_count` scope or class method, 
    # query param comments_count_asc/comments_count_desc
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants