Skip to content

Commit 6362f67

Browse files
committed
Add social media support using content block approach
1 parent fe6857f commit 6362f67

11 files changed

Lines changed: 152 additions & 32 deletions

File tree

app/controllers/admin/editions_controller.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class Admin::EditionsController < Admin::BaseController
44

55
before_action :remove_blank_parameters
66
before_action :clean_edition_parameters, only: %i[create update]
7-
before_action :destroy_blank_social_media_accounts, only: %i[create update]
87
before_action :clear_scheduled_publication_if_not_activated, only: %i[create update]
98
before_action :clear_response_form_file_cache, only: %i[create update]
109
before_action :find_edition, only: %i[show edit update revise diff confirm_destroy destroy update_bypass_id update_image_display_option]
@@ -293,7 +292,6 @@ def permitted_edition_attributes
293292
default_news_image_attributes: %i[file file_cache id],
294293
nation_inapplicabilities_attributes: %i[id nation_id alternative_url excluded],
295294
fatality_notice_casualties_attributes: %i[id personal_details _destroy],
296-
social_media_accounts_attributes: %i[social_media_service_id url title _destroy id],
297295
document_attributes: [
298296
:id,
299297
:slug,
@@ -472,16 +470,6 @@ def clean_edition_parameters
472470
end
473471
end
474472

475-
def destroy_blank_social_media_accounts
476-
if edition_params[:social_media_accounts_attributes]
477-
edition_params[:social_media_accounts_attributes].each_pair do |_key, account|
478-
if account[:social_media_service_id].blank? && account[:url].blank? && account[:title].blank?
479-
account[:_destroy] = "1"
480-
end
481-
end
482-
end
483-
end
484-
485473
def clear_scheduled_publication_if_not_activated
486474
if params[:scheduled_publication_active] && params[:scheduled_publication_active].to_i.zero?
487475
edition_params.keys.each do |key|

app/controllers/admin/standard_editions_controller.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
class Admin::StandardEditionsController < Admin::EditionsController
33
rescue_from ConfigurableDocumentType::NotFoundError, with: :render_not_found
44

5-
def social_media_accounts
6-
find_edition
7-
@edition.social_media_accounts.build if @edition.social_media_accounts.empty?
8-
end
9-
105
def choose_type
116
@permitted_configurable_document_types = ConfigurableDocumentType.where_group(params[:group])
127
.select { |type| can?(current_user, type) }

app/models/configurable_content_blocks/factory.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def blocks
3434
"object" => {
3535
"default" => ->(_page) { ConfigurableContentBlocks::DefaultObject.new(self) },
3636
},
37+
"array" => {
38+
"social_media_accounts" => ->(_page) { ConfigurableContentBlocks::SocialMediaAccounts.new },
39+
},
3740
"date" => {
3841
"default" => ->(_page) { ConfigurableContentBlocks::DefaultDate.new },
3942
},
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module ConfigurableContentBlocks
2+
class SocialMediaAccounts
3+
def to_partial_path
4+
"admin/configurable_content_blocks/social_media_accounts"
5+
end
6+
7+
def publishing_api_payload(_schema, content)
8+
return [] if content.blank?
9+
10+
items = content.is_a?(Hash) ? content.values : Array(content)
11+
12+
items.filter_map do |item|
13+
next if item["_destroy"] == "1"
14+
next if item["social_media_service_id"].blank? && item["url"].blank?
15+
16+
service = SocialMediaService.find_by(id: item["social_media_service_id"])
17+
next if service.nil?
18+
19+
{
20+
href: item["url"],
21+
service_type: service.name.parameterize,
22+
title: item["title"].presence || service.name,
23+
}
24+
end
25+
end
26+
end
27+
end

app/models/configurable_document_types/topical_event.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,36 @@
2424
}
2525
}
2626
}
27+
},
28+
"social_media_links": {
29+
"title": "Social media accounts",
30+
"type": "array",
31+
"format": "social_media_accounts",
32+
"items": {
33+
"type": "object",
34+
"properties": {
35+
"social_media_service_id": {
36+
"title": "Service",
37+
"type": "integer"
38+
},
39+
"url": {
40+
"title": "URL",
41+
"type": "string"
42+
},
43+
"title": {
44+
"title": "Title",
45+
"type": "string"
46+
}
47+
},
48+
"validations": {
49+
"presence": {
50+
"attributes": ["social_media_service_id", "url"]
51+
},
52+
"uri": {
53+
"attributes": ["url"]
54+
}
55+
}
56+
}
2757
}
2858
}
2959
},

app/models/standard_edition.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class StandardEdition < Edition
88
include Edition::WorldLocations
99
include Edition::Organisations
1010
include Edition::WorldwideOrganisations
11-
include Edition::SocialMediaAccounts
1211
include HasBlockContent
1312
include StandardEdition::DefaultLeadImage
1413

@@ -62,11 +61,6 @@ def allows_file_attachments?
6261
type_instance.settings["file_attachments_enabled"]
6362
end
6463

65-
def can_be_associated_with_social_media_accounts?
66-
edit_screens = type_instance.settings.dig("edit_screens")
67-
edit_screens&.key?("social_media_accounts")
68-
end
69-
7064
def can_be_marked_political?
7165
type_instance.settings["history_mode_enabled"]
7266
end

app/models/standard_edition/block_content.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class StandardEdition::BlockContent
1111
"no_footnotes_allowed" => NoFootnotesInGovspeakValidator,
1212
"presence" => ActiveModel::Validations::PresenceValidator,
1313
"safe_html" => SafeHtmlValidator,
14+
"uri" => UriValidator,
1415
"valid_internal_path_links" => InternalPathLinksValidator,
1516
"duration" => DurationValidator,
1617
}.freeze
@@ -74,6 +75,8 @@ def attributes_class_for(attribute_config)
7475
case property_schema["type"]
7576
when "object"
7677
attribute key
78+
when "array"
79+
attribute key, default: -> { {} }
7780
else
7881
attribute key, property_schema["type"].to_sym
7982
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<% item ||= {}
2+
errors ||= []
3+
url_errors = errors.select { |e| e.attribute.to_s.end_with?(".url") }
4+
service_errors = errors.select { |e| e.attribute.to_s.end_with?(".social_media_service_id") } %>
5+
6+
<%= render "govuk_publishing_components/components/select", {
7+
id: path.push("social_media_service_id").form_control_id,
8+
label: "Service (required)",
9+
name: "#{path.form_control_name}[social_media_service_id]",
10+
heading_size: "s",
11+
options: [{ text: "", value: "" }] +
12+
social_media_services.map do |service|
13+
{
14+
text: service.name,
15+
value: service.id,
16+
selected: item["social_media_service_id"].to_s == service.id.to_s,
17+
}
18+
end,
19+
full_width: true,
20+
error_items: service_errors.map { |e| { text: e.message } },
21+
} %>
22+
23+
<%= render "govuk_publishing_components/components/input", {
24+
label: {
25+
text: "URL (required)",
26+
},
27+
name: "#{path.form_control_name}[url]",
28+
id: path.push("url").form_control_id,
29+
value: item["url"],
30+
heading_size: "s",
31+
error_items: url_errors.map { |e| { text: e.message } },
32+
} %>
33+
34+
<%= render "govuk_publishing_components/components/input", {
35+
label: {
36+
text: "Title (optional)",
37+
},
38+
hint: "If left blank, the service name will be used",
39+
name: "#{path.form_control_name}[title]",
40+
id: path.push("title").form_control_id,
41+
value: item["title"],
42+
heading_size: "s",
43+
} %>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<% required ||= false
2+
content ||= {}
3+
errors ||= []
4+
items = content.is_a?(Hash) ? content.values : Array(content)
5+
items = [{}] if items.empty?
6+
social_media_services = SocialMediaService.all %>
7+
8+
<%= render "govuk_publishing_components/components/fieldset", {
9+
legend_text: "#{schema['title']}#{required ? ' (required)' : ''}",
10+
heading_size: "l",
11+
} do %>
12+
<%= render "govuk_publishing_components/components/add_another", {
13+
fieldset_legend: "Account",
14+
add_button_text: "Add account",
15+
items: items.each_with_index.filter_map do |item, index|
16+
next if item["_destroy"] == "1"
17+
18+
item_path = path.push(index.to_s)
19+
item_errors = errors.select { |e| e.attribute.to_s.start_with?(item_path.validation_error_attribute) }
20+
21+
{
22+
fields: render(partial: "admin/configurable_content_blocks/social_media_account_fields", locals: {
23+
item: item,
24+
path: item_path,
25+
social_media_services: social_media_services,
26+
errors: item_errors,
27+
}),
28+
destroy_checkbox: render("govuk_publishing_components/components/checkboxes", {
29+
name: "#{item_path.form_control_name}[_destroy]",
30+
items: [{ label: "Delete", value: "1" }],
31+
}),
32+
}
33+
end,
34+
empty: begin
35+
empty_index = items.length
36+
empty_path = path.push(empty_index.to_s)
37+
render(partial: "admin/configurable_content_blocks/social_media_account_fields", locals: {
38+
item: {},
39+
path: empty_path,
40+
social_media_services: social_media_services,
41+
errors: [],
42+
})
43+
end,
44+
} %>
45+
<% end %>

config/routes.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })
303303
get :change_type
304304
get :change_type_preview
305305
patch :apply_change_type
306-
get :social_media_accounts
307306
end
308307

309308
resources :translations, controller: "standard_edition_translations", except: %i[index show create] do

0 commit comments

Comments
 (0)