Skip to content

Commit

Permalink
Maintain lead organisations first when publishing HTML attachments
Browse files Browse the repository at this point in the history
- add test for ordering.
- update existing test to handle new mock requirements
- add delegation for call_for_evidence and consultation_response so
  that they can retrieve lead and supporting organisations
  separately to pass to presenter.
- add tests for delegated methods
  • Loading branch information
KludgeKML committed Jul 22, 2024
1 parent 1fc69b6 commit 521daaa
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
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

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
17 changes: 17 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,23 @@ class ConsultationResponseTest < ActiveSupport::TestCase
assert_equal [], response.organisations
end

test "delegates lead_organisations and supporting_organisations to the parent" do
response = build(:consultation_outcome)

assert_equal [], response.lead_organisations
assert_equal [], response.supporting_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

0 comments on commit 521daaa

Please sign in to comment.