Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(910) Don’t change the object key when changing the name #9947

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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def update
redirect_to content_block_manager.review_embedded_object_content_block_manager_content_block_edition_path(
@content_block_edition,
object_type: @subschema.block_type,
object_name: @content_block_edition.key_for_object(@object),
object_name: params[:object_name],
)
end
rescue ActiveRecord::RecordInvalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,7 @@ def add_object_to_details(object_type, body)
end

def update_object_with_details(object_type, object_name, body)
key = key_for_object(body)

if key != object_name
details[object_type].delete(object_name)
end

add_object_to_details(object_type, body)
details[object_type][object_name] = body.to_h
end

def key_for_object(object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
When("I complete the {string} form with the following fields:") do |object_type, table|
fields = table.hashes.first
@details = fields
@object_name ||= @details["name"].parameterize
fields.keys.each do |k|
field = find_field "content_block_manager_content_block_edition_details_#{object_type.pluralize}_#{k}"
if field.tag_name == "select"
Expand All @@ -37,7 +38,7 @@

assert_not_nil edition
assert_not_nil edition.document
key = @details["name"].parameterize
key = @object_name

@details.keys.each do |k|
assert_equal edition.details[object_type.parameterize.pluralize][key][k], @details[k]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class ContentBlockManager::ContentBlock::Editions::EmbeddedObjectsTest < ActionD
assert_equal "Something edited. You can add another something or continue to create schema block", flash[:notice]
end

it "should rename the object if a new name is given" do
it "should not rename the object if a new name is given" do
put content_block_manager.embedded_object_content_block_manager_content_block_edition_path(
edition,
object_type:,
Expand All @@ -180,12 +180,12 @@ class ContentBlockManager::ContentBlock::Editions::EmbeddedObjectsTest < ActionD
assert_redirected_to content_block_manager.review_embedded_object_content_block_manager_content_block_edition_path(
edition,
object_type:,
object_name: "new-name",
object_name: "embedded",
)

updated_edition = edition.reload

assert_equal updated_edition.details, { "something" => { "new-name" => { "name" => "New Name", "is" => "different" } } }
assert_equal updated_edition.details, { "something" => { "embedded" => { "name" => "New Name", "is" => "different" } } }
end

it "should render errors if a validation error is thrown" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,10 @@ class ContentBlockManager::ContentBlockEditionTest < ActiveSupport::TestCase
assert_equal content_block_edition.details["something"], { "my-thing" => { "name" => "My thing", "something" => "changed" } }
end

it "removes the original object if the name changes" do
it "keeps the original key name if the name changes" do
content_block_edition.update_object_with_details("something", "my-thing", { "name" => "Other thing", "something" => "changed" })

assert_equal content_block_edition.details["something"], { "other-thing" => { "name" => "Other thing", "something" => "changed" } }
end

it "creates a random key if a name is not provided" do
SecureRandom.expects(:alphanumeric).at_least_once.returns("RANDOM-STRING")
content_block_edition.update_object_with_details("something", "my-thing", { "something" => "changed" })

assert_equal content_block_edition.details["something"], { "random-string" => { "something" => "changed" } }
assert_equal content_block_edition.details["something"], { "my-thing" => { "name" => "Other thing", "something" => "changed" } }
end
end

Expand Down
Loading