Skip to content

Commit

Permalink
Merge pull request #2843 from sciencehistory/external_id_search_on_ad…
Browse files Browse the repository at this point in the history
…min_work_page

Admin work list page: filter by external_id
  • Loading branch information
eddierubeiz authored Jan 15, 2025
2 parents 5a5f910 + fb535fd commit 4564b46
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/controllers/admin/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,15 @@ def build_search(params)
scope = Work.all
if params[:title_or_id].present?
sanitized_search_phrase = Work.sanitize_sql_like(params[:title_or_id])
scope = scope.where("(title ILIKE ? OR friendlier_id ILIKE ? OR id = ?)",

# This expression will match any element of the external_id jsonb array,
# as long as its value is sanitized_search_phrase, regardless of the value of 'category'.
jsonb_match_value = "'[{\"value\": \"#{sanitized_search_phrase}\"}]'::jsonb"

# This condition attempts to match external_id against jsonb_match_value:
matches_external_id = "json_attributes -> 'external_id' @> #{jsonb_match_value}"

scope = scope.where("(title ILIKE ? OR friendlier_id ILIKE ? OR #{matches_external_id} OR id = ?)",
"%#{sanitized_search_phrase}%",
"%#{sanitized_search_phrase}%",
Work.type_for_attribute(:id).cast(params[:title_or_id])
Expand Down
19 changes: 19 additions & 0 deletions spec/controllers/admin/works_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,25 @@
expect(rows.length).to eq 2
end

# object bib item accn aspace interview
context "filtering by external_id" do
let!(:works) { [
create(:work, title: "work_a", external_id: [
Work::ExternalId.new({"value"=>"1111", "category"=>"interview"}),
]),
create(:work, title: "work_b", external_id: [
Work::ExternalId.new({"value"=>"2222", "category"=>"aspace"}),
]),
] }
it "can filter on external id, regardless of the category of the ID" do
get :index, params: { title_or_id: "1111" }
rows = response.parsed_body.css('.table.admin-list tbody tr')
expect(rows.length).to eq 1
get :index, params: { title_or_id: "2222" }
rows = response.parsed_body.css('.table.admin-list tbody tr')
expect(rows.length).to eq 1
end
end

end
end
Expand Down

0 comments on commit 4564b46

Please sign in to comment.