-
-
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.
Demonstrate (and ensure) custom search
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
spec/example_app/app/controllers/admin/log_entries_controller.rb
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 |
---|---|---|
@@ -1,4 +1,30 @@ | ||
module Admin | ||
class LogEntriesController < Admin::ApplicationController | ||
def filter_resources(resources, search_term:) | ||
return resources if search_term.blank? | ||
|
||
customer_ids = Customer.where( | ||
[ | ||
"name ILIKE ?", | ||
"%#{search_term}%", | ||
], | ||
).pluck(:id) | ||
order_ids = Order.joins(:customer).where( | ||
[ | ||
"customers.name ILIKE ?", | ||
"%#{search_term}%", | ||
], | ||
).pluck(:id) | ||
|
||
customers_filter = resources.where( | ||
logeable_type: "Customer", | ||
logeable_id: customer_ids, | ||
) | ||
orders_filter = resources.where( | ||
logeable_type: "Order", | ||
logeable_id: order_ids, | ||
) | ||
customers_filter.or(orders_filter) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require "rails_helper" | ||
|
||
RSpec.feature "Log search", type: :feature do | ||
it "filters logs related to a customer name", :js do | ||
c1 = create(:customer, name: "John Petrucci") | ||
c2 = create(:customer, name: "John Myung") | ||
c3 = create(:customer, name: "James LaBrie") | ||
o1a = create(:order, customer: c1) | ||
o2 = create(:order, customer: c2) | ||
o3 = create(:order, customer: c3) | ||
o1b = create(:order, customer: c1) | ||
|
||
[c1, c2, o1a, c3, o2, o3, o1b].each do |record| | ||
create(:log_entry, logeable: record) | ||
end | ||
|
||
visit admin_log_entries_path | ||
expect(page).to have_records_table(rows: 7) | ||
|
||
fill_in :search, with: "John" | ||
submit_search | ||
expect(page).to have_records_table(rows: 5) | ||
end | ||
|
||
def have_records_table(rows:) | ||
have_css("[role=main] table tr[data-url]", count: rows) | ||
end | ||
|
||
def submit_search | ||
page.execute_script("$('.search').submit()") | ||
end | ||
end |