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

Already on GitHub? Sign in to your account

#909 - multiple columns to sortable #1766

Closed
wants to merge 4 commits into
from

Conversation

Projects
None yet
7 participants

awinabi commented Nov 13, 2012

Adding in this feature for #909.
This pull request will let pass multiple columns to sortable in the following formats

:sortable => [:first_name, :last_name]
:sortable => ["user.first_name", "user.last_name"]
:sortable => "first_name, last_name"

Added in a couple of specs as well

vjt commented Nov 16, 2012

Hi,

meta_search already provides this functionality: https://github.com/ernie/meta_search#sorting-columns - its sort strings look like first_name_and_last_name.asc - and it even supports associations.

I think that it would be best to re-use meta_search features rather that rebuilding them in Active Admin. As a proof of concept, consider the following monkeypatch:

ActiveAdmin::ResourceController::Collection::Sorting.class_eval do
  def sort_order(chain)
    params[:order] ||= active_admin_config.sort_order
    if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
      chain.metasearch(:meta_sort => [$1, $2].join('.')).relation
    else
      chain
    end
  end
end

Used like this in an index#column, for a Foo model having a belongs_to :contact:

column :contact, :sortable => 'contact_first_name_and_contact_last_name'

Works perfectly with ActiveAdmin 0.5.0 stable.

What do you think?

~Marcello

Contributor

pcreux commented Dec 11, 2012

Thanks for this @vjt. I assume we would just need to add this to the documentation right?

vjt commented Dec 11, 2012

@pcreux well, no - I propose to change ActiveAdmin::ResourceController::Collection::Sorting#sort_order's implementation, I wouldn't recommend to add monkeypatches in the documentation.

If you agree that using meta_search instead of re-implementing (and testing) this feature in ActiveAdmin, I can send a proper pull request.

Let me know.

vjt commented Dec 11, 2012

And, of course, @awinabi - what do you think?

awinabi commented Dec 11, 2012

well, I am new to the opensource world and have not really played with meta_search yet. @vjt u can go ahead with the pull request. thanks.

zzeni commented Jan 31, 2013

@vjt, the suggested patch works great, with just one little remark - the sorting of associated columns will stop working. I.e.:

class District < ActiveRecord::Base
  belongs_to :city
end
class City < ActiveRecord::Base
  has_many :districts
end

ActiveAdmin.register District do
  index do
    column :city, :sortable => 'cities.name'
  end

  controller do
    def scoped_collection
      end_of_association_chain.includes(:city)
    end
  end
end

won't work anymore, and has to be changed to:

column :city, :sortable => 'city_name'

vjt commented Feb 7, 2013

@zzeni ok, great to hear. Sorry for my late reply, I'm a bit overwhelmed lately. I'll fix this compatibility issue and send the pull request asap.

Cheers :-).

Owner

seanlinsley commented Mar 15, 2013

@vjt how about that pull request? 🐱

vjt commented Mar 15, 2013

@daxter forked & bundling now, working on it - thanks for the heads up 👍

Owner

seanlinsley commented Mar 15, 2013

Moved up there

@ghost

ghost commented Aug 16, 2013

This stopped working in 0.6.0; any suggestions on how to fix? ...

uninitialized constant ActiveAdmin::ResourceController::Collection (NameError)
from /Users/aaron/.rvm/gems/ruby-1.9.3-p429/gems/activeadmin-0.6.0/lib/active_admin.rb:75:in `setup'
from /Users/aaron/git/brightbytes/config/initializers/active_admin.rb:16:in `<top (required)>'

Hello,

If I understand correctly, multiple columns sorting isn't available out of the box yet. What is the current best way to achieve it?
In my case I need to sort on two DB columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment