Skip to content

Commit

Permalink
Merge pull request #9953 from alphagov/content-modelling/907-allow-fi…
Browse files Browse the repository at this point in the history
…elds-with-copy-code-to-be-configured

(907) Allow fields with copy code to be configured
  • Loading branch information
pezholio authored Feb 19, 2025
2 parents af88e36 + 14fd6fb commit e345da6
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def rows
{
key: key.titleize,
value: object[key],
data: copy_embed_code(key),
data: is_embeddable?(key) ? copy_embed_code(key) : nil,
},
]
rows.push(embed_code_row(key)) unless is_editable
rows.push(embed_code_row(key)) unless is_editable || !is_embeddable?(key)
rows
}.flatten
end
Expand All @@ -41,6 +41,14 @@ def embed_code_row(key)
}
end

def embeddable_fields
@embeddable_fields = content_block_edition.document.schema.subschema(object_type).embeddable_fields
end

def is_embeddable?(key)
embeddable_fields.include?(key)
end

def copy_embed_code(key)
unless is_editable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def latest_draft
editions.where(state: :draft).order(created_at: :asc).last
end

def schema
@schema ||= ContentBlockManager::ContentBlock::Schema.find_by_block_type(block_type)
end

private

def embed_code_prefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Schema
VALID_SCHEMAS = %w[email_address postal_address pension].freeze
private_constant :VALID_SCHEMAS

CONFIG_PATH = File.join(ContentBlockManager::Engine.root, "config", "content_block_manager.yml")

class << self
def valid_schemas
VALID_SCHEMAS
Expand All @@ -26,6 +28,10 @@ def find_by_block_type(block_type)
def is_valid_schema?(key)
key.start_with?(SCHEMA_PREFIX) && key.end_with?(*valid_schemas)
end

def schema_settings
@schema_settings ||= YAML.load_file(CONFIG_PATH)
end
end

attr_reader :id, :body
Expand All @@ -52,7 +58,7 @@ def subschema(name)
end

def subschemas
@subschemas ||= embedded_objects.map { |object| EmbeddedSchema.new(*object) }
@subschemas ||= embedded_objects.map { |object| EmbeddedSchema.new(*object, @id) }
end

def permitted_params
Expand All @@ -63,8 +69,13 @@ def block_type
@block_type ||= id.delete_prefix("#{SCHEMA_PREFIX}_")
end

def embeddable_fields
config["embeddable_fields"] || fields
end

class EmbeddedSchema < Schema
def initialize(id, body)
def initialize(id, body, parent_schema_id)
@parent_schema_id = parent_schema_id
body = body["patternProperties"]&.values&.first || raise(ArgumentError, "Subschema `#{id}` is invalid")
super(id, body)
end
Expand All @@ -76,13 +87,23 @@ def fields
def block_type
@id
end

private

def config
self.class.schema_settings.dig("schemas", @parent_schema_id, "subschemas", @id) || {}
end
end

private

def embedded_objects
@body["properties"].select { |_k, v| v["type"] == "object" }
end

def config
@config ||= self.class.schema_settings.dig("schemas", @id) || {}
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
schemas:
content_block_pension:
subschemas:
rates:
embeddable_fields:
- amount
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Feature: View a content object
Scenario: GDS Editor can copy embed code for a specific field
When I visit the Content Block Manager home page
When I click to view the document with title "My pension"
And I click to copy the embed code for the pension "My pension", rate "My rate" and field "name"
And I click to copy the embed code for the pension "My pension", rate "My rate" and field "amount"
Then the embed code should be copied to my clipboard

Scenario: GDS Editor without javascript can see embed code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ class ContentBlockManager::Shared::EmbeddedObjects::SummaryCardComponentTest < V
}
end

let(:content_block_edition) { build_stubbed(:content_block_edition, :email_address, details:) }
let(:schema) { stub(:schema) }
let(:subschema) { stub(:subschema, embeddable_fields: %w[name field-1 field-2]) }
let(:document) { build(:content_block_document, :email_address, schema:) }
let(:content_block_edition) { build_stubbed(:content_block_edition, :email_address, details:, document:) }

before do
schema.stubs(:subschema).returns(subschema)
end

it "renders a summary list" do
component = ContentBlockManager::Shared::EmbeddedObjects::SummaryCardComponent.new(
Expand Down Expand Up @@ -73,6 +80,40 @@ class ContentBlockManager::Shared::EmbeddedObjects::SummaryCardComponentTest < V
assert_selector ".govuk-summary-list__row[data-embed-code-row='true']", text: content_block_edition.document.embed_code_for_field("embedded-objects/my-embedded-object/field-2")
end

describe "when only some fields are embeddable" do
let(:subschema) { stub(:subschema, embeddable_fields: %w[field-1]) }

it "only renders copy code buttons for embeddable fields" do
component = ContentBlockManager::Shared::EmbeddedObjects::SummaryCardComponent.new(
content_block_edition:,
object_type: "embedded-objects",
object_name: "my-embedded-object",
)

render_inline component

assert_no_selector ".govuk-summary-list__row[data-embed-code='#{content_block_edition.document.embed_code_for_field('embedded-objects/my-embedded-object/name')}']", text: "Name"
assert_no_selector ".govuk-summary-list__row[data-embed-code='#{content_block_edition.document.embed_code_for_field('embedded-objects/my-embedded-object/field-2')}']", text: "Field 2"

assert_selector ".govuk-summary-list__row[data-embed-code='#{content_block_edition.document.embed_code_for_field('embedded-objects/my-embedded-object/field-1')}']", text: "Field 1"
end

it "only renders an embed code row for embeddable fields" do
component = ContentBlockManager::Shared::EmbeddedObjects::SummaryCardComponent.new(
content_block_edition:,
object_type: "embedded-objects",
object_name: "my-embedded-object",
)

render_inline component

assert_no_selector ".govuk-summary-list__row[data-embed-code-row='true']", text: content_block_edition.document.embed_code_for_field("embedded-objects/my-embedded-object/name")
assert_no_selector ".govuk-summary-list__row[data-embed-code-row='true']", text: content_block_edition.document.embed_code_for_field("embedded-objects/my-embedded-object/field-2")

assert_selector ".govuk-summary-list__row[data-embed-code-row='true']", text: content_block_edition.document.embed_code_for_field("embedded-objects/my-embedded-object/field-1")
end
end

describe "when card is editable" do
it "renders a summary list with edit link" do
component = ContentBlockManager::Shared::EmbeddedObjects::SummaryCardComponent.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@
latest_edition_id { nil }
live_edition_id { nil }

transient do
schema { nil }
end

ContentBlockManager::ContentBlock::Schema.valid_schemas.each do |type|
trait type.to_sym do
block_type { type }
schema { build(:content_block_schema, block_type: type) }
end
end

after(:build) do |content_block_document, evaluator|
content_block_document.stubs(:schema).returns(evaluator.schema)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,20 @@ class ContentBlockManager::ContentBlockDocumentTest < ActiveSupport::TestCase
assert_equal newest_draft, document.latest_draft
end
end

describe "#schema" do
let(:document) { build(:content_block_document, :email_address) }
let(:schema) { build(:content_block_schema) }

it "returns a schema object" do
document.unstub(:schema)

ContentBlockManager::ContentBlock::Schema
.expects(:find_by_block_type)
.with(document.block_type)
.returns(schema)

assert_equal document.schema, schema
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ class ContentBlockManager::SchemaTest < ActiveSupport::TestCase
end
end

describe ".schema_settings" do
it "should return the schema settings" do
stub_schema = stub("schema_settings")
YAML.expects(:load_file)
.with(ContentBlockManager::ContentBlock::Schema::CONFIG_PATH)
.returns(stub_schema)

assert_equal ContentBlockManager::ContentBlock::Schema.schema_settings, stub_schema
end
end

describe "when a schema has embedded objects" do
let(:body) do
{
Expand Down Expand Up @@ -298,4 +309,36 @@ class ContentBlockManager::SchemaTest < ActiveSupport::TestCase
end
end
end

describe "#embeddable_fields" do
describe "when config exists for a schema" do
before do
ContentBlockManager::ContentBlock::Schema
.stubs(:schema_settings)
.returns({
"schemas" => {
schema.id => {
"embeddable_fields" => %w[something else],
},
},
})
end

it "returns the config values" do
assert_equal schema.embeddable_fields, %w[something else]
end
end

describe "when config does not exist for a schema" do
before do
ContentBlockManager::ContentBlock::Schema
.stubs(:schema_settings)
.returns({})
end

it "returns the fields" do
assert_equal schema.embeddable_fields, schema.fields
end
end
end
end

0 comments on commit e345da6

Please sign in to comment.