From e2b1c3a9c3f1c5c3b353c42318a3d6f8842afebb Mon Sep 17 00:00:00 2001 From: Paul Hayes Date: Mon, 9 Nov 2015 15:51:03 +0000 Subject: [PATCH 1/3] Move logic out of template render Split component into two, a top part that deals with the logic and a second part that renders HTML. --- .../govuk_component/analytics_meta_tags.raw.html.erb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/views/govuk_component/analytics_meta_tags.raw.html.erb b/app/views/govuk_component/analytics_meta_tags.raw.html.erb index 717ec0912..0e8de7c90 100644 --- a/app/views/govuk_component/analytics_meta_tags.raw.html.erb +++ b/app/views/govuk_component/analytics_meta_tags.raw.html.erb @@ -3,18 +3,20 @@ # parsed JSON document which will have string keys by default, but our # components use symbol keys and we want consistency. links_hash = content_item.to_h.deep_symbolize_keys[:links] || {} + meta_tags = {} organisations = [] organisations += links_hash[:organisations] || [] organisations += links_hash[:lead_organisations] || [] organisations += links_hash[:supporting_organisations] || [] organisations += links_hash[:worldwide_organisations] || [] + organisations_content = organisations.map{ |link| "<#{link[:analytics_identifier]}>" }.join + meta_tags["govuk:analytics:organisations"] = organisations_content if organisations_content world_locations = links_hash[:world_locations] || [] + world_locations_content = world_locations.map{ |link| "<#{link[:analytics_identifier]}>" }.join + meta_tags["govuk:analytics:world-locations"] = world_locations_content if world_locations_content %> -<% if organisations.any? %> - -<% end %> -<% if world_locations.any? %> - +<% meta_tags.each do |name, content| %> + <% end %> From e2f314ea323c2d5e286f0dc4b625add5b9c66614 Mon Sep 17 00:00:00 2001 From: Paul Hayes Date: Mon, 9 Nov 2015 16:28:12 +0000 Subject: [PATCH 2/3] Add format and need-ids meta tags Allow more types of metadata to be tracked in the new world. --- .../analytics_meta_tags.raw.html.erb | 8 ++++++- .../analytics_meta_tags_test.rb | 21 ++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/views/govuk_component/analytics_meta_tags.raw.html.erb b/app/views/govuk_component/analytics_meta_tags.raw.html.erb index 0e8de7c90..9744227b7 100644 --- a/app/views/govuk_component/analytics_meta_tags.raw.html.erb +++ b/app/views/govuk_component/analytics_meta_tags.raw.html.erb @@ -2,9 +2,15 @@ # We have to call deep_symbolize_keys because we're often dealing with a # parsed JSON document which will have string keys by default, but our # components use symbol keys and we want consistency. - links_hash = content_item.to_h.deep_symbolize_keys[:links] || {} + content_item_hash = content_item.to_h.deep_symbolize_keys + links_hash = content_item_hash[:links] || {} meta_tags = {} + meta_tags["govuk:format"] = content_item_hash[:format] if content_item_hash[:format] + + need_ids = content_item_hash[:need_ids] || [] + meta_tags["govuk:need-ids"] = need_ids.join(',') if need_ids.any? + organisations = [] organisations += links_hash[:organisations] || [] organisations += links_hash[:lead_organisations] || [] diff --git a/test/govuk_component/analytics_meta_tags_test.rb b/test/govuk_component/analytics_meta_tags_test.rb index d39efe660..62139f2b1 100644 --- a/test/govuk_component/analytics_meta_tags_test.rb +++ b/test/govuk_component/analytics_meta_tags_test.rb @@ -6,6 +6,16 @@ def component_name "analytics_meta_tags" end + test "renders format in a meta tag" do + render_component(content_item: { format: "case_study" }) + assert_meta_tag('govuk:format', 'case_study') + end + + test "renders need IDs as a comma separated list" do + render_component(content_item: { need_ids: [100001, 100002] }) + assert_meta_tag('govuk:need-ids', '100001,100002') + end + test "renders organisations in a meta tag with angle brackets" do content_item = { links: { @@ -17,8 +27,7 @@ def component_name } render_component(content_item: content_item) - - assert_select "meta[name='govuk:analytics:organisations'][content='']" + assert_meta_tag('govuk:analytics:organisations', '') end test "renders world locations in a meta tag with angle brackets" do @@ -36,8 +45,7 @@ def component_name } render_component(content_item: content_item) - - assert_select "meta[name='govuk:analytics:world-locations'][content='']" + assert_meta_tag('govuk:analytics:world-locations', '') end test "handling of string keys and/or GdsApi::Response objects" do @@ -54,7 +62,10 @@ def component_name api_response = GdsApi::Response.new(net_http_response) render_component(content_item: api_response) + assert_meta_tag('govuk:analytics:world-locations', '') + end - assert_select "meta[name='govuk:analytics:world-locations'][content='']" + def assert_meta_tag(name, content) + assert_select "meta[name='#{name}'][content='#{content}']" end end From 1a1944333f5572833bd95716d731fd46aad6768d Mon Sep 17 00:00:00 2001 From: Paul Hayes Date: Mon, 9 Nov 2015 17:36:42 +0000 Subject: [PATCH 3/3] Don't render any meta tags when no trackable data --- .../govuk_component/analytics_meta_tags.raw.html.erb | 8 ++++---- test/govuk_component/analytics_meta_tags_test.rb | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/views/govuk_component/analytics_meta_tags.raw.html.erb b/app/views/govuk_component/analytics_meta_tags.raw.html.erb index 9744227b7..f52340c83 100644 --- a/app/views/govuk_component/analytics_meta_tags.raw.html.erb +++ b/app/views/govuk_component/analytics_meta_tags.raw.html.erb @@ -16,12 +16,12 @@ organisations += links_hash[:lead_organisations] || [] organisations += links_hash[:supporting_organisations] || [] organisations += links_hash[:worldwide_organisations] || [] - organisations_content = organisations.map{ |link| "<#{link[:analytics_identifier]}>" }.join - meta_tags["govuk:analytics:organisations"] = organisations_content if organisations_content + organisations_content = organisations.map { |link| "<#{link[:analytics_identifier]}>" }.join + meta_tags["govuk:analytics:organisations"] = organisations_content if organisations.any? world_locations = links_hash[:world_locations] || [] - world_locations_content = world_locations.map{ |link| "<#{link[:analytics_identifier]}>" }.join - meta_tags["govuk:analytics:world-locations"] = world_locations_content if world_locations_content + world_locations_content = world_locations.map { |link| "<#{link[:analytics_identifier]}>" }.join + meta_tags["govuk:analytics:world-locations"] = world_locations_content if world_locations.any? %> <% meta_tags.each do |name, content| %> diff --git a/test/govuk_component/analytics_meta_tags_test.rb b/test/govuk_component/analytics_meta_tags_test.rb index 62139f2b1..7baf4d5b5 100644 --- a/test/govuk_component/analytics_meta_tags_test.rb +++ b/test/govuk_component/analytics_meta_tags_test.rb @@ -6,6 +6,10 @@ def component_name "analytics_meta_tags" end + test "no meta tags are rendered when there's no trackable data" do + assert_empty render_component(content_item: {}) + end + test "renders format in a meta tag" do render_component(content_item: { format: "case_study" }) assert_meta_tag('govuk:format', 'case_study')