From c7068740ec3c6640cc7dfcf1059585cd49dcaafa Mon Sep 17 00:00:00 2001 From: Tommy Palmer Date: Wed, 12 Mar 2014 17:30:24 +0000 Subject: [PATCH] Adding BetaLabelInserter A new Inserter Class for adding the Beta Label style. This will eventually replace the Beta Notice as the de-facto way to show Users that a page or service is in Beta. To use this, send a selector with a position like so: [Slimmer::Headers::BETA_LABEL] = "before:main.content" We currently only accept before and after as the 2 options for where it can be inserted. This follows on from https://github.com/alphagov/slimmer/pull/64 and https://github.com/alphagov/static/pull/381. --- lib/slimmer.rb | 1 + lib/slimmer/headers.rb | 2 + lib/slimmer/processors/beta_label_inserter.rb | 36 ++++++++++++++++ .../processors/beta_notice_inserter.rb | 2 +- lib/slimmer/test_templates/beta_label.html | 1 + test/processors/beta_label_inserter_test.rb | 42 +++++++++++++++++++ 6 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 lib/slimmer/processors/beta_label_inserter.rb create mode 100644 lib/slimmer/test_templates/beta_label.html create mode 100644 test/processors/beta_label_inserter_test.rb diff --git a/lib/slimmer.rb b/lib/slimmer.rb index c66dcdd..6d06f65 100644 --- a/lib/slimmer.rb +++ b/lib/slimmer.rb @@ -24,6 +24,7 @@ module Slimmer module Processors autoload :BetaNoticeInserter, 'slimmer/processors/beta_notice_inserter' + autoload :BetaLabelInserter, 'slimmer/processors/beta_label_inserter' autoload :BodyClassCopier, 'slimmer/processors/body_class_copier' autoload :BodyInserter, 'slimmer/processors/body_inserter' autoload :ConditionalCommentMover, 'slimmer/processors/conditional_comment_mover' diff --git a/lib/slimmer/headers.rb b/lib/slimmer/headers.rb index 7dba856..dc5ef13 100644 --- a/lib/slimmer/headers.rb +++ b/lib/slimmer/headers.rb @@ -7,6 +7,7 @@ module Headers SLIMMER_HEADER_MAPPING = { application_name: "Application-Name", beta: "Beta", + beta_label: "BetaLabel", format: "Format", need_id: "Need-ID", page_owner: "Page-Owner", @@ -23,6 +24,7 @@ module Headers APPLICATION_NAME_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:application_name]}" ARTEFACT_HEADER = "#{HEADER_PREFIX}-Artefact" BETA_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:beta]}" + BETA_LABEL = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:beta_label]}" FORMAT_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:format]}" ORGANISATIONS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:organisations]}" WORLD_LOCATIONS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:world_locations]}" diff --git a/lib/slimmer/processors/beta_label_inserter.rb b/lib/slimmer/processors/beta_label_inserter.rb new file mode 100644 index 0000000..4d665b0 --- /dev/null +++ b/lib/slimmer/processors/beta_label_inserter.rb @@ -0,0 +1,36 @@ +module Slimmer::Processors + class BetaLabelInserter + def initialize(skin, headers) + @skin = skin + @headers = headers + end + + def filter(page_template) + if should_add_beta_label? + if position == 'before' + page_template.at_css(selector).add_previous_sibling(beta_label_block) + elsif position == 'after' + page_template.at_css(selector).add_next_sibling(beta_label_block) + end + end + + end + + def should_add_beta_label? + !! @headers[Slimmer::Headers::BETA_LABEL] + end + + def beta_label_block + @beta_label_block ||= @skin.template('beta_label').to_s + end + + private + def selector + @headers[Slimmer::Headers::BETA_LABEL].gsub(/.*:/, '') + end + + def position + @headers[Slimmer::Headers::BETA_LABEL].gsub(/:.*/, '') + end + end +end diff --git a/lib/slimmer/processors/beta_notice_inserter.rb b/lib/slimmer/processors/beta_notice_inserter.rb index cc9a858..141a4a9 100644 --- a/lib/slimmer/processors/beta_notice_inserter.rb +++ b/lib/slimmer/processors/beta_notice_inserter.rb @@ -7,6 +7,7 @@ def initialize(skin, headers) def filter(content_document, page_template) if should_add_beta_notice? + warn "[DEPRECATION WARNING] BETA_HEADER is deprecated. Use BETA_LABEL instead." page_template.css('body').add_class('beta') if header = page_template.at_css('#global-header') header.add_next_sibling(beta_notice_block) @@ -32,4 +33,3 @@ def beta_notice_block end end end - diff --git a/lib/slimmer/test_templates/beta_label.html b/lib/slimmer/test_templates/beta_label.html new file mode 100644 index 0000000..4138c0b --- /dev/null +++ b/lib/slimmer/test_templates/beta_label.html @@ -0,0 +1 @@ +

This page is BETA.

diff --git a/test/processors/beta_label_inserter_test.rb b/test/processors/beta_label_inserter_test.rb new file mode 100644 index 0000000..e76764d --- /dev/null +++ b/test/processors/beta_label_inserter_test.rb @@ -0,0 +1,42 @@ +require_relative "../test_helper" + +class BetaLabelInserterTest < MiniTest::Unit::TestCase + + def setup + super + beta_label_block = '

This page is BETA.

' + @skin = stub("Skin", :template => beta_label_block) + @template = as_nokogiri %{ +
GOV.UK
+ } + end + + def test_should_add_beta_label_after + + headers = { + Slimmer::Headers::BETA_LABEL => "after:header#main" + } + + Slimmer::Processors::BetaLabelInserter.new(@skin, headers).filter(@template) + + assert_in @template, '#main + .beta-label' + end + + def test_should_add_beta_label_before + headers = { + Slimmer::Headers::BETA_LABEL => "before:header#main" + } + + Slimmer::Processors::BetaLabelInserter.new(@skin, headers).filter(@template) + + assert_in @template, '.beta-label + #main' + end + + def test_should_not_add_beta_label + headers = {} + + Slimmer::Processors::BetaLabelInserter.new(@skin, headers).filter(@template) + + assert_not_in @template, '.beta-label' + end +end