Skip to content

Commit 8693289

Browse files
committed
Adding cms site wide banner single into site header
New single cms resource New cms view component
1 parent ff252ad commit 8693289

File tree

13 files changed

+95
-4
lines changed

13 files changed

+95
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class Cms::SiteWideBannerComponent < ViewComponent::Base
4+
def initialize(text_content:)
5+
@text_content = text_content
6+
end
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="cms-site-wide-banner-component">
2+
<span class="govuk-body-s"><%= @text_content %></span>
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.cms-site-wide-banner-component {
2+
text-align: center;
3+
background-color: $black;
4+
padding: 10px;
5+
6+
span {
7+
color: $white;
8+
}
9+
}

app/controllers/application_controller.rb

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class ApplicationController < ActionController::Base
55

66
before_action :authenticate
77
before_action :access_cms_header
8+
before_action :access_cms_site_wide_notification
89

910
def authenticate
1011
return unless ENV["BASIC_AUTH_PASSWORD"]
@@ -20,6 +21,12 @@ def access_cms_header
2021
@cms_header = nil
2122
end
2223

24+
def access_cms_site_wide_notification
25+
@cms_site_wide_notification = Cms::Singles::SiteWideBanner.get
26+
rescue ActiveRecord::RecordNotFound
27+
@cms_site_wide_notification = nil
28+
end
29+
2330
def authenticate_user!
2431
redirect_to(helpers.create_account_url) unless current_user
2532
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Cms
2+
module Models
3+
module Meta
4+
class SiteWideBanner
5+
attr_reader :text_content
6+
7+
def initialize(text_content:)
8+
@text_content = text_content
9+
end
10+
11+
def render
12+
Cms::SiteWideBannerComponent.new(text_content:)
13+
end
14+
end
15+
end
16+
end
17+
end

app/services/cms/providers/strapi/factories/model_factory.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module ModelFactory
2323
Models::TextBlock => :to_text_block,
2424
Models::TextBlockWithoutWrapper => :to_text_block_without_wrapper,
2525
Models::TextField => :to_text_field,
26-
Models::WebPagePreview => :to_web_page_preview
26+
Models::WebPagePreview => :to_web_page_preview,
27+
Models::Meta::SiteWideBanner => :to_site_wide_banner
2728
}
2829

2930
def self.process_model(mapping, all_data)
@@ -171,6 +172,10 @@ def self.to_simple_title(strapi_data, _all_data)
171172
{title: strapi_data}
172173
end
173174

175+
def self.to_site_wide_banner(strapi_data, _all_data)
176+
{text_content: strapi_data}
177+
end
178+
174179
def self.to_slug(strapi_data, _all_data)
175180
{slug: strapi_data[:slug]}
176181
end

app/services/cms/providers/strapi/graphql_client.rb

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def one(resource_class, resource_id = nil, preview: false, preview_key: nil)
3434
data = clean_aliases(response.original_hash)
3535

3636
results = data[:data][resource_class.graphql_key.to_sym][:data]
37+
38+
return nil if resource_class.graphql_key.to_sym == :siteWideBanner && results.nil?
39+
3740
raise ActiveRecord::RecordNotFound if results.empty?
3841

3942
record = if resource_class.is_collection

app/services/cms/providers/strapi/queries/base_query.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class BaseQuery
2323
Models::TextBlock => SimpleField,
2424
Models::TextBlockWithoutWrapper => SimpleField,
2525
Models::TextField => SimpleField,
26-
Models::WebPagePreview => WebPagePreview
26+
Models::WebPagePreview => WebPagePreview,
27+
Models::Meta::SiteWideBanner => SiteWideBanner
2728
}.freeze
2829

2930
def initialize(collection_class, resource_filter = "slug")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Cms
2+
module Providers
3+
module Strapi
4+
module Queries
5+
class SiteWideBanner
6+
def self.embed(name)
7+
<<~GRAPHQL.freeze
8+
textContent
9+
GRAPHQL
10+
end
11+
end
12+
end
13+
end
14+
end
15+
end

app/services/cms/resource.rb

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def self.get(resource_id = nil, preview: false, preview_key: nil)
6868
client.one(self, resource_id)
6969
end
7070
end
71+
72+
return nil if data.nil? && resource_key.to_sym == :siteWideBanner
73+
7174
new(**data)
7275
end
7376

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Cms
2+
module Singles
3+
class SiteWideBanner < Resource
4+
def self.resource_attribute_mappings
5+
[
6+
{model: Models::Meta::SiteWideBanner, key: :textContent}
7+
]
8+
end
9+
10+
def self.cache_expiry
11+
15.minutes
12+
end
13+
14+
def self.graphql_key = "siteWideBanner"
15+
16+
def self.resource_key = "siteWideBanner"
17+
end
18+
end
19+
end

app/views/components/_header.html.erb

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@
5858
<%= render model.render %>
5959
<% end %>
6060
<% end %>
61-
6261
</div>
6362
</nav>
6463
</div>
64+
<% if @cms_site_wide_notification %>
65+
<%= render @cms_site_wide_notification.data_models.first.render %>
66+
<% end %>
6567
</header>

spec/support/cms/providers/strapi/schema.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)