Skip to content

Commit b96317a

Browse files
committed
Make routes and parts dynamic
We can now define arbitrary routes (and their equivalent arbitrary parts) by simply declaring a different `"part": "/route"` property under the `forms`. In this case we only want one route and part - topical event about pages - but you can imagine how we'd extend this to support all possible Corporate Information Pages, for example.
1 parent 72dfa7a commit b96317a

6 files changed

Lines changed: 37 additions & 24 deletions

File tree

app/models/configurable_document_type.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,24 @@ def properties
8787
@schema["attributes"] || {}
8888
end
8989

90+
def parts
91+
parts = []
92+
@forms.each_value.flat_map do |form|
93+
next unless form["fields"]
94+
95+
form["fields"].each do |_, field|
96+
parts << field["part"] if field["part"]
97+
end
98+
end
99+
parts.uniq
100+
end
101+
90102
def fields_for_part(part_key)
91103
@forms.each_value.flat_map do |form|
92104
next unless form["fields"]
93105

94-
form["fields"].select { |_, field| field["part_of"] == part_key }.map do |key, field|
95-
{ "key" => key, "part_name" => field["part_name"] }
106+
form["fields"].select { |_, field| field["part"] == part_key }.map do |key, field|
107+
{ "key" => key, "part_name" => field["part_name"], "part" => part_key }
96108
end
97109
end
98110
end

app/models/configurable_document_types/topical_event.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@
3838
"title": "Title",
3939
"block": "default_string",
4040
"attribute_path": ["block_content", "about_title"],
41-
"part_of": "about_page_parts",
41+
"part": "/about",
4242
"part_name": "title",
4343
"translatable": true
4444
},
4545
"about_summary": {
4646
"title": "Summary",
4747
"block": "default_textarea",
4848
"attribute_path": ["block_content", "about_summary"],
49-
"part_of": "about_page_parts",
49+
"part": "/about",
5050
"part_name": "summary",
5151
"translatable": true
5252
},
5353
"about_body": {
5454
"title": "Body",
5555
"block": "govspeak",
5656
"attribute_path": ["block_content", "about_body"],
57-
"part_of": "about_page_parts",
57+
"part": "/about",
5858
"part_name": "body",
5959
"translatable": true
6060
}
@@ -224,9 +224,6 @@
224224
},
225225
"settings": {
226226
"base_path_prefix": "/government/topical-events",
227-
"additional_routes": [
228-
"/about"
229-
],
230227
"publishing_api_schema_name": "topical_event",
231228
"publishing_api_document_type": "topical_event",
232229
"rendering_app": "frontend",

app/models/standard_edition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def base_path
107107
end
108108

109109
def additional_routes
110-
type_instance.settings["additional_routes"] || []
110+
type_instance.parts
111111
end
112112

113113
def type_instance

app/presenters/publishing_api/payload_builder/block_content.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def social_media_links(attribute)
5757
end
5858

5959
def parts(_attribute)
60-
# TODO: make more abstract by passing a parameter to the 'parts' method
61-
# so that we're not hardcoding this only for 'about page' usage.
62-
PayloadBuilder::MultipleParts.for(item, "about_page_parts")
60+
item.type_instance.parts.map do |part|
61+
PayloadBuilder::MultipleParts.for(item, part)
62+
end
6363
end
6464
end
6565
end

app/presenters/publishing_api/payload_builder/multiple_parts.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module PublishingApi
22
module PayloadBuilder
33
class MultipleParts
4-
attr_reader :item, :part_of
4+
attr_reader :item, :part
55

6-
def self.for(item, part_of)
7-
new(item, part_of).call
6+
def self.for(item, part)
7+
new(item, part).call
88
end
99

10-
def initialize(item, part_of)
10+
def initialize(item, part)
1111
@item = item
12-
@part_of = part_of
12+
@part = part
1313
end
1414

1515
def call
@@ -19,11 +19,13 @@ def call
1919
private
2020

2121
def parts
22-
item.type_instance.fields_for_part(part_of).map do |field|
23-
{
24-
field["part_name"].to_sym => item.block_content&.public_send(field["key"]),
25-
}
22+
hash = {
23+
slug: part.gsub("/", ""),
24+
}
25+
item.type_instance.fields_for_part(part).each_with_object(hash) do |field, obj|
26+
obj[field["part_name"].to_sym] = item.block_content&.public_send(field["key"])
2627
end
28+
hash
2729
end
2830
end
2931
end

test/unit/app/models/configurable_document_type_test.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ class ConfigurableDocumentTypeTest < ActiveSupport::TestCase
165165
"title" => "Title",
166166
"block" => "default_string",
167167
"attribute_path" => %w[block_content about_title],
168-
"part_of" => "about_page_parts",
168+
"part" => "/about",
169169
"part_name" => "title",
170170
"translatable" => true,
171171
},
172172
"about_summary" => {
173173
"title" => "Summary",
174174
"block" => "default_textarea",
175175
"attribute_path" => %w[block_content about_summary],
176-
"part_of" => "about_page_parts",
176+
"part" => "/about",
177177
"part_name" => "summary",
178178
"translatable" => true,
179179
},
@@ -189,13 +189,15 @@ class ConfigurableDocumentTypeTest < ActiveSupport::TestCase
189189
{
190190
"key" => "about_title",
191191
"part_name" => "title",
192+
"part" => "/about",
192193
},
193194
{
194195
"key" => "about_summary",
195196
"part_name" => "summary",
197+
"part" => "/about",
196198
},
197199
]
198-
assert_equal expected_fields, document_type.fields_for_part("about_page_parts")
200+
assert_equal expected_fields, document_type.fields_for_part("/about")
199201
end
200202

201203
test "#form creates a flattened hash of fields if no form key is provided" do

0 commit comments

Comments
 (0)