Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ STRAPI_IMAGE_URL="http://strapi.teachcomputing.rpfdev.com"
STRAPI_GRAPHQL_URL="http://strapi.teachcomputing.rpfdev.com/graphql"
STRAPI_CONNECTION_TYPE="graphql"

NODE_OPTIONS=--openssl-legacy-provider
NODE_OPTIONS=--openssl-legacy-provider
16 changes: 16 additions & 0 deletions app/components/cms/primary_glossary_table_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class Cms::PrimaryGlossaryTableComponent < ViewComponent::Base
def initialize(title:)
@title = title
@records = begin
Cms::Collections::PrimaryGlossaryTableItems.all_records
rescue ActiveRecord::RecordNotFound
[]
end
end

def render?
@records.any?
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= render GovGridRowComponent.new(additional_classes: "primary-glossary-table", padding: {top: 0, bottom: 0}) do |row| %>
<%= row.with_column("full") do %>
<% if @title %>
<h2 class="govuk-heading-m" id="glossary"><%= @title %></h2>
<% end %>
<table class="govuk-table govuk-!-margin-bottom-0">
<thead class="govuk-table__head">
<tr>
<th class="govuk-table__header">Term</th>
<th class="govuk-table__header">Key Stage</th>
<th class="govuk-table__header">Definition</th>
</tr>
</thead>
<tbody class="govuk-table__body">
<% @records.each do |record| %>
<tr class="govuk-table__row">
<td class="govuk-table__cell"><%= record.term.value %></td>
<td class="govuk-table__cell"><%= record.key_stage.value %></td>
<td class="govuk-table__cell"><%= render Cms::RichTextBlockComponent.new(blocks: record.definition.blocks) %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.primary-glossary-table {
.govuk-table {
border-collapse: collapse;

th, td {
border: 1px solid black;
padding: 10px;
}

.govuk-table__header {
color: $white;
background-color: $purple;
text-wrap: nowrap;
text-align: center;
}

.govuk-table__cell {
padding-right: 10px;
}
}
}
20 changes: 8 additions & 12 deletions app/jobs/searchable_page_indexing_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ class SearchablePageIndexingJob < ApplicationJob
def perform
now = DateTime.now
SearchablePages::CmsBlog.delete_all
page = 1
per_page = 100
loop do
blog_search_records = Cms::Collections::Blog.all(page, per_page) # Strapi Graphql has a max limit of 100
if blog_search_records.resources.any?
SearchablePages::CmsBlog.insert_all(blog_search_records.resources.map { |blog| blog.to_search_record(now) })
end
break if (page * per_page) > blog_search_records.total_records
page += 1

all_blogs = Cms::Collections::Blog.all_records

if all_blogs.any?
SearchablePages::CmsBlog.insert_all(all_blogs.map { |blog| blog.to_search_record(now) })
end

SearchablePages::CmsWebPage.delete_all
page_search_records = Cms::Collections::WebPage.all(1, 100)
if page_search_records.resources.any?
SearchablePages::CmsWebPage.insert_all(page_search_records.resources.map { |page| page.to_search_record(now) })
page_search_records = Cms::Collections::WebPage.all_records
if page_search_records.any?
SearchablePages::CmsWebPage.insert_all(page_search_records.map { |page| page.to_search_record(now) })
end
enrichment_pages = Cms::Collections::EnrichmentPage.all(1, 10)
if enrichment_pages.resources.any?
Expand Down
31 changes: 31 additions & 0 deletions app/services/cms/collections/primary_glossary_table_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Cms
module Collections
class PrimaryGlossaryTableItems < Resource
def self.is_collection = true

def self.resource_key
"primary-computing-glossary-table"
end

def self.graphql_key
"primaryComputingGlossaryTables"
end

def self.collection_attribute_mappings
[
{model: Cms::Models::TextField, key: :term},
{model: Cms::Models::TextField, key: :keyStage},
{model: Cms::Models::TextBlock, key: :definition}
]
end

def self.resource_attribute_mappings
[
{model: Cms::Models::TextField, key: :term},
{model: Cms::Models::TextField, key: :keyStage},
{model: Cms::Models::TextBlock, key: :definition}
]
end
end
end
end
15 changes: 15 additions & 0 deletions app/services/cms/dynamic_components/primary_glossary_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Cms
module DynamicComponents
class PrimaryGlossaryTable
attr_accessor :title

def initialize(title:)
@title = title
end

def render
PrimaryGlossaryTableComponent.new(title:)
end
end
end
end
2 changes: 2 additions & 0 deletions app/services/cms/providers/strapi/factories/blocks_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def self.generate_component(component_name, strapi_data)
to_icon_row(strapi_data)
when "two-column-video-section"
to_two_column_video_section(strapi_data)
when "primary-glossary-table"
DynamicComponents::PrimaryGlossaryTable.new(title: strapi_data[:title])
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Cms
module Providers
module Strapi
module Mocks
module DynamicComponents
class PrimaryGlossaryTable < StrapiMock
strapi_component "blocks.primary-glossary-table"

attribute(:title) { Faker::Lorem.sentence }
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Cms
module Providers
module Strapi
module Mocks
class PrimaryGlossaryTableItems < StrapiMock
attribute(:term) { Faker::Lorem.word }
attribute(:keyStage) { Faker::Lorem.word }
attribute(:definition) { RichBlocks.generate_data }
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Cms
module Providers
module Strapi
module Queries
module Components
module Blocks
class PrimaryGlossaryTable < BaseComponentQuery
def self.name = "ComponentBlocksPrimaryGlossaryTable"

def self.base_fields
<<~GRAPHQL.freeze
title
GRAPHQL
end
end
end
end
end
end
end
end
1 change: 1 addition & 0 deletions app/services/cms/providers/strapi/queries/dynamic_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DynamicZone
Components::Blocks::NumberedIconList,
Components::Blocks::NumericCardsSection,
Components::Blocks::PictureCardSection,
Components::Blocks::PrimaryGlossaryTable,
Components::Blocks::QuestionAndAnswer,
Components::Blocks::ResourceCardSection,
Components::Blocks::SplitHorizontalCard,
Expand Down
15 changes: 15 additions & 0 deletions app/services/cms/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ def self.all(page, page_size, params: {})
Collection.new(**response)
end

def self.all_records(params: {})
page = 1
per_page = 100
all_records = []

loop do
records = all(page, per_page, params:)
all_records += records.resources

break if (page * per_page) > records.total_records
page += 1
end
all_records
end

def self.clear_cache(key = nil)
if key
Rails.cache.delete("#{resource_key}-#{key}", namespace: "cms")
Expand Down
61 changes: 61 additions & 0 deletions spec/components/cms/primary_glossary_table_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Cms::PrimaryGlossaryTableComponent, type: :component do
let(:title) { Faker::Lorem.word }

context "with records" do
before do
stub_strapi_primary_computing_glossary_table_collection

render_inline(described_class.new(
title: title
))
end

it "renders the title" do
expect(page).to have_css("h2", text: title)
end

it "renders a table with headers" do
expect(page).to have_css("table")
expect(page).to have_css(".govuk-table__header", text: "Term")
expect(page).to have_css(".govuk-table__header", text: "Key Stage")
expect(page).to have_css(".govuk-table__header", text: "Definition")
end

it "renders the glossary records" do
expect(page).to have_css(".govuk-table__row", count: 5)
expect(page).to have_css(".cms-rich-text-block-component", count: 5)
end
end

context "with no title" do
before do
stub_strapi_primary_computing_glossary_table_collection

render_inline(described_class.new(
title: nil
))
end

it "does not render a title" do
expect(page).to_not have_css("h2")
end
end

context "when no records found" do
before do
stub_strapi_graphql_collection_error("primaryComputingGlossaryTables")

render_inline(described_class.new(
title: title
))
end

it "does not render" do
expect(page).to_not have_css("table")
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "rails_helper"

RSpec.describe Cms::DynamicComponents::PrimaryGlossaryTable do
before do
stub_strapi_primary_computing_glossary_table_collection
@text = Cms::Providers::Strapi::Factories::ComponentFactory.process_component(Cms::Mocks::DynamicComponents::PrimaryGlossaryTable.generate_raw_data)
end

it "should render as PrimaryGlossaryTableComponent" do
expect(@text.render).to be_a(Cms::PrimaryGlossaryTableComponent)
end
end
40 changes: 40 additions & 0 deletions spec/services/cms/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,44 @@ def self.resource_key
end.to raise_error(Cms::Errors::NoCmsProviderDefined)
end
end

describe "#all_records" do
describe "with multiple records" do
before do
blogs = Array.new(210) { Cms::Mocks::Blog.generate_raw_data }

stub_strapi_blog_collection(blogs:, page: 1, page_size: 100)
stub_strapi_blog_collection(blogs:, page: 2, page_size: 100)
stub_strapi_blog_collection(blogs:, page: 3, page_size: 100)
end

it "should return all records" do
expect(Cms::Collections::Blog.all_records.count).to eq(210)
end
end

describe "with one record" do
before do
blogs = [Cms::Mocks::Blog.generate_raw_data]

stub_strapi_blog_collection(blogs:, page: 1, page_size: 100)
end

it "should return one record" do
expect(Cms::Collections::Blog.all_records.count).to eq(1)
end
end

describe "with no records" do
before do
blogs = []

stub_strapi_blog_collection(blogs:, page: 1, page_size: 100)
end

it "should return one record" do
expect(Cms::Collections::Blog.all_records.count).to eq(0)
end
end
end
end
Loading