-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="beta-label"><p>This page is BETA.</p></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |