Skip to content

Commit c4ab88b

Browse files
jamiestamplauraghiorghisor-tw
authored andcommitted
Add a new optional title field to social media links
This title is primarily intended to be used for the "Other" service type. We need to be able to distinguish between the various channels. Nonetheless, the title input can also be used to provide multiple instances of any other channel type. Future commit will add the necessary validations.
1 parent d60aea6 commit c4ab88b

4 files changed

Lines changed: 88 additions & 6 deletions

File tree

app/models/configurable_document_types/topical_event.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@
111111
"block": "default_string",
112112
"attribute_path": ["url"],
113113
"translatable": true
114+
},
115+
"title": {
116+
"title": "Title",
117+
"block": "default_string",
118+
"attribute_path": ["title"],
119+
"translatable": true
114120
}
115121
}
116122
}
@@ -130,6 +136,9 @@
130136
},
131137
"url": {
132138
"type": "string"
139+
},
140+
"title": {
141+
"type": "string"
133142
}
134143
}
135144
}
@@ -156,7 +165,8 @@
156165
"attributes": ["social_media_links"],
157166
"fields": {
158167
"service_field": "social_media_service_name",
159-
"url_field": "url"
168+
"url_field": "url",
169+
"title_field": "title"
160170
}
161171
}
162172
}

app/presenters/publishing_api/payload_builder/block_content.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ def social_media_links(attribute)
4444
return [] if content.blank?
4545

4646
content.map do |item|
47-
# `item` looks something like `{"url"=>"foo", "social_media_service_name"=>"Facebook"}`
47+
# `item` looks something like `{"url"=>"foo", "social_media_service_name"=>"Facebook", "title"=> "Optional title"}`
4848
service_name = item["social_media_service_name"]
4949
service_url = item["url"]
50+
title = item["title"]
5051
{
51-
title: service_name,
52+
title: title.presence || service_name,
5253
service_type: service_name.parameterize, # "Google Plus" => "google-plus"
5354
href: service_url,
5455
}

features/fixtures/test_configurable_document_type.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@
138138
"block": "default_string",
139139
"attribute_path": ["url"],
140140
"translatable": true
141+
},
142+
"title": {
143+
"title": "Title",
144+
"block": "default_string",
145+
"attribute_path": ["title"],
146+
"translatable": true
141147
}
142148
}
143149
}
@@ -184,6 +190,9 @@
184190
},
185191
"url": {
186192
"type": "string"
193+
},
194+
"title": {
195+
"type": "string"
187196
}
188197
}
189198
}

test/unit/app/presenters/publishing_api/payload_builder/block_content_test.rb

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,78 @@ class PublishingApi::PayloadBuilder::BlockContentTest < ActiveSupport::TestCase
161161
end
162162

163163
test "social_media_links returns array of social media links" do
164+
value_of_links = [
165+
{ "social_media_service_name" => "Twitter", "url" => "https://twitter.com" },
166+
{ "social_media_service_name" => "Other", "title" => "Other 1", "url" => "https://example.com" },
167+
{ "social_media_service_name" => "Other", "title" => "Other 2", "url" => "https://personal.com" },
168+
]
169+
@block_content.stubs(:some_attribute).returns(value_of_links)
170+
171+
builder = PublishingApi::PayloadBuilder::BlockContent.new(@item)
172+
173+
expected_payload = [
174+
{
175+
title: "Twitter",
176+
service_type: "twitter",
177+
href: "https://twitter.com",
178+
},
179+
{
180+
title: "Other 1",
181+
service_type: "other",
182+
href: "https://example.com",
183+
},
184+
{
185+
title: "Other 2",
186+
service_type: "other",
187+
href: "https://personal.com",
188+
},
189+
]
190+
assert_equal expected_payload, builder.send(:social_media_links, :some_attribute)
191+
end
192+
193+
test "social_media_links defaults title to service name when no title provided" do
164194
value_of_links = [{ "social_media_service_name" => "twitter", "url" => "https://example.com" }]
165195
@block_content.stubs(:some_attribute).returns(value_of_links)
166196

167197
builder = PublishingApi::PayloadBuilder::BlockContent.new(@item)
168198

169199
expected_payload = [
170200
{
171-
title: value_of_links.first["social_media_service_name"],
172-
service_type: value_of_links.first["social_media_service_name"].parameterize,
173-
href: value_of_links.first["url"],
201+
title: "twitter",
202+
service_type: "twitter",
203+
href: "https://example.com",
204+
},
205+
]
206+
assert_equal expected_payload, builder.send(:social_media_links, :some_attribute)
207+
end
208+
209+
test "social_media_links uses custom title when provided" do
210+
value_of_links = [{ "social_media_service_name" => "Twitter", "url" => "https://twitter.com/govuk", "title" => "GOV.UK on Twitter" }]
211+
@block_content.stubs(:some_attribute).returns(value_of_links)
212+
213+
builder = PublishingApi::PayloadBuilder::BlockContent.new(@item)
214+
215+
expected_payload = [
216+
{
217+
title: "GOV.UK on Twitter",
218+
service_type: "twitter",
219+
href: "https://twitter.com/govuk",
220+
},
221+
]
222+
assert_equal expected_payload, builder.send(:social_media_links, :some_attribute)
223+
end
224+
225+
test "social_media_links falls back to service name when title is blank" do
226+
value_of_links = [{ "social_media_service_name" => "Facebook", "url" => "https://facebook.com/govuk", "title" => "" }]
227+
@block_content.stubs(:some_attribute).returns(value_of_links)
228+
229+
builder = PublishingApi::PayloadBuilder::BlockContent.new(@item)
230+
231+
expected_payload = [
232+
{
233+
title: "Facebook",
234+
service_type: "facebook",
235+
href: "https://facebook.com/govuk",
174236
},
175237
]
176238
assert_equal expected_payload, builder.send(:social_media_links, :some_attribute)

0 commit comments

Comments
 (0)