Skip to content

Commit

Permalink
Search for documents and add it to the collection group
Browse files Browse the repository at this point in the history
Using the scope that is used by the edition filter in the documents search - :with_title_containing to search for live documents containing the query entered by user.
If query is empty then alert the user to enter a search query.
Link the Add link to the existing POST create_whitehall_member in the original document collection group membership controller.
This creates the membership link between the document and the collection group.
Redirect to the documents page if it is new design system.
  • Loading branch information
minhngocd committed Sep 25, 2023
1 parent f6585bb commit 70cf844
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.app-view-document-collection-document-search-results__table {
.govuk-table__cell,
.govuk-table__cell > * {
vertical-align: middle;
}

form {
display: inline;

.govuk-button--secondary {
margin-left: govuk-spacing(1);
vertical-align: middle;
}
}
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ $govuk-page-width: 1140px;
@import "./admin/views/dashboard-index";
@import "./admin/views/document-history-tab";
@import "./admin/views/document-collection-group-memberships-index";
@import "./admin/views/document-collection-group-document-search-search-title-slug";
@import "./admin/views/edit-edition";
@import "./admin/views/edit-person";
@import "./admin/views/edition-resource";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def search
redirect_to(action: :search_title_slug, document_collection_id: @collection, group_id: @group) if params[:search_option] == "title-or-slug"
end

def search_title_slug; end
def search_title_slug
flash.now[:alert] = "Please enter a search query" if params[:query] && params[:query].empty?
@results = Edition.live_edition.with_title_containing(params[:query].strip) if params[:query].present?
end

private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ def index; end
def create_whitehall_member
membership = DocumentCollectionGroupMembership.new(document: @document, document_collection_group: @group)
if membership.save
redirect_to admin_document_collection_groups_path(@collection),
notice: "'#{params[:title]}' added to '#{@group.heading}'"
title = @document.latest_edition.title
redirect_to create_redirect_path,
notice: "'#{title}' added to '#{@group.heading}'"
else
redirect_to admin_document_collection_groups_path(@collection),
redirect_to create_redirect_path,
alert: "#{membership.errors.full_messages.join('. ')}."
end
end
Expand Down Expand Up @@ -59,8 +60,16 @@ def destroy

private

def create_redirect_path
if get_layout == "design_system"
admin_document_collection_group_document_collection_group_memberships_path(@collection, @group)
else
admin_document_collection_groups_path(@collection)
end
end

def get_layout
design_system_actions = %w[index confirm_destroy destroy] if preview_design_system?(next_release: false)
design_system_actions = %w[index confirm_destroy destroy create_whitehall_member] if preview_design_system?(next_release: false)

if design_system_actions&.include?(action_name)
"design_system"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,50 @@
<% content_for :title_margin_bottom, 4 %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/search", {
label_text: 'Search by title or slug',
label_size: "l"
} %>
<div class="govuk-grid-column-two-thirds app-view-document-collection-document-search-bar">
<%= form_with method: :get do |_form| %>
<%= render "govuk_publishing_components/components/search", {
name: "query",
value: params[:query],
label_text: 'Search by title or slug',
label_size: "l"
} %>
<% end %>
</div>

<% if @results %>
<div class="govuk-grid-column-two-thirds app-view-document-collection-document-search-results">
<p class="govuk-heading-s govuk-!-margin-bottom-3"><%= pluralize(number_with_delimiter(@results.count), "document") %></p>
<hr class="govuk-section-break govuk-section-break--m govuk-section-break--visible">
<% if @results.empty? %>
<p class="govuk-body app-view-document-collection-document-search-results__no_documents">No documents found</p>
<% else %>
<div class="govuk-table--with-actions app-view-document-collection-document-search-results__table">
<%= render "govuk_publishing_components/components/table", {
rows: @results.map do |edition|
[
{
text: tag.span(index_table_title_row(edition), class: "govuk-!-font-weight-bold")
},
{
text:
link_to(sanitize("View #{tag.span(edition.title, class: "govuk-visually-hidden")}"), edition.public_url, class: "govuk-link") +
button_to("Add",
admin_document_collection_new_whitehall_member_path(@collection),
method: :post,
params: {
group_id: @group.id,
document_id: edition.document.id
},
aria: { label: "Add document to the document collection group" },
class: "gem-c-button govuk-button govuk-button--secondary"
)
}
]
end
} %>
</div>
<% end %>
</div>
<% end %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,42 @@ class Admin::DocumentCollectionGroupDocumentSearchControllerTest < ActionControl
assert_redirected_to admin_document_collection_group_search_title_slug_path(@collection, @group)
end

test "GET #search_title_slug renders search for title & slug" do
test "GET #search_title_slug without query renders search for title & slug page with no results section" do
get :search_title_slug, params: @request_params
assert_template "document_collection_group_document_search/search_title_slug"
assert_select ".app-view-document-collection-document-search-results", count: 0
end

view_test "GET #search_title_slug with a query returns the document with the query in the results" do
edition = build(:consultation, title: "Something", document: build(:document, slug: "something"))

mockLiveEditions = mock();
mockLiveEditions.expects(:with_title_containing).with("Something").once.returns([edition])

Edition.expects(:live_edition).once.returns(mockLiveEditions);

@request_params[:query] = "Something "
get :search_title_slug, params: @request_params

assert_template "document_collection_group_document_search/search_title_slug"
assert_select ".govuk-table__row .govuk-table__cell a[href='#{edition.public_url}']", text: "View #{edition.title}"
end

view_test "GET #search_title_slug with a query that returns no results renders empty results list" do
mockLiveEditions = mock();
mockLiveEditions.expects(:with_title_containing).with("Something").once.returns([])
Edition.expects(:live_edition).once.returns(mockLiveEditions);

@request_params[:query] = "Something "
get :search_title_slug, params: @request_params
assert_template "document_collection_group_document_search/search_title_slug"
assert_select ".govuk-body", text: /No documents found/
end

view_test "GET #search_title_slug with an empty query string shows an alert flash" do
@request_params[:query] = ""
get :search_title_slug, params: @request_params
assert_template "document_collection_group_document_search/search_title_slug"
assert_select ".gem-c-error-alert__message", text: /Please enter a search query/
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ def id_params
assert_select ".govuk-table__row:nth-child(1) .govuk-table__cell:nth-child(2) a[href='#{edition.public_url}']", text: "View #{edition.title}"
assert_select ".govuk-table__row:nth-child(1) .govuk-table__cell:nth-child(2) a[href='#{confirm_destroy_path}']", text: "Remove #{edition.title}"
end

test "POST #create_whitehall_member adds a whitehall document to a group and redirects" do
document = create(:publication).document
assert_difference "@group.reload.documents.size" do
post :create_whitehall_member, params: id_params.merge(document_id: document.id)
end
assert_redirected_to admin_document_collection_group_document_collection_group_memberships_path(@collection, @group)
end

test "POST #create_whitehall_member warns user when document not found" do
post :create_whitehall_member, params: id_params.merge(document_id: 1234, title: "blah")
assert_match %r{couldn't find.*blah}, flash[:alert]
end
end

0 comments on commit 70cf844

Please sign in to comment.