-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search filters to dashboards (#947)
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,
- Loading branch information
1 parent
3c32333
commit 56ede63
Showing
7 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require "spec_helper" | ||
require "administrate/search" | ||
|
||
describe Administrate::Search::Query do | ||
subject { described_class.new(query) } | ||
|
||
context "when query is nil" do | ||
let(:query) { nil } | ||
|
||
it "treats nil as a blank string" do | ||
expect(subject.terms).to eq("") | ||
end | ||
end | ||
|
||
context "when query is blank" do | ||
let(:query) { "" } | ||
|
||
it "returns true if blank" do | ||
expect(subject).to be_blank | ||
end | ||
end | ||
|
||
context "when given a query with only terms" do | ||
let(:query) { "foo bar" } | ||
|
||
it "returns the parsed search terms" do | ||
expect(subject.terms).to eq("foo bar") | ||
end | ||
end | ||
|
||
context "when query includes filters" do | ||
let(:query) { "vip: active:" } | ||
|
||
it "is not blank" do | ||
expect(subject).to_not be_blank | ||
end | ||
|
||
it "parses filter syntax" do | ||
expect(subject.filters).to eq(["vip", "active"]) | ||
end | ||
end | ||
|
||
context "when query includes both filters and terms" do | ||
let(:query) { "vip: example.com" } | ||
|
||
it "splits filters and terms" do | ||
expect(subject.filters).to eq(["vip"]) | ||
expect(subject.terms).to eq("example.com") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters