Skip to content

Commit

Permalink
Merge pull request #2186 from tvdeyen/skip-validations-on-element-toggle
Browse files Browse the repository at this point in the history
Do not validate element during toggle fold and create
  • Loading branch information
tvdeyen authored Sep 2, 2021
2 parents 35e4ea7 + 0d1378d commit a9811b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/controllers/alchemy/admin/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/alchemy/admin/elements_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/alchemy/admin/elements_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a9811b4

Please sign in to comment.