-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Collection filters #947
Collection filters #947
Conversation
This is going to be a great feature, I like the direction it’s going in and I appreciate the work you’ve put in to pick up a previous PR. I’ve been starting to think about what the I’m going to tag this with “post-1.0”, so it’s easy to pick up soon. |
I'll definitely keep it up to date, not least because I very much want this feature in Administrate proper, so I can get back to deploying the main line. If I have to wait, so be it, but I'd obviously prefer seeing it merged sooner than later - you know what they say about long-running feature branches ;) I might want to add a bit more features on top of this (somehow exposing the filters in the UI for starters), although I'd prefer keeping those in separate branches. I guess I can hold off on creating new PRs for those and still keep this one lean and mergeable. |
You could open more PRs, but based upon this one. As long as we keep this one updated it won't be too bad. I'm hoping we'll not be too long until we get this merged, regardless! |
What is the status on this? |
Rebased on the latest master, still applies cleanly and should be usable from our fork. Still awaiting that 1.0 release 🤞 |
Please release... |
We need a way to do this now too. Here's another plea for a release! |
Rebased on a fresh master, fixing a few conflicts, and I have added backwards compatibility with Ruby versions < 2.4 ( The current build failures are all because of the gemfiles are locked to a version of nokogiri with known vulnerabilities. Fixing this seems out of scope of this pull request:
|
Version 0.9.0 updates Nokogiri. Can we try rebuilding? |
Rebased on master, awaiting CI judgement 🤞 |
This is looking great, and thanks for keeping it up to date! I'm still planning on this for post-1.0, but we're moving close to that now. FWIW, my plan is to not merge too much in between now and then and this will go out in |
Thanks a lot Jacob!! I haven't seen your work until now while I was looking for my old PR :( Nice to had inspired your branch!! :D Cheers!! |
@nando Now we just need to make sure this branch doesn't fall by the wayside as well - here's hoping for a soonish merge 🤞 |
Rebased on the most recent master, still squeaky clean, ready for an easy merge :hint hint: 😇 |
@koppen nice job, hopefully @Graysonwright, @nickcharlton, or @tysongach can merge it in soon. Otherwise I'd love to see a fork run by you. I'd love to keep this going and add my own changes eventually. |
And just like that, this branch is now up to date with the master branch and both CI and Hound are showing green checkmarks. And there was much rejoicing! @hadees Thanks :) Not sure about the whole fork-business, but the branch at https://github.com/substancelab/administrate/tree/276-collection_filters should be perfectly functional. I can't promise to keep it up to date, but you should be able to work off it, though, I guess. |
@@ -149,5 +156,26 @@ class User < ActiveRecord::Base; end | |||
search.run | |||
end | |||
end | |||
|
|||
it "searches using a filter" do | |||
begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantBegin: Redundant begin block detected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, however if we want to maintain compatibility with rubies before 2.5, this begin block needs to be here.
Merge this pretty please? :D |
@nickcharlton - How long until v1 as I'm really looking forward to this feature in master. |
If we add: COLLECTION_FILTERS = { inactive: ->(resources) { resources.where("login_at < ?", 1.week.ago) } } to a dashboard, we can query the resources of that dashboard with bob inactive: to find users named "bob" who hasn't logged in the last week. If you already had the `inactive` scope you could define the filter like so to take advantage of existing ActiveRecord scopes (and other class methods on the resource class). COLLECTION_FILTERS = { inactive: ->(resources) { resources.inactive } } While the chosen hash-based syntax is a bit more verbose than simply exposing already defined scopes like so: # app/dashboards/customer_dashboard.rb COLLECTION_FILTERS = [:inactive] it allows us to define filters for use in Administrate without having to clutter the resource classes with scopes. It still allows us to add the simpler syntax in a backwards compatible way at some point down the line if we feel the need. For example it could end up looking like: COLLECTION_FILTERS = { vip: :vip, # This could call the method `vip` on resources inactive: ->(resources) { resources.where("login_at < ?", 1.week.ago) } } * Allow search_spec to be run on its own, * Introduce the concept of a search query - adding collection scopes/filters means we need to add more involved search query parsing; this gives us a place for that,
Thanks for everyone's patience on this one! I've merged now and we'll cut a release shortly. |
@nickcharlton - What are your thoughts on having filters show up as a dropdown? This would be very similar to how active_admin works. |
@ACPK, could you open a new issue with that? Perhaps with an example? And then we can take it from there. |
This adds the option to define a set of filters that can be triggered in order to reduce the number of resources listed on the index page of a dashboard. Currently the only way to trigger the filters is via the search field, using a syntax similar to the one used here on GitHub.
Why
Often we need to expose more detailed/involved queries than those currently capable via simple searches. Previously we'd have to add special actions that query the database correctly and customize the views with links to trigger those actions.
Example
For example, if we want to be able to find "inactive" customers (for some definition of inactive), we can add
to a dashboard. This way, we can query the resources of that dashboard by typing
bob inactive:
in the search field to find users named "bob" who hasn't logged in the last week.Credits
This is basically a reimplementation of @nando's work from #276, I merely took some of his thoughts and ideas and repackaged them in a way that makes sense for Administrate anno 2017.
Also in an effort to keep an otherwise huge pull request as small as possible, I haven't gone all the way that he did in the original pull request, so we're missing automatic filtering setup and exposing available filters in the UI.
Nor are the filters implemented here as flexible or powerful as those proposed in #276.
Further details
If you already had the
inactive
scope you could define the filter like the following to take advantage of existing ActiveRecord scopes (and other class methods on the resource class).While the chosen hash-based syntax is a bit more verbose than simply exposing already defined scopes like so:
it allows us to define filters for use in Administrate without having to clutter the resource classes with scopes.
Future proofing
The chosen syntax should allow us to add the above mentioned simpler syntax in a backwards compatible way at some point down the line if we feel the need. For example it could end up looking like:
but that's a step I am not ready to take just yet.
It also opens up for a way to have filters that take arguments, ie we could define:
and query it as
before:2016-01-12
which, again, is a step I am not ready to take here and now.