Skip to content

Commit 64fdeab

Browse files
authored
Add ability for admins to filter list of organization by name (#1768)
* Add filterrific gem * Implement search by organization name via filterritic on admins#organization * Add system to spec to ensure filtering organizations as admin works * Remove change only used for testing
1 parent a33fdf0 commit 64fdeab

File tree

10 files changed

+106
-20
lines changed

10 files changed

+106
-20
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ gem "cocoon"
1616
gem "devise", '>= 4.7.1'
1717
gem "devise_invitable"
1818
gem "dotenv-rails"
19+
gem "filterrific"
1920
gem "flipper"
2021
gem "flipper-active_record"
2122
gem "flipper-ui"

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ GEM
163163
fakeredis (0.8.0)
164164
redis (~> 4.1)
165165
ffi (1.12.2)
166+
filterrific (5.2.1)
166167
flipper (0.18.0)
167168
flipper-active_record (0.18.0)
168169
activerecord (>= 5.0, < 7)
@@ -533,6 +534,7 @@ DEPENDENCIES
533534
factory_bot_rails
534535
faker
535536
fakeredis
537+
filterrific
536538
flipper
537539
flipper-active_record
538540
flipper-ui

app/assets/config/manifest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//= link filterrific/filterrific-spinner.gif
12
//= link_tree ../images
23
//= link_directory ../javascripts .js
34
//= link_directory ../stylesheets .css

app/assets/javascripts/application.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//= require popper
1616
//= require bootstrap
1717
// require jquery_ujs
18+
//= require filterrific/filterrific-jquery
1819
//= require bootstrap-select
1920
//= require bootstrap/alert
2021
//= require fastclick
@@ -62,7 +63,10 @@ $(document).ready(function () {
6263
height: isMobile || isShortHeight ? 'auto' : 'parent',
6364
defaultView: isMobile ? 'listWeek' : 'month'
6465
});
66+
});
6567

68+
$(document).ready(function() {
69+
Filterrific.init();
6670
});
6771

6872
$(document).ready(function () {

app/controllers/admin/organizations_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ def update
1515
end
1616

1717
def index
18-
@organizations = Organization.alphabetized.all
18+
@filterrific = initialize_filterrific(
19+
Organization.alphabetized,
20+
params[:filterrific]
21+
) || return
22+
23+
@organizations = @filterrific.find.page(params[:page])
24+
25+
respond_to do |format|
26+
format.html
27+
format.js
28+
end
1929
end
2030

2131
def new

app/models/organization.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ def bottom(limit = 5)
104104

105105
include Geocodable
106106

107+
filterrific(
108+
available_filters: [
109+
:search_name
110+
]
111+
)
112+
107113
scope :alphabetized, -> { order(:name) }
114+
scope :search_name, ->(query) { where('name ilike ?', "%#{query}%") }
108115

109116
# NOTE: when finding Organizations, use Organization.find_by(short_name: params[:organization_id])
110117
def to_param
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<table class="table">
2+
<thead>
3+
<tr>
4+
<th>Organization</th>
5+
<th>Contact E-mail</th>
6+
<th class="date">Added On</th>
7+
<th class="text-right">Actions</th>
8+
</tr>
9+
</thead>
10+
11+
<tbody>
12+
<% @organizations.each do |organization| %>
13+
<tr class="<%= organization.short_name %>">
14+
<td><%= organization.name %></td>
15+
<td><%= link_to organization.email, "mailto:#{organization.email}" %></td>
16+
<td class="date"><%= organization.created_at.strftime("%F") %></td>
17+
<td class="text-right">
18+
<%= view_button_to admin_organization_path(organization.id) %>
19+
<%= edit_button_to edit_admin_organization_path(organization.id) %>
20+
<%= delete_button_to(admin_organization_path(organization.id), { confirm: confirm_delete_msg(organization.name) }) unless (Organization.count <= 1) %>
21+
</td>
22+
</tr>
23+
<% end %>
24+
</tbody>
25+
</table>

app/views/admin/organizations/index.html.erb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@
2727
<!-- Default box -->
2828
<div class="card">
2929
<div class="card-header">
30-
<div class="float-right">
31-
<h2 class="card-title">
32-
<%= new_button_to new_admin_organization_path, { text: "Add New Organization" } %>
33-
</h2>
34-
</div>
30+
<%= form_for_filterrific @filterrific, remote: true do |f| %>
31+
<div class="row flex-row justify-content-between align-items-center">
32+
<div class='col-3'>
33+
<%= f.label :search_name, "Search By Organization Name" %>
34+
<%= f.text_field(:search_name, class: 'filterrific-periodically-observed form-control') %>
35+
</div>
36+
<div class="col-3">
37+
<%= render_filterrific_spinner %>
38+
</div>
39+
<div clas="col-9">
40+
<div class="float-right">
41+
<%= new_button_to new_admin_organization_path, { text: "Add New Organization" } %>
42+
</div>
43+
</div>
44+
</div>
45+
<% end %>
3546
</div>
3647
<div class="card-body p-0">
37-
<table class="table">
38-
<thead>
39-
<tr>
40-
<th>Organization</th>
41-
<th>Contact E-mail</th>
42-
<th class="date">Added On</th>
43-
<th class="text-right">Actions</th>
44-
</tr>
45-
</thead>
46-
<tbody>
47-
<%= render partial: "organization_row", collection: @organizations %>
48-
49-
</tbody>
50-
</table>
48+
<div id="filterrific_results">
49+
<%= render( partial: 'list', locals: { organizations: @organizations }) %>
50+
</div>
5151
</div>
5252
</div>
5353
<!-- /.card -->
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<% js = escape_javascript(
2+
render(partial: 'list', locals: { organizations: @organizations })
3+
) %>
4+
$("#filterrific_results").html("<%= js %>")

spec/system/admin/organizations_system_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@
44
sign_in(@super_admin)
55
end
66

7+
it "filters by organizations by name in organizations index page" do
8+
foo_org = FactoryBot.create(:organization, name: 'foo')
9+
bar_org = FactoryBot.create(:organization, name: 'bar')
10+
baz_org = FactoryBot.create(:organization, name: 'baz')
11+
12+
visit admin_organizations_path
13+
14+
# All organizations listed on load
15+
[foo_org, bar_org, baz_org].each do |o|
16+
expect(page).to have_content(o.name)
17+
end
18+
19+
# Searching by 'ba' should remove the 'foo' organization
20+
# from the organizations list but keep the 'bar' and 'baz'
21+
# organization listed.
22+
fill_in "filterrific_search_name", with: "ba"
23+
24+
expect(page).not_to have_content(foo_org.name)
25+
[bar_org, baz_org].each do |o|
26+
expect(page).to have_content(o.name)
27+
end
28+
29+
# Searching by 'bar' should only have the 'bar' organization
30+
# listed.
31+
fill_in "filterrific_search_name", with: "bar"
32+
[foo_org, baz_org].each do |o|
33+
expect(page).not_to have_content(o.name)
34+
end
35+
36+
expect(page).to have_content(bar_org.name)
37+
end
38+
739
it "creates a new organization" do
840
allow(User).to receive(:invite!).and_return(true)
941
visit new_admin_organization_path

0 commit comments

Comments
 (0)