Skip to content

Commit

Permalink
Upload script and additional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrett committed Feb 20, 2024
1 parent 71bd2d2 commit 69465ce
Show file tree
Hide file tree
Showing 28 changed files with 300 additions and 114 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gem 'audited', '~> 5.4'
gem 'bcrypt', '~> 3.1.16'
gem 'bootsnap', '>= 1.17.1', require: false
gem 'canonical-rails', '~> 0.2'
gem "contentful_rails", "~> 0.5.0", git: 'https://github.com/listingsproject/contentful_rails.git'
gem "contentful_rails", "~> 0.5.0"
gem 'cssbundling-rails'
gem 'devise', '>= 4.9.3'
gem 'devise-security', '~> 0.18.0'
Expand Down
15 changes: 5 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
GIT
remote: https://github.com/listingsproject/contentful_rails.git
revision: a050d953cba74bda571677691f88237afdcf764c
specs:
contentful_rails (0.5.0)
contentful_model (~> 1.0)
rails (>= 4.2)
redcarpet (~> 3.2)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -140,6 +131,10 @@ GEM
contentful (~> 2.7)
contentful-management (~> 2.0)
redcarpet
contentful_rails (0.5.0)
contentful_model (~> 1.0)
rails (>= 4.2)
redcarpet (~> 3.2)
crass (1.0.6)
cssbundling-rails (1.3.3)
railties (>= 6.0.0)
Expand Down Expand Up @@ -798,7 +793,7 @@ DEPENDENCIES
byebug
canonical-rails (~> 0.2)
capybara (~> 3.36)
contentful_rails (~> 0.5.0)!
contentful_rails (~> 0.5.0)
cssbundling-rails
cucumber (~> 6.1)
devise (>= 4.9.3)
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ApplicationController < ActionController::Base

include Pundit::Authorization

helper_method :menu_item
helper_method :menu_item, :section

def check
render json: { status: "OK", version: release_version, sha: ENV["SHA"], environment: Rails.env }, status: :ok
Expand All @@ -30,6 +30,10 @@ def menu_item
:home
end

def section
''
end

def not_found
raise ActionController::RoutingError, "Not Found"
end
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
class HomeController < ApplicationController
before_action :authenticate_user!, if: proc { !ENV["AUTH_ON_EVERYTHING"].nil? }

helper_method :section

def index
@page = Web::Page.home
@pages = Web::Page.search(placement: 'home').load
end

private

def section
'home'
end
end
2 changes: 1 addition & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def menu_item
end

def page_params
params.permit(:cms_path)
params.permit(:cms_path, :slug)
end

def page(page_name)
Expand Down
49 changes: 16 additions & 33 deletions app/controllers/web/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,39 @@
module Web
class PagesController < ApplicationController
before_action { load_page }
before_action { page }

helper_method :breadcrumbs
helper_method :page, :slug, :section, :breadcrumbs

def index; end

def show
if @page.children?
if @page.cards?
render 'show_cards'
else
render 'show_navigation'
end
elsif @page.navigation?
render 'show_navigation'
end
render page.page_style
end

private

def menu_item
cms_path_array.first
def breadcrumbs
[]
end

def page_params
params.permit(:cms_path, :slug)
params.permit(:section, :overview, :slug)
end

def page(page_name)
content.send(page_name)
end

def breadcrumbs
if cms_path == "/"
{}
else
cms_path_array.each_with_object({ "Home" => "/" }) do |obj, memo|
page = load_page(lookup_page: obj.to_sym)
memo[page.menu_title] = page.full_path
end
end
def slug
page_params[:slug]
end

def cms_path
page_params[:cms_path] || "/"
def section
page_params[:section]
end

def cms_path_array
cms_path&.split("/")
def lookup_slug
slug.presence || section
end

def load_page
@page = Web::Page.by_slug(page_params[:slug])
def page
@page ||= Web::Page.by_slug(lookup_slug)
end
end
end
26 changes: 23 additions & 3 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def link_to_preview(article, link_to_args = {})
link_to text, admin_article_path(article), link_to_args
end

def navigation
def yml_navigation
render(HeaderComponent.new(service_name: t('service.name'), classes: 'dfe-header noprint', container_classes: %w[dfe-header-f-header-flex], navigation_label: 'Primary navigation')) do |header|
menu.each do |key, item|
yml_menu.each do |key, item|
header.with_navigation_item(
text: item[:menu_title],
href: ["/", item[:parent_path], item[:slug]].join("/").squeeze("/"),
Expand All @@ -36,7 +36,27 @@ def navigation
end
end

def yml_menu
@yml_menu ||= Rails.configuration.content
end

def navigation
render(HeaderComponent.new(service_name: t('service.name'), classes: 'dfe-header noprint', container_classes: %w[dfe-header-f-header-flex], navigation_label: 'Primary navigation')) do |header|
header.with_navigation_item(
text: 'Home', href: '/', active: request.path == root_path, classes: %w[dfe-header__navigation-item],
)
menu.each do |item|
header.with_navigation_item(
text: item.title,
href: ["/", item.parent&.slug, item.slug].join("/").squeeze("/"),
active: item.slug == section,
classes: %w[dfe-header__navigation-item],
)
end
end
end

def menu
@menu ||= Rails.configuration.content
@menu ||= Web::Page.search(placement: 'home').load
end
end
4 changes: 4 additions & 0 deletions app/models/content_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def helpful_tools
true
end

def meta_description
description
end

# Called when a page is created or a position attribute changes
class << self
def reorder
Expand Down
22 changes: 13 additions & 9 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def full_path
["/", parent_path, slug].join("/").squeeze("/")
end

def meta_description
'placeholder'
end

def cards
if card_attributes.present?
card_attributes.map { |attrs| Card.new(attrs) }
Expand All @@ -28,22 +32,22 @@ class Hero
class Card
include ActiveModel::Model

attr_accessor :slug, :image_id, :title, :body, :thumbnail, :parent_path, :subpages
attr_accessor :slug, :image_id, :title, :body, :parent_path, :subpages

def path
[parent_path, slug].join("/")
end

def asset
@asset = if image_id.blank?
default_asset
else
Asset.new(id: image_id)
end
def meta_description
'card description'
end

def default_asset
@default_asset ||= Asset.new(id: '706uv3PhtBRxqRffZKa2b3')
def thumbnail
OpenStruct.new(
url: 'https://placehold.co/380x254/347ca9/FFFFFF/png',
description: 'A placeholder picture',
title: 'Title for a placeholder picture',
)
end
end
end
25 changes: 16 additions & 9 deletions app/models/web/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Web
class Page < ContentfulModel::Base
extend ::Caching

self.content_type_id = 'page'
self.content_type_id = 'helpPage'

has_many_nested :pages, root: -> { Web::Page.home }

Expand All @@ -16,26 +16,33 @@ def self.by_slug(slug)
find_by(slug: slug.to_s).first
end

def path
["/", parent&.parent&.slug, parent&.slug, slug].join("/").squeeze("/")
end

def hero
OpenStruct.new(title: hero_title, body: hero_description)
end

def cards?
page_style == 'cards'
end

def navigation?
page_style == 'navigation'
page_style == 'side-nav'
end

# @return [Web::Resource]
def hero
return nil if fields[:page_abstract].blank?
def home?
placement.match(/home/)
end

fetch_or_store self.class.to_key(fields[:page_abstract].id) do
Web::Resource.find(fields[:page_abstract].id)
end
def footer?
placement.match(/footer/)
end

# @return [ContentfulModel::Asset]
def thumbnail
return '//external-image-resource-placeholder' if fields[:image].blank?
return OpenStruct.new(url: 'https://placehold.co/380x254/347ca9/FFFFFF/png') if fields[:image].blank?

fetch_or_store self.class.to_key(fields[:image].id) do
ContentfulModel::Asset.find(fields[:image].id)
Expand Down
3 changes: 2 additions & 1 deletion app/models/web/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Web
class Resource < ContentfulModel::Base
extend ::Caching

self.content_type_id = 'resource'
self.content_type_id = 'helpResource'

belongs_to_many :web_page

# @param name [String]
Expand Down
4 changes: 2 additions & 2 deletions app/views/home/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
h2 = t('home.title')
p.govuk-body = t('home.body')

.govuk-grid-row
= render partial: 'shared/card', collection: @page.pages
.govuk-grid-row class='govuk-!-margin-bottom-6'
= render partial: 'shared/card', collection: @pages

= render 'shared/ctas'
5 changes: 3 additions & 2 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ html.govuk-template lang='en'
= tag.meta content: 'width=device-width,initial-scale=1', name: 'viewport'
= tag.meta name: 'robots', content: 'noindex,nofollow' unless Rails.application.live?
= tag.meta property: 'og:image', content: image_path('images/govuk-opengraph-image.png')
= tag.meta name: 'description', content: @page.description if @page.description.present?
= tag.meta name: 'description', content: @page&.meta_description if @page&.meta_description.present?
= csrf_meta_tags
= tag.meta name: 'theme-color', content: '#0b0c0c'

Expand All @@ -34,7 +34,8 @@ html.govuk-template lang='en'
= render 'layouts/cookie_banner'

= govuk_skip_link
= navigation

= ENV['CONTENTFUL'].present? ? navigation : yml_navigation

.dfe-content-page--header class='govuk-!-padding-bottom-4'
.dfe-width-container
Expand Down
4 changes: 2 additions & 2 deletions app/views/pages/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
p.govuk-body =@page.body

.govuk-grid-row
= render partial: 'card', collection: @page.cards
= render partial: 'shared/card', collection: @page.cards

= render 'ctas'
= render 'shared/ctas'
6 changes: 3 additions & 3 deletions app/views/shared/_card.html.slim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.govuk-grid-column-one-third
.hf-card
a href=card.slug
a href=card.path
.hf-card-container
= image_tag(card.thumbnail.url, class: 'full-width-image', alt: card.thumbnail.description, title: card.thumbnail.title)
.hf-card-details
h3.govuk-heading-m=card&.hero&.title
p.govuk-body=card&.hero&.body
h3.govuk-heading-m=card&.title
p.govuk-body=card&.meta_description || card&.hero_description
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
- content_for(:hero) do
= render partial: 'hero', locals: { hero: @page.hero }

- if @page.intro.present?
= m(@page.intro)
- if @page.introduction.present?
= m(@page.introduction)

- if @page.content_list.present?
.govuk-grid-row
Expand Down
11 changes: 11 additions & 0 deletions app/views/web/pages/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- content_for(:page_title) do
= @page.title

- content_for(:hero) do
= render partial: 'hero', locals: { hero: @page.hero }

- if @page.children?
.govuk-grid-row class='govuk-!-margin-top-4 govuk-!-margin-bottom-5'
= render partial: 'shared/card', collection: @page.pages

= render 'shared/ctas'
Loading

0 comments on commit 69465ce

Please sign in to comment.