Skip to content

Commit

Permalink
Merge pull request #9556 from alphagov/refactor-republishing-helper
Browse files Browse the repository at this point in the history
Make republishable_content_types less clever
  • Loading branch information
richardTowers authored Nov 11, 2024
2 parents b67d517 + de90d1e commit ab6322a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 56 deletions.
37 changes: 32 additions & 5 deletions app/helpers/admin/republishing_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,23 @@ def bulk_content_type_metadata
end

def republishable_content_types
editionable_content_types = Edition.descendants.select { |descendant|
descendant.descendants.count.zero?
}.map(&:to_s)
editionable_content_types = %w[
CallForEvidence
CaseStudy
Consultation
CorporateInformationPage
DetailedGuide
DocumentCollection
WorldwideOrganisation
FatalityNotice
LandingPage
NewsArticle
Publication
Speech
StatisticalDataSet
]

[editionable_content_types, non_editionable_content_types].flatten.sort
[*editionable_content_types, *non_editionable_content_types].sort
end

def republishable_content_types_select_options
Expand Down Expand Up @@ -101,7 +113,22 @@ def republishing_index_bulk_republishing_rows
end

def non_editionable_content_types
ApplicationRecord.subclasses.select { |subclass| subclass.included_modules.include? PublishesToPublishingApi }.map(&:to_s)
%w[
Contact
Government
HistoricalAccount
OperationalField
Organisation
Person
PolicyGroup
Role
RoleAppointment
StatisticsAnnouncement
TakePartPage
TopicalEvent
TopicalEventAboutPage
WorldLocationNews
]
end

def content_ids_string_to_array(content_ids_string)
Expand Down
63 changes: 12 additions & 51 deletions test/unit/app/helpers/admin/republishing_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

class Admin::RepublishingHelperTest < ActionView::TestCase
test "#republishable_content_types returns a sorted list combining valid document types and other publishable content types" do
# we need to eager load here to ensure we have all the models
Rails.application.eager_load!

expected_content_types = omnipresent_content_types.sort
result_minus_test_types = republishable_content_types.reject { |type| content_types[:test_specific].include? type }
# Ignore "abstract" Edition descendants
unexpected_content_types = %w[Announcement GenericEdition Publicationesque SearchableEdition]

assert_equal expected_content_types, result_minus_test_types
expected_content_types = (
Edition.descendants.map(&:to_s) + non_editionable_content_types - unexpected_content_types
).reject { _1.include?("Test::") }.sort

assert_equal expected_content_types, republishable_content_types
end

test "#non_editionable_content_types returns a list of non-editionable content types" do
# we need to eager load here to ensure we have all the models
Rails.application.eager_load!
expected_non_editionable_models = ApplicationRecord.subclasses
.select { |subclass| subclass.included_modules.include? PublishesToPublishingApi }
.map(&:to_s)
.sort

assert_equal content_types[:omnipresent_non_editionable], non_editionable_content_types.sort
assert_equal expected_non_editionable_models, non_editionable_content_types.sort
end

test "#republishing_index_bulk_republishing_rows capitalises the first letter of the bulk content type" do
Expand Down Expand Up @@ -45,9 +51,6 @@ class Admin::RepublishingHelperTest < ActionView::TestCase
end

test "#republishable_content_types_select_options creates select options from republishable_content_types" do
# we need to eager load here to ensure we have all the models
Rails.application.eager_load!

options = republishable_content_types_select_options

assert_includes options, {
Expand Down Expand Up @@ -126,45 +129,3 @@ class Admin::RepublishingHelperTest < ActionView::TestCase
assert_equal expected_rows, confirm_documents_by_content_ids_edition_rows([document_a, document_b])
end
end

def omnipresent_content_types
content_types[:omnipresent_editionable].concat(content_types[:omnipresent_non_editionable])
end

def content_types
{ omnipresent_editionable: %w[CallForEvidence
CaseStudy
Consultation
CorporateInformationPage
DetailedGuide
DocumentCollection
WorldwideOrganisation
FatalityNotice
LandingPage
NewsArticle
Publication
Speech
StatisticalDataSet],
omnipresent_non_editionable: %w[Contact
Government
HistoricalAccount
OperationalField
Organisation
Person
PolicyGroup
Role
RoleAppointment
StatisticsAnnouncement
TakePartPage
TopicalEvent
TopicalEventAboutPage
WorldLocationNews],
test_specific: %w[GenericEdition
SearchableEdition
Edition::AlternativeFormatProviderTest::EditionWithAlternativeFormat
Edition::AppointmentTest::EditionWithAppointment
Edition::WorldwideOrganisationTest::EditionWithWorldwideOrganisations
Edition::ImagesTest::EditionWithImages
Edition::StatisticalDataSetsTest::EditionWithStatisticalDataSets
Edition::LimitedAccessTest::LimitedByDefaultEdition] }
end

0 comments on commit ab6322a

Please sign in to comment.