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 document to collection by slug #8254

Merged
merged 10 commits into from
Sep 28, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.app-view-document-collection-document-search-results__table {
.govuk-table__cell:nth-child(2) {
white-space: nowrap;
}

.govuk-table__cell,
.govuk-table__cell * {
vertical-align: middle;
}

form {
display: inline;

.govuk-button--secondary {
margin-left: govuk-spacing(3);
}
}
}
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
@@ -0,0 +1,40 @@
class Admin::DocumentCollectionGroupDocumentSearchController < Admin::BaseController
before_action :load_document_collection
before_action :load_document_collection_group
before_action :check_new_design_system_permissions
layout :get_layout

def search_options; end

def search
if params[:search_option].blank?
flash.now[:alert] = "Please select a search option"
render :search_options
end
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
flash.now[:alert] = "Please enter a search query" if params[:query] && params[:query].empty?
@results = Edition.published.with_title_containing(params[:query].strip) if params[:query].present?
end

private

def check_new_design_system_permissions
forbidden! unless new_design_system?
end

def get_layout
preview_design_system?(next_release: false) ? "design_system" : "admin"
end

def load_document_collection
@collection = DocumentCollection.includes(document: :latest_edition).find(params[:document_collection_id])
end

def load_document_collection_group
@group = @collection.groups.find(params[:group_id])
session[:document_collection_selected_group_id] = params[:group_id]
end
end
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
@@ -0,0 +1,50 @@
<% content_for :back_link do %>
<%= render "govuk_publishing_components/components/back_link", {
href: admin_document_collection_group_document_collection_group_memberships_path(@collection, @group),
} %>
<% end %>
<% content_for :page_title, "Add document" %>
<% content_for :title, "Add document" %>
<% content_for :context, @group.heading %>
<% content_for :title_margin_bottom, 4 %>

<%
search_options = [
{
value: "url",
text: "Search via URL",
},
{
value: "title-or-slug",
text: "Search via title",
},
]
%>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with url: admin_document_collection_group_search_options_path(@collection, @group) do %>
<%= render "govuk_publishing_components/components/radio", {
heading: "Search options",
name: "search_option",
id: "search_option",
heading_size: "l",
items: search_options,
} %>

<div class="govuk-button-group">
<%= render "govuk_publishing_components/components/button", {
text: "Next",
data_attributes: {
module: "gem-track-click",
"track-category": "form-button",
"track-action": "document-collection-search-options-next-button",
"track-label": "Next",
},
} %>

<%= link_to("Cancel", admin_document_collection_group_document_collection_group_memberships_path(@collection, @group), class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<% content_for :back_link do %>
<%= render "govuk_publishing_components/components/back_link", {
href: admin_document_collection_group_search_options_path(@collection, @group),
} %>
<% end %>
<% content_for :page_title, "Add document" %>
<% content_for :title, "Add document" %>
<% content_for :context, @group.heading %>
<% content_for :title_margin_bottom, 4 %>

<div class="govuk-grid-row">
<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",
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 results found. Search again using the <%= link_to "full URL", "#", class: "govuk-link govuk-link--no-visited-state" %>.</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|
title_span = tag.span(index_table_title_row(edition), class: "govuk-!-font-weight-bold")
view_link = link_to(sanitize("View #{tag.span(edition.title, class: "govuk-visually-hidden")}"), edition.public_url, class: "govuk-link")
add_button = 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",
data_attributes: {
module: "gem-track-click",
"track-category": "form-button",
"track-action": "add-document-to-collection-group-button",
"track-label": "Add",
},
)

[{ text: title_span }, { text: view_link + add_button }]
end,
} %>
</div>
<% end %>
</div>
<% end %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<%= render "govuk_publishing_components/components/button", {
text: "Remove",
destructive: true,
data_attributes: {
module: "gem-track-click",
"track-category": "form-button",
"track-action": "remove-document-from-collection-group-button",
"track-label": "Remove",
},
} %>
<%= link_to("Cancel", admin_document_collection_group_members_path(@collection, @group), class: "govuk-link govuk-link--no-visited-state") %>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
} %>

<p class="govuk-body govuk-!-text-align-right">
<%= link_to "Add document", "#", class: "govuk-link" %>
<%= link_to "Add document", admin_document_collection_group_search_options_path(@collection, @group), class: "govuk-link" %>
</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<%= render "govuk_publishing_components/components/button", {
text: "Delete",
destructive: true,
data_attributes: {
module: "gem-track-click",
"track-category": "form-button",
"track-action": "delete-document-collection-group-button",
"track-label": "Delete",
},
} %>
<%= link_to("Cancel", admin_document_collection_groups_path(@collection), class: "govuk-link govuk-link--no-visited-state") %>
</div>
Expand Down
10 changes: 6 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })

resources :authors, only: [:show]
resource :document_searches, only: [:show]

resources :document_collections, path: "collections", except: [:index] do
resources :document_collection_groups, as: :groups, path: "groups" do
get :search_options, to: "document_collection_group_document_search#search_options"
post :search_options, to: "document_collection_group_document_search#search"
get :search_title_slug, to: "document_collection_group_document_search#search_title_slug"
member { get :confirm_destroy }
resource :document_collection_group_membership,
as: :members,
path: "members",
only: [:destroy]
resource :document_collection_group_membership, as: :members, path: "members", only: [:destroy]
resources :document_collection_group_memberships, path: "members", only: %i[index destroy] do
get :confirm_destroy, on: :member
end
Expand All @@ -46,6 +47,7 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })
post "non-whitehall-member" => "document_collection_group_memberships#create_non_whitehall_member", as: :new_non_whitehall_member
post "groups/update_memberships" => "document_collection_groups#update_memberships", as: :update_group_memberships
end

resources :organisations do
resources :groups, except: [:show]
resources :corporate_information_pages do
Expand Down
16 changes: 13 additions & 3 deletions features/document-collections.feature
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Feature: Grouping documents into a collection
@design-system-only
Scenario: Deleting a group
Given a document collection "May 2012 Update" exists
And a the document collection "May 2012 Update" has a group with the heading "Temporary group"
And the document collection "May 2012 Update" has a group with the heading "Temporary group"
When I delete the group "Temporary group"
Then I can see that the group "Temporary group" has been deleted

Expand All @@ -61,12 +61,22 @@ Feature: Grouping documents into a collection
Then I can see that the group "Brand new group" has been added

@design-system-only
Scenario: Adding a new group
Scenario: Editing a group
Given a document collection "May 2012 Update" exists
And a the document collection "May 2012 Update" has a group with the heading "Group to be edited"
And the document collection "May 2012 Update" has a group with the heading "Group to be edited"
When I edit the group "Group to be edited"'s heading to "Interesting new heading"
Then I can see that the heading has been updated to "Interesting new heading"

@design-system-only
Scenario: Adding a document to a group via title
Given a document collection "Collection" exists
And the document collection "Collection" has a group with the heading "Group"
And a published document "Document 1" exists
When I select to add a new document to the collection group through "Search via title"
And I search by "title" for "Document 1"
And I add "Document 1" to the document collection
Then I should see "Document 1" in the list for the collection group "Group"

@design-system-only
Scenario: Removing a document from a group
Given a published publication called "Document to be removed" in a published document collection
Expand Down
27 changes: 25 additions & 2 deletions features/step_definitions/document_collection_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@
expect(body.index(doc_title1) < body.index(doc_title2)).to be(true)
end

And(/^a the document collection "([^"]*)" has a group with the heading "([^"]*)"$/) do |collection_title, heading|
And(/^the document collection "([^"]*)" has a group with the heading "([^"]*)"$/) do |collection_title, heading|
document_collection = DocumentCollection.find_by!(title: collection_title)
create(:document_collection_group, heading:, document_collection:)
@group = create(:document_collection_group, heading:, document_collection:)
end

When(/^I delete the group "(.*?)"$/) do |title|
Expand Down Expand Up @@ -249,3 +249,26 @@
expected_order = list.hashes.map(&:values).flatten
expect(actual_order).to eq(expected_order)
end

When(/^I select to add a new document to the collection group through "([^"]*)"$/) do |search_option|
visit admin_document_collection_group_document_collection_group_memberships_path(@document_collection, @group)
click_link "Add document"
choose search_option
click_button "Next"
end

And(/^I search by "([^"]*)" for "([^"]*)"$/) do |search_type, search_term|
fill_in "Search by #{search_type.downcase}", with: search_term
click_button "Search"
end

And(/^I add "([^"]*)" to the document collection$/) do |document_title|
expect(page).to have_content document_title
click_button "Add"
end

Then(/^I should see "([^"]*)" in the list for the collection group "([^"]*)"$/) do |document_title, collection_title|
expect(page).to have_content "'#{document_title}' added to '#{collection_title}'"
documents = all(".govuk-table__cell").map(&:text)
expect(documents).to include document_title
end
Loading