diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 4729c23562..a9017ea6c9 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -65,6 +65,8 @@ module Admin update_tag_rules(tag_rules_attributes) if tag_rules_attributes.present? update_enterprise_notifications + delete_custom_tab if params[:custom_tab] == 'false' + if @object.update(enterprise_params) flash[:success] = flash_message_for(@object, :successfully_updated) respond_with(@object) do |format| @@ -137,6 +139,11 @@ module Admin protected + def delete_custom_tab + @object.custom_tab.destroy if @object.custom_tab.present? + enterprise_params.delete(:custom_tab_attributes) + end + def build_resource enterprise = super enterprise.address ||= Spree::Address.new diff --git a/app/views/admin/enterprises/form/_white_label.html.haml b/app/views/admin/enterprises/form/_white_label.html.haml index 7405f99312..c4fcc63a7f 100644 --- a/app/views/admin/enterprises/form/_white_label.html.haml +++ b/app/views/admin/enterprises/form/_white_label.html.haml @@ -42,7 +42,8 @@ .three.columns.alpha = label_tag :custom_tab, t('.create_custom_tab') .three.columns - = check_box_tag :custom_tab, true, @object.custom_tab&.title.present?, { "data-controller": "checkbox-display", "data-checkbox-display-target-id-value": "custom_tab_form" } + = hidden_field_tag :custom_tab, "false" # submit `true` if checkbox is checked, `false` otherwise + = check_box_tag :custom_tab, "true", @object.custom_tab&.title.present?, { "data-controller": "checkbox-display", "data-checkbox-display-target-id-value": "custom_tab_form" } %div{id: "custom_tab_form"} = f.fields_for :custom_tab, @object.custom_tab do |custom_tab_form| diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index b76a9c066c..a75e006c34 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -770,6 +770,14 @@ describe ' expect(page). to have_field "enterprise_custom_tab_attributes_content", with: custom_tab.content end + + it "can delete custom tab if uncheck the checkbox" do + uncheck "Create custom tab in shopfront" + click_button 'Update' + expect(flash_message) + .to eq('Enterprise "First Distributor" has been successfully updated!') + expect(distributor1.reload.custom_tab).to be_nil + end end end end