Skip to content

Commit 1250f4b

Browse files
committed
Make can_be_associated_with_topical_events? config-driven
For configurable document types only, we now check the configuration of the content type to decide whether or not the content type can be associated with topical events. This method is/may be used to determine which bits of the UI to render to the publisher, etc.
1 parent 677a5ba commit 1250f4b

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

app/models/concerns/edition/topical_events.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def process_associations_before_save(edition)
3232
add_trait Trait
3333
end
3434

35+
# This method can be deleted when all legacy content types have been migrated to
36+
# being config-driven (which has its own implementation of the method).
3537
def can_be_associated_with_topical_events?
3638
true
3739
end

app/models/standard_edition.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ def allows_features?
7878
type_instance.settings["features_enabled"]
7979
end
8080

81+
def can_be_associated_with_topical_events?
82+
[
83+
ConfigurableContentBlocks::Path.new("topical_event_ids"), # Legacy: delete when topical events migrated
84+
ConfigurableContentBlocks::Path.new("topical_event_document_ids"),
85+
].any? { |path| field_paths.include?(path) }
86+
end
87+
8188
def can_be_marked_political?
8289
type_instance.settings["history_mode_enabled"]
8390
end

test/unit/app/models/edition/topical_events_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,26 @@ class Edition::TopicalEventsTest < ActiveSupport::TestCase
6262

6363
assert_equal topical_event.document, new_edition.topical_event_documents.first
6464
end
65+
66+
test "can_be_associated_with_topical_events? returns true for legacy content types that include the module" do
67+
self.class.const_set("DummyLegacyEdition", Class.new(Edition) do
68+
include Edition::TopicalEvents
69+
end)
70+
71+
edition = DummyLegacyEdition.new
72+
assert edition.can_be_associated_with_topical_events?
73+
end
74+
75+
test "can_be_associated_with_topical_events? can be overridden by the class that includes it (e.g. to make its return value config-driven)" do
76+
self.class.const_set("DummyStandardEdition", Class.new(Edition) do
77+
include Edition::TopicalEvents
78+
79+
def can_be_associated_with_topical_events?
80+
false
81+
end
82+
end)
83+
84+
edition = DummyStandardEdition.new
85+
assert_not edition.can_be_associated_with_topical_events?
86+
end
6587
end

test/unit/app/models/standard_edition_test.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,81 @@ class StandardEditionTest < ActiveSupport::TestCase
410410
assert_not_includes missing_translations, :en
411411
end
412412

413+
test "it allows topical event associations if the configurable document type configuration references it" do
414+
test_type_with_config_driven_topical_events =
415+
build_configurable_document_type(
416+
"test_type_with_config_driven_topical_events", {
417+
"forms" => {
418+
"documents" => {
419+
"fields" => {
420+
"topical_event_documents" => {
421+
"title" => "Topical events",
422+
"block" => "select_with_search_tagging",
423+
"container" => "topical_event_documents",
424+
"attribute_path" => %w[topical_event_document_ids],
425+
"translatable" => false,
426+
},
427+
},
428+
},
429+
},
430+
}
431+
)
432+
433+
ConfigurableDocumentType.setup_test_types(test_type_with_config_driven_topical_events)
434+
435+
assert StandardEdition.new(configurable_document_type: "test_type_with_config_driven_topical_events").can_be_associated_with_topical_events?
436+
end
437+
438+
test "it does not allow topical event associations if the configurable document type configuration doesn't reference it" do
439+
test_type_without_topical_events =
440+
build_configurable_document_type(
441+
"test_type_without_topical_events", {
442+
"forms" => {
443+
"documents" => {
444+
"fields" => {
445+
"field_attribute" => {
446+
"title" => "Test Attribute",
447+
"block" => "govspeak",
448+
"attribute_path" => %w[block_content field_attribute],
449+
"translatable" => true,
450+
},
451+
},
452+
},
453+
},
454+
}
455+
)
456+
457+
ConfigurableDocumentType.setup_test_types(test_type_without_topical_events)
458+
459+
assert_not StandardEdition.new(configurable_document_type: "test_type_without_topical_events").can_be_associated_with_topical_events?
460+
end
461+
462+
# Legacy: delete when topical events have been migrated
463+
test "it allows topical event associations if the configurable document type configuration references legacy topical events" do
464+
test_type_with_legacy_topical_events =
465+
build_configurable_document_type(
466+
"test_type_with_legacy_topical_events", {
467+
"forms" => {
468+
"documents" => {
469+
"fields" => {
470+
"topical_events" => {
471+
"title" => "Topical events",
472+
"block" => "select_with_search_tagging",
473+
"container" => "topical_events",
474+
"attribute_path" => %w[topical_event_ids],
475+
"translatable" => false,
476+
},
477+
},
478+
},
479+
},
480+
}
481+
)
482+
483+
ConfigurableDocumentType.setup_test_types(test_type_with_legacy_topical_events)
484+
485+
assert StandardEdition.new(configurable_document_type: "test_type_with_legacy_topical_events").can_be_associated_with_topical_events?
486+
end
487+
413488
test "conditionally requires worldwide organisation and world location associations" do
414489
test_type = build_configurable_document_type(
415490
"test_type", {

0 commit comments

Comments
 (0)