Skip to content

Commit

Permalink
Add basic Citation search
Browse files Browse the repository at this point in the history
Allow basic filtering of Citations by source_url.
  • Loading branch information
garethrees authored and gbp committed Oct 11, 2024
1 parent 7a3ae91 commit 8ce3279
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/controllers/admin/citations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
class Admin::CitationsController < AdminController
def index
@query = params[:query]

citations = (
if @query
Citation.search(@query)
else
Citation
end
)

@citations =
Citation.
citations.
order(created_at: :desc).
paginate(page: params[:page], per_page: 50)
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/citation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class Citation < ApplicationRecord
or(where(citable: info_request_batch.info_requests))
end

def self.search(query)
where(<<~SQL, query: query)
lower(citations.source_url) LIKE lower('%'||:query||'%')
SQL
end

def applies_to_batch_request?
citable.is_a?(InfoRequestBatch)
end
Expand Down
11 changes: 11 additions & 0 deletions app/views/admin/citations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
</div>
</div>


<%= form_tag({}, method: :get, class: 'form form-search') do %>
<div class="input-append">
<%= text_field_tag 'query', params[:query], size: 30, class: 'input-large search-query' %>
<%= submit_tag 'Search', class: 'btn' %>
</div>

<span class="help-inline">(substring search, source_url)</span>
<% end %>
<%= render partial: 'admin/citations/list',
locals: { citations: @citations } %>
Expand Down
1 change: 1 addition & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Highlighted Features

* Add basic Citation searching in admin UI (Gareth Rees)
* Fix script/mailin when multiple EXCEPTION_NOTIFICATIONS_TO addresses are
specified (Graeme Porteous)
* Add example logrotate configuration (Graeme Porteous)
Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/admin/citations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
expect(assigns[:citations]).to all(be_a(Citation))
end

it 'assigns the query' do
get :index, params: { query: 'hello' }
expect(assigns[:query]).to eq('hello')
end

it 'filters citations by the search query' do
net = FactoryBot.create(:citation, source_url: 'https://example.net/a')
org = FactoryBot.create(:citation, source_url: 'https://example.org/b')
get :index, params: { query: 'example.net' }
expect(assigns[:citations]).to include(net)
expect(assigns[:citations]).not_to include(org)
end

it 'renders the correct template' do
expect(response).to render_template(:index)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/models/citation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ def setup_for_x_scope_data
end
end

describe '.search' do
subject { described_class.search(query) }

let!(:net) do
FactoryBot.create(:citation, source_url: 'https://example.net/story')
end

let!(:org) do
FactoryBot.create(:citation, source_url: 'https://example.org/story')
end

let(:query) { 'example.net' }

it { is_expected.to include(net) }
it { is_expected.not_to include(org) }
end

subject(:citation) { FactoryBot.build(:citation) }

describe 'associations' do
Expand Down

0 comments on commit 8ce3279

Please sign in to comment.