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

Maintain lead organisations first when publishing HTML attachments #9166

Merged
merged 1 commit into from
Jul 22, 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
4 changes: 4 additions & 0 deletions app/models/call_for_evidence_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def access_limited_object

delegate :organisations, to: :parent_attachable

delegate :lead_organisations, to: :parent_attachable

delegate :supporting_organisations, to: :parent_attachable

delegate :alternative_format_contact_email, to: :call_for_evidence

delegate :publicly_visible?, to: :parent_attachable
Expand Down
4 changes: 4 additions & 0 deletions app/models/consultation_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def access_limited_object

delegate :organisations, to: :parent_attachable

delegate :lead_organisations, to: :parent_attachable

delegate :supporting_organisations, to: :parent_attachable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test that can be added to ensure CallForEvidenceResponse and ConsultationResponse delegate these methods? As removing these lines would presumably cause some issues down the line!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Have added tests for this.


delegate :alternative_format_contact_email, to: :consultation

delegate :publicly_visible?, to: :parent_attachable
Expand Down
10 changes: 9 additions & 1 deletion app/presenters/publishing_api/html_attachment_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def links
def edition_links
{
parent: parent_content_ids, # please use the breadcrumb component when migrating document_type to government-frontend
organisations: parent.organisations.pluck(:content_id).uniq,
organisations: (lead_org_ids + supporting_org_ids).uniq,
primary_publishing_organisation:,
government: government_id,
}
Expand Down Expand Up @@ -95,6 +95,14 @@ def lead_org_id
parent.try(:lead_organisations).try(:first).try(:content_id)
end

def lead_org_ids
(parent.try(:lead_organisations) || []).pluck(:content_id)
end

def supporting_org_ids
(parent.try(:supporting_organisations) || []).pluck(:content_id)
end

def first_org_id
parent.try(:organisations).try(:first).try(:content_id)
end
Expand Down
10 changes: 10 additions & 0 deletions test/unit/app/models/call_for_evidence_response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,14 @@ class CallForEvidenceResponseTest < ActiveSupport::TestCase

assert_equal [], response.organisations
end

test "delegates lead_organisations and supporting_organisations to the parent" do
lead_organisation = create(:organisation)
supporting_organisation = create(:organisation)
call_for_evidence = create(:call_for_evidence, lead_organisations: [lead_organisation], supporting_organisations: [supporting_organisation])
response = build(:call_for_evidence_outcome, call_for_evidence:)

assert_equal [lead_organisation], response.lead_organisations
assert_equal [supporting_organisation], response.supporting_organisations
end
end
10 changes: 10 additions & 0 deletions test/unit/app/models/consultation_response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ class ConsultationResponseTest < ActiveSupport::TestCase
assert_equal [], response.organisations
end

test "delegates lead_organisations and supporting_organisations to the parent" do
lead_organisation = create(:organisation)
supporting_organisation = create(:organisation)
consultation = create(:consultation, lead_organisations: [lead_organisation], supporting_organisations: [supporting_organisation])
response = build(:consultation_outcome, consultation:)

assert_equal [lead_organisation], response.lead_organisations
assert_equal [supporting_organisation], response.supporting_organisations
end

test "allows HTML attachments" do
outcome = build(:consultation_outcome)
assert outcome.allows_html_attachments?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,34 @@ def present(record)
html_attachment = HtmlAttachment.last
# if an organisation has multiple translations, pluck returns
# duplicate content_ids because it constructs a left outer join
html_attachment.attachable.organisations.expects(:pluck).with(:content_id).returns(%w[abcdef abcdef])
lead_orgs = mock
lead_orgs.expects(:pluck).with(:content_id).returns(%w[abcdef])
html_attachment.attachable.expects(:lead_organisations).returns(lead_orgs).twice

supporting_orgs = mock
supporting_orgs.expects(:pluck).with(:content_id).returns(%w[abcdef])
html_attachment.attachable.expects(:supporting_organisations).returns(supporting_orgs)

assert_equal %w[abcdef], present(html_attachment).links[:organisations]
end

test "HtmlAttachment presents lead organisation content_ids before supporting organisation content_ids" do
create(:publication, :with_html_attachment, :published)

html_attachment = HtmlAttachment.last
# if an organisation has multiple translations, pluck returns
# duplicate content_ids because it constructs a left outer join
lead_orgs = mock
lead_orgs.expects(:pluck).with(:content_id).returns(%w[abcdef])
html_attachment.attachable.expects(:lead_organisations).returns(lead_orgs).twice

supporting_orgs = mock
supporting_orgs.expects(:pluck).with(:content_id).returns(%w[bcdefg])
html_attachment.attachable.expects(:supporting_organisations).returns(supporting_orgs)

assert_equal %w[abcdef bcdefg], present(html_attachment).links[:organisations]
end

test "HtmlAttachment presents primary_publishing_organisation" do
create(:publication, :with_html_attachment, :published)

Expand Down