Skip to content
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

Add basic Citation search #8406

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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