Skip to content

Commit

Permalink
Adding BetaLabelInserter
Browse files Browse the repository at this point in the history
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 #64 and
alphagov/static#381.
  • Loading branch information
tommyp committed Mar 12, 2014
1 parent d14f431 commit 9dbaeba
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/slimmer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion lib/slimmer/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -17,12 +18,13 @@ module Headers
result_count: "Result-Count",
section: "Section",
skip: "Skip",
template: "Template",
template: "Template"
}

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]}"
Expand Down
36 changes: 36 additions & 0 deletions lib/slimmer/processors/beta_label_inserter.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/slimmer/processors/beta_notice_inserter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -32,4 +33,3 @@ def beta_notice_block
end
end
end

1 change: 1 addition & 0 deletions lib/slimmer/test_templates/beta_label.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="beta-label"><p>This page is BETA.</p></div>
42 changes: 42 additions & 0 deletions test/processors/beta_label_inserter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative "../test_helper"

class BetaLabelInserterTest < MiniTest::Unit::TestCase

def setup
super
beta_label_block = '<div class="beta-label"><p>This page is BETA.</p></div>'
@skin = stub("Skin", :template => beta_label_block)
@template = as_nokogiri %{
<html><body><div><div id="wrapper"><header id="main">GOV.UK</header></div></div></body></html>
}
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

0 comments on commit 9dbaeba

Please sign in to comment.