Skip to content

Commit

Permalink
Only show copy code on embeddable fields
Browse files Browse the repository at this point in the history
  • Loading branch information
pezholio committed Feb 19, 2025
1 parent 89a82c3 commit 0336c55
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 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 @@ -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

0 comments on commit 0336c55

Please sign in to comment.