Skip to content

Commit de24e34

Browse files
committed
fixed
1 parent 2c4eb79 commit de24e34

5 files changed

Lines changed: 43 additions & 34 deletions

File tree

app/models/concerns/standard_edition/has_block_content.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ module StandardEdition::HasBlockContent
77
def block_content=(value)
88
value_to_merge = (value || {}).to_h.deep_stringify_keys
99
merged_value = (self[:block_content] || {}).deep_merge(value_to_merge)
10-
super(merged_value)
10+
11+
normalised = StandardEdition::BlockContent.new(type_instance.schema)
12+
normalised.assign_attributes(merged_value)
13+
14+
@block_content = normalised # keep the memoized object in sync (optional but nice)
15+
super(normalised.to_h)
1116
end
1217

1318
def block_content

app/models/configurable_content_blocks/factory.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def build_block(block)
99
"default_string" => ->(_page) { ConfigurableContentBlocks::DefaultString.new },
1010
"govspeak" => ->(page) { ConfigurableContentBlocks::Govspeak.new(page.images, page.attachments) },
1111
"default_date" => ->(_page) { ConfigurableContentBlocks::DefaultDate.new },
12-
"social_media_service_select" => ->(page) { ConfigurableContentBlocks::SocialMediaServiceSelect.new },
12+
"social_media_service_select" => ->(_page) { ConfigurableContentBlocks::SocialMediaServiceSelect.new },
1313
"image_select" => ->(page) { ConfigurableContentBlocks::ImageSelect.new(page.valid_images) },
1414
"lead_image_select" => ->(page) { ConfigurableContentBlocks::LeadImageSelect.new(page.valid_lead_images, default_lead_image: page.default_lead_image, placeholder_image_url: page.placeholder_image_url) },
1515
"default_object" => ->(_page) { ConfigurableContentBlocks::DefaultObject.new(self) },

app/models/standard_edition/block_content.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def attributes=(values)
3232
# { "0" => { "foo" => "bar" }, "1" => { "delete_me" => "something", "_destroy" => "1" } }
3333
# to
3434
# [ { "foo" => "bar" } ]
35-
public_send(setter, values[key].values.reject { |h| h["_destroy"] == "1" })
35+
vals = values[key].is_a?(Hash) ? values[key].values : values[key]
36+
public_send(setter, vals.reject { |h| h["_destroy"] == "1" })
3637
else
3738
public_send(setter, values[key])
3839
end

app/presenters/publishing_api/payload_builder/block_content.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def social_media_links(attribute)
9090
service = SocialMediaService.find(item["social_media_service_id"].to_i)
9191
{
9292
title: service.name,
93-
service_type: service.name.parameterize.underscore, # "Google Plus" => "google_plus"
93+
service_type: service.name.parameterize, # "Google Plus" => "google-plus"
9494
href: item["url"],
9595
}
9696
end

app/views/admin/configurable_content_blocks/_default_array.html.erb

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,52 @@
66
required_attributes ||= []
77
element_properties = schema.fetch("fields") %>
88

9+
<%
10+
render_fields = ->(social_media_link, index) do
11+
element_fields = capture do
12+
element_properties.each do |property_key, property_schema|
13+
block = default_array.block_factory.build_block(property_schema["block"])
14+
%>
15+
<%= render block, {
16+
schema: property_schema,
17+
content: social_media_link[property_key],
18+
# TODO: support translations
19+
# translated_content: social_media_link[property_key],
20+
path: ConfigurableContentBlocks::Path.new(["social_media_links", index, property_key]),
21+
required: required_attributes.include?(property_key),
22+
required_attributes: property_schema["block"] == "default_array" ? required_attributes : [],
23+
right_to_left: right_to_left,
24+
errors: errors,
25+
} %>
26+
<%
27+
end # element_properties.each loop
28+
end # element_fields = capture
29+
end # render_fields definition
30+
%>
31+
932
<%= render "govuk_publishing_components/components/fieldset",
1033
{ legend_text: "#{schema['title']}#{required ? ' (required)' : ''}",
1134
heading_size: "l",
1235
id: path.form_control_id } do %>
1336
<%
1437
# TODO: make the array key (`social_media_links`) dynamic
15-
items = content["social_media_links"].map do |social_media_link|
16-
%>
17-
<% element_fields = capture do %>
18-
<% element_properties.each do |property_key, property_schema| %>
19-
<% #raise [property_key, content.to_h, schema.inspect].inspect %>
20-
<% block = default_array.block_factory.build_block(property_schema["block"]) %>
21-
<%= render block, {
22-
schema: property_schema,
23-
content: social_media_link[property_key],
24-
# translated_content: social_media_link[property_key],
25-
path: ConfigurableContentBlocks::Path.new([property_key]),
26-
required: required_attributes.include?(property_key),
27-
required_attributes: property_schema["block"] == "default_array" ? required_attributes : [],
28-
right_to_left: right_to_left,
29-
errors: errors,
30-
} %>
31-
<% end %>
32-
<% end %>
33-
34-
<%
38+
items = content["social_media_links"].map.with_index do |social_media_link, index|
39+
# raise path.form_control_name.inspect # "edition[block_content][social_media_links]"
3540
{
36-
fields: element_fields,
37-
# TODO: the noJS experience:
38-
# destroy_checkbox: render("govuk_publishing_components/components/checkboxes", {
39-
# name: "#{item_path.form_control_name}[_destroy]",
40-
# items: [{ label: "Remove", value: "1" }],
41-
# }),
41+
fields: render_fields.call(social_media_link, index),
42+
destroy_checkbox: render("govuk_publishing_components/components/checkboxes", {
43+
name: "#{path.form_control_name}[#{index}][_destroy]",
44+
items: [{ label: "Remove", value: "1" }],
45+
}),
4246
}
43-
%>
44-
<% end %>
47+
end
48+
%>
4549

4650
<%= render "govuk_publishing_components/components/add_another", {
4751
fieldset_legend: schema["title"],
4852
add_button_text: "Add another",
4953
empty_fields: true,
5054
items: items,
51-
# TODO: the noJS experience:
52-
empty: "TBC", # render_fields.call({}, path.push(items.length.to_s), []),
55+
empty: render_fields.call({}, items.length)
5356
} %>
5457
<% end %>

0 commit comments

Comments
 (0)