Skip to content

Commit

Permalink
Merge pull request #274 from alphagov/travel-advice-print
Browse files Browse the repository at this point in the history
Add print view for travel advice
  • Loading branch information
NickColley authored Mar 8, 2017
2 parents ac967ea + 35a7d3e commit dac8b1a
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 27 deletions.
2 changes: 2 additions & 0 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def content_item_template
end

def render_template
request.variant = :print if params[:variant] == "print"

with_locale do
render content_item_template
end
Expand Down
8 changes: 4 additions & 4 deletions app/presenters/travel_advice_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def related_items
}
end

def parts
content_item["details"]["parts"] || []
end

def parts_navigation
part_links.each_slice(part_navigation_group_size).to_a
end
Expand Down Expand Up @@ -145,10 +149,6 @@ def current_part
end
end

def parts
content_item["details"]["parts"] || []
end

def part_links
summary_link_title = 'Current travel advice'
summary_part_link = is_summary? ? summary_link_title : link_to(summary_link_title, @base_path)
Expand Down
34 changes: 34 additions & 0 deletions app/views/content_items/travel_advice.html+print.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<%
content_for :title, "Print #{@content_item.page_title}"
content_for :simple_header, true
content_for :extra_head_content do %>
<meta name="robots" content="noindex, nofollow">
<script>window.onload = function() { window.print(); }</script>
<% end %>

<div class="grid-row travel-advice-print">
<div class="column-two-thirds">
<%= render 'govuk_component/title', @content_item.title_and_context %>

<section>
<h1 class="part-title">
<%= @content_item.current_part_title %>
</h1>
<%= render 'shared/travel_advice_summary', content_item: @content_item %>
<%= render 'govuk_component/govspeak',
content: @content_item.current_part_body,
direction: page_text_direction %>
</section>

<% @content_item.parts.each do |part| %>
<section>
<h1 class="part-title">
<%= part['title'] %>
</h1>
<%= render 'govuk_component/govspeak',
content: part['body'],
direction: page_text_direction %>
</section>
<% end %>
</div>
</div>
25 changes: 2 additions & 23 deletions app/views/content_items/travel_advice.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,15 @@
</h1>

<% if @content_item.is_summary? %>
<% if @content_item.alert_status.present? %>
<% alert_body = capture do %>
<div class="help-notice">
<%= @content_item.alert_status %>
</div>
<% end %>
<%= render 'govuk_component/govspeak',
content: alert_body,
direction: page_text_direction %>
<% end %>

<%= render 'govuk_component/metadata', @content_item.metadata %>
<% if @content_item.map %>
<figure class="map">
<img src="<%= @content_item.map["url"] %>" alt="<%= @content_item.map["alt_text"] %>">
<% if @content_item.map_download_url %>
<figcaption class="map-download">
<a href="<%= @content_item.map_download_url %>">Download map (PDF)</a>
</figcaption>
<% end %>
</figure>
<% end %>
<%= render 'shared/travel_advice_summary', content_item: @content_item %>
<% end %>

<%= render 'govuk_component/govspeak',
content: @content_item.current_part_body,
direction: page_text_direction %>

<div class="print-link">
<%= link_to "Print entire guide", @content_item.print_link %>
<%= link_to "Print entire guide", @content_item.print_link, rel: 'alternate' %>
</div>
</div>
<div class="column-one-third add-title-margin">
Expand Down
22 changes: 22 additions & 0 deletions app/views/shared/_travel_advice_summary.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<% if content_item.alert_status.present? %>
<% alert_body = capture do %>
<div class="help-notice">
<%= content_item.alert_status %>
</div>
<% end %>
<%= render 'govuk_component/govspeak',
content: alert_body,
direction: page_text_direction %>
<% end %>

<%= render 'govuk_component/metadata', content_item.metadata %>
<% if content_item.map %>
<figure class="map">
<img src="<%= content_item.map["url"] %>" alt="<%= content_item.map["alt_text"] %>">
<% if content_item.map_download_url %>
<figcaption class="map-download">
<a href="<%= content_item.map_download_url %>">Download map (PDF)</a>
</figcaption>
<% end %>
</figure>
<% end %>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
mount JasmineRails::Engine => "/specs" if defined?(JasmineRails)

get 'healthcheck', to: proc { [200, {}, ['']] }
get '*path/:variant' => 'content_items#show',
constraints: {
variant: /print/
}

# FIXME: Update when https://trello.com/c/w8HN3M4A/ is ready
get 'foreign-travel-advice/:country/:part' => 'travel_advice#show'
Expand Down
19 changes: 19 additions & 0 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ class ContentItemsControllerTest < ActionController::TestCase
)
end

test 'routing handles paths with print variant' do
assert_routing(
'/government/news/statement-the-status-of-eu-nationals-in-the-uk/print',
controller: 'content_items',
action: 'show',
path: 'government/news/statement-the-status-of-eu-nationals-in-the-uk',
variant: 'print'
)
end

test "gets item from content store" do
content_item = content_store_has_schema_example('case_study', 'case_study')

Expand Down Expand Up @@ -91,6 +101,15 @@ class ContentItemsControllerTest < ActionController::TestCase
assert_select "feed title", 'Travel Advice Summary'
end

test "renders print variants" do
content_item = content_store_has_schema_example('travel_advice', 'full-country')
get :show, params: { path: path_for(content_item), variant: 'print' }

assert_response :success
assert_equal request.variant, [:print]
assert_select ".travel-advice-print"
end

test "gets item from content store even when url contains multi-byte UTF8 character" do
content_item = content_store_has_schema_example('case_study', 'case_study')
utf8_path = "government/case-studies/caf\u00e9-culture"
Expand Down
53 changes: 53 additions & 0 deletions test/integration/travel_advice_print_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'test_helper'

class TravelAdvicePrint < ActionDispatch::IntegrationTest
test "it renders the print view" do
setup_and_visit_travel_advice_print('full-country')
assert page.has_css?(".travel-advice-print")
end

test "it is not indexable by search engines" do
setup_and_visit_travel_advice_print('full-country')
assert page.has_css?("meta[name='robots'][content='noindex, nofollow']", visible: false)
end

test "it renders the summary and all parts in the print view" do
setup_and_visit_travel_advice_print('full-country')
parts = @content_item['details']['parts']

assert_has_component_metadata_pair("Still current at", Date.today.strftime("%-d %B %Y"))
assert_has_component_metadata_pair("Updated", Date.parse(@content_item["details"]["reviewed_at"]).strftime("%-d %B %Y"))

within shared_component_selector("metadata") do
component_args = JSON.parse(page.text)
latest_update = component_args["other"].fetch("Latest update")

assert latest_update.include?('<p>')
assert latest_update.include?(@content_item['details']['change_description'].gsub('Latest update: ', ''))
end

assert page.has_css?("h1", text: 'Current travel advice')
parts.each do |part|
assert page.has_css?("h1", text: part['title'])
end

page.all(shared_component_selector("govspeak")).each_with_index do |govspeak_component, i|
within(govspeak_component) do
content_passed_to_component = JSON.parse(page.text).fetch("content").gsub(/\s+/, ' ')
if i == 0
assert_equal @content_item["details"]["summary"].gsub(/\s+/, ' '), content_passed_to_component
else
assert_equal parts[i - 1]['body'].gsub(/\s+/, ' '), content_passed_to_component
end
end
end
end

def setup_and_visit_travel_advice_print(name)
example = get_content_example_by_format_and_name('travel_advice', name)
@content_item = JSON.parse(example).tap do |item|
content_store_has_item(item["base_path"], item.to_json)
visit "#{item['base_path']}/print"
end
end
end
7 changes: 7 additions & 0 deletions test/presenters/travel_advice_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ def format_name
assert_equal "<p>Test&lt;br&gt;XML</p>", present_example(example).atom_change_description
end

test "presents all parts for the print view" do
example_parts = schema_item("full-country")["details"]["parts"]
presented = presented_item("full-country")

assert_equal example_parts, presented.parts
end

private

def present_latest(latest)
Expand Down

0 comments on commit dac8b1a

Please sign in to comment.