Skip to content

Commit

Permalink
Merge pull request #8953 from alphagov/worldwide-organisation-pages-f…
Browse files Browse the repository at this point in the history
…eatures

Worldwide organisation pages features
  • Loading branch information
jkempster34 authored Apr 17, 2024
2 parents 6509553 + 3f1a679 commit 3b371a2
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 3 deletions.
15 changes: 15 additions & 0 deletions app/helpers/admin/tabbed_nav_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,19 @@ def editionable_worldwide_organisation_nav_items(worldwide_organisation, current
},
]
end

def worldwide_organisation_page_nav_items(page, current_path)
[
{
label: "Page",
href: edit_admin_editionable_worldwide_organisation_page_path(page.edition, page),
current: current_path == edit_admin_editionable_worldwide_organisation_page_path(page.edition, page),
},
{
label: sanitize("Attachments #{tag.span(page.attachments.count, class: 'govuk-tag govuk-tag--grey') if page.attachments.count.positive?}"),
href: admin_worldwide_organisation_page_attachments_path(page),
current: current_path == admin_worldwide_organisation_page_attachments_path(page),
},
]
end
end
15 changes: 15 additions & 0 deletions app/models/editionable_worldwide_organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ def process_associations_before_save(new_edition)

add_trait CloneDefaultImageTrait

class ClonePagesTrait < Edition::Traits::Trait
def process_associations_before_save(new_edition)
@edition.pages.each do |page|
new_page = page.dup

page.attachments.each do |attachment|
new_page.attachments << attachment.deep_clone
end
new_edition.pages << new_page
end
end
end

add_trait ClonePagesTrait

include AnalyticsIdentifierPopulator
self.analytics_prefix = "WO"

Expand Down
24 changes: 24 additions & 0 deletions app/models/worldwide_organisation_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class WorldwideOrganisationPage < ApplicationRecord
validates :corporate_information_page_type_id,
presence: true,
exclusion: { in: [CorporateInformationPageType::AboutUs.id], message: "Type cannot be `About us`" }
validate :unique_worldwide_organisation_and_page_type, on: :create, if: :edition

delegate :display_type_key, to: :corporate_information_page_type

include Attachable

def title(_locale = :en)
corporate_information_page_type.title(edition)
Expand All @@ -18,4 +23,23 @@ def corporate_information_page_type
def corporate_information_page_type=(type)
self.corporate_information_page_type_id = type && type.id
end

def publicly_visible?
true
end

def access_limited?
false
end

private

def unique_worldwide_organisation_and_page_type
current_page_types = edition.pages.map(&:corporate_information_page_type_id).flatten
duplicate_page = current_page_types.include?(corporate_information_page_type_id)

if duplicate_page
errors.add(:base, "Another '#{display_type_key.humanize}' page already exists for this worldwide organisation")
end
end
end
8 changes: 6 additions & 2 deletions app/views/admin/worldwide_organisation_pages/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@
{
text: type,
value: value,
selected: form.object.corporate_information_page_type_id == value,
}
end,
} %>
<% end %>
<%= render "govuk_publishing_components/components/textarea", {
<%= render "govuk_publishing_components/components/character_count", {
textarea: {
label: {
text: "Summary",
heading_size: "l",
},
name: "worldwide_organisation_page[summary]",
id: "worldwide_organisation_page_summary",
value: form.object.summary,
rows: 4,
error_items: errors_for(form.object.errors, :summary),
},
id: "worldwide_organisation_page_summary",
maxlength: 160,
} %>
<% if Flipflop.govspeak_visual_editor? %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/admin/worldwide_organisation_pages/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<% content_for :context, @worldwide_organisation.title %>
<% content_for :error_summary, render(Admin::ErrorSummaryComponent.new(object: @worldwide_organisation_page)) %>
<%= render "components/secondary_navigation", {
aria_label: "Worldwide organisation page navigation",
items: secondary_navigation_tabs_items(@worldwide_organisation_page, request.path),
} %>
<%= render partial: "form", locals: {worldwide_organisation_page: @worldwide_organisation_page} %>
</div>

Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })
get :confirm_destroy, on: :member
end
end
resources :worldwide_organisation_pages, only: [] do
resources :attachments, except: [:show] do
get :confirm_destroy, on: :member
end
end

resources :detailed_guides, path: "detailed-guides", except: [:index]
resources :people do
resources :translations, controller: "person_translations" do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddContentIdToWorldwideOrganisationPages < ActiveRecord::Migration[7.1]
def change
add_column :worldwide_organisation_pages, :content_id, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_03_25_104416) do
ActiveRecord::Schema[7.1].define(version: 2024_04_10_133547) do
create_table "assets", charset: "utf8mb3", force: :cascade do |t|
t.string "asset_manager_id", null: false
t.string "variant", null: false
Expand Down Expand Up @@ -1217,6 +1217,7 @@
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "content_id"
t.index ["edition_id"], name: "index_worldwide_organisation_pages_on_edition_id"
end

Expand Down
8 changes: 8 additions & 0 deletions features/editionable-worldwide-organisations.feature
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ Feature: Editionable worldwide organisations
And I visit the pages tab for the worldwide organisation
Then I should see that the list of pages for the worldwide organisation is empty

Scenario: Managing attachments for a worldwide organisation page
Given an editionable worldwide organisation "Test Worldwide Organisation" with a "Personal information charter" page
When I visit the pages tab for the worldwide organisation
And I click the "Edit" link for the "Personal information charter" page
And I click the Attachments tab
And I upload a file attachment with the title "Beard Length Statistics 2014" and the file "dft_statistical_data_set_sample.csv"
Then The "Beard Length Statistics 2014" attachment should have uploaded successfully

@javascript
Scenario: Reordering home page offices for a worldwide organisation
Given An editionable worldwide organisation "Test Worldwide Organisation" with home page offices "Home page office 1" and "Home page office 2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@
end
end

When(/^I click the Attachments tab$/) do
click_link "Attachments"
end

When(/^I submit the confirmation form to delete the "([^"]*)" page$/) do |type|
worldwide_organisation_page = WorldwideOrganisationPage.last
expect(page).to have_current_path(confirm_destroy_admin_editionable_worldwide_organisation_page_path(worldwide_organisation_page.edition, worldwide_organisation_page))
Expand Down Expand Up @@ -332,3 +336,7 @@
expect(last_item).to have_button("Up")
expect(last_item).not_to have_button("Down")
end

Then(/^The "(.*?)" attachment should have uploaded successfully$/) do |attachment_title|
expect(page).to have_content("Attachment '#{attachment_title}' uploaded")
end
6 changes: 6 additions & 0 deletions test/factories/editionable_worldwide_organisations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
organisation.default_news_image = build(:featured_image_data)
end
end

trait(:with_pages) do
after :create do |organisation|
create(:worldwide_organisation_page, edition: organisation)
end
end
end

factory :draft_editionable_worldwide_organisation, parent: :editionable_worldwide_organisation, traits: [:draft]
Expand Down
19 changes: 19 additions & 0 deletions test/unit/app/helpers/admin/tabbed_nav_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,23 @@ def current_user

assert_equal expected_output, secondary_navigation_tabs_items(edition, edit_admin_editionable_worldwide_organisation_path(edition))
end

test "#secondary_navigation_tabs_items for worldwide organisation pages" do
page = build_stubbed(:worldwide_organisation_page)

expected_output = [
{
label: "Page",
href: edit_admin_editionable_worldwide_organisation_page_path(page.edition, page),
current: true,
},
{
label: "Attachments ",
href: admin_worldwide_organisation_page_attachments_path(page),
current: false,
},
]

assert_equal expected_output, secondary_navigation_tabs_items(page, edit_admin_editionable_worldwide_organisation_page_path(page.edition, page))
end
end
24 changes: 24 additions & 0 deletions test/unit/app/models/editionable_worldwide_organisation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,30 @@ class EditionableWorldwideOrganisationTest < ActiveSupport::TestCase
end
end

test "should clone pages when a new draft of published edition is created" do
published_worldwide_organisation = create(
:editionable_worldwide_organisation,
:published,
:with_pages,
)

draft_worldwide_organisation = published_worldwide_organisation.create_draft(create(:writer))

assert_equal published_worldwide_organisation.pages.first.attributes.except("id", "edition_id"), draft_worldwide_organisation.pages.first.attributes.except("id", "edition_id")
end

test "should clone page attachments when a new draft of published edition is created" do
page = create(:worldwide_organisation_page)
attachment = build(:file_attachment)
page.attachments << attachment
published_worldwide_organisation = create(:editionable_worldwide_organisation, :published, pages: [page])

draft_worldwide_organisation = published_worldwide_organisation.create_draft(create(:writer))

cloned_attachment = draft_worldwide_organisation.pages.first.attachments.first
assert_equal cloned_attachment.attributes.except("id", "attachable_id", "safely_resluggable"), attachment.attributes.except("id", "attachable_id", "safely_resluggable")
end

test "when destroyed, will remove its home page list for storing offices" do
world_organisation = create(:editionable_worldwide_organisation)
h = world_organisation.__send__(:home_page_offices_list)
Expand Down
10 changes: 10 additions & 0 deletions test/unit/app/models/worldwide_organisation_page_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class WorldwideOrganisationPageTest < ActiveSupport::TestCase
assert page.errors[:corporate_information_page_type_id].include?("Type cannot be `About us`")
end

test "should not be valid when a worldwide organisation page of that type already exists for the worldwide organisation" do
organisation = create(:editionable_worldwide_organisation)
create(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::TermsOfReference, edition: organisation)

page = build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::TermsOfReference, edition: organisation.reload)

assert_not page.valid?
assert page.errors[:base].include?("Another 'Terms of reference' page already exists for this worldwide organisation")
end

test "should derive title from type" do
page = build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::TermsOfReference)
assert_equal "Terms of reference", page.title
Expand Down

0 comments on commit 3b371a2

Please sign in to comment.