From b7351b847aafd2d569fc150584b20d9ff24205ba Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sat, 28 Aug 2021 18:09:48 +0200 Subject: [PATCH 1/2] Do not validate element during toggle fold Elements with ingredient validations might get invalid on update. Since we do not care about the validations during toggling a UI state we skip them. --- app/controllers/alchemy/admin/elements_controller.rb | 6 +++++- spec/requests/alchemy/admin/elements_controller_spec.rb | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/controllers/alchemy/admin/elements_controller.rb b/app/controllers/alchemy/admin/elements_controller.rb index ebaee1de57..557f44310d 100644 --- a/app/controllers/alchemy/admin/elements_controller.rb +++ b/app/controllers/alchemy/admin/elements_controller.rb @@ -91,10 +91,14 @@ def order end end + # Toggle fodls the element and persists the state in the db + # + # Ingredient validations might make the element invalid. + # In this case we are just toggling a UI state and do not care about the validations. def fold @page = @element.page @element.folded = !@element.folded - @element.save + @element.save(validate: false) end private diff --git a/spec/requests/alchemy/admin/elements_controller_spec.rb b/spec/requests/alchemy/admin/elements_controller_spec.rb index 8f77b787f6..67d260f6e6 100644 --- a/spec/requests/alchemy/admin/elements_controller_spec.rb +++ b/spec/requests/alchemy/admin/elements_controller_spec.rb @@ -36,6 +36,15 @@ post fold_admin_element_path(id: element.id, format: :js) expect(response.body).to include("Alchemy.Tinymce.init([#{element.ingredient_by_role(:text).id}]);") end + + context "with validations" do + let(:element) { create(:alchemy_element, :with_ingredients, name: :all_you_can_eat_ingredients) } + + it "saves without running validations" do + post fold_admin_element_path(id: element.id, format: :js) + expect(element.reload).to be_folded + end + end end end From 0d1378dae99450eab52d0afe83b2d304534726c5 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sat, 28 Aug 2021 18:42:40 +0200 Subject: [PATCH 2/2] Do not validate during create If an element has ingredient validations careating it first and calling valid? after that triggers the on update validation callback. Resulting in returning the new template instead. Solved by only building the element and saving it later. This does not trigger the on update callback. To spare an extra save we set the elements position instead of moving it down. The only unnecessary ave we could not prevent is if we paste from clipboard. But this is acceptable. --- app/controllers/alchemy/admin/elements_controller.rb | 6 +++--- .../alchemy/admin/elements_controller_spec.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/controllers/alchemy/admin/elements_controller.rb b/app/controllers/alchemy/admin/elements_controller.rb index 557f44310d..826eae9737 100644 --- a/app/controllers/alchemy/admin/elements_controller.rb +++ b/app/controllers/alchemy/admin/elements_controller.rb @@ -32,14 +32,14 @@ def create if @paste_from_clipboard = params[:paste_from_clipboard].present? @element = paste_element_from_clipboard else - @element = Element.create(create_element_params) + @element = Element.new(create_element_params) end if @page.definition["insert_elements_at"] == "top" @insert_at_top = true - @element.move_to_top + @element.position = 1 end end - if @element.valid? + if @element.save render :create else @element.page_version = @page_version diff --git a/spec/controllers/alchemy/admin/elements_controller_spec.rb b/spec/controllers/alchemy/admin/elements_controller_spec.rb index 34249a7014..cfdcff05ff 100644 --- a/spec/controllers/alchemy/admin/elements_controller_spec.rb +++ b/spec/controllers/alchemy/admin/elements_controller_spec.rb @@ -189,6 +189,16 @@ module Alchemy expect(subject).to render_template(:new) end end + + context "with ingredient validations" do + subject do + post :create, params: { element: { page_version_id: page_version.id, name: "all_you_can_eat_ingredients" } }, xhr: true + end + + it "creates element without error" do + expect(subject).to render_template(:create) + end + end end describe "#update" do