mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-27 06:05:19 +00:00
Add deleting tag rule
TagRuleController is now a subclass of Spree::Admin::BaseController because Admin::ResourceController did not play well with turbo_stream. And to be honest we did not need all the functionality provided by the ResourceController
This commit is contained in:
@@ -15,10 +15,11 @@
|
||||
= rule_data[:text_top]
|
||||
%td
|
||||
= render TagListInputComponent.new(name: element_name("preferred_#{rule_data[:taggable]}_tags"), tags: rule.tags.split(","))
|
||||
%td.actions{ rowspan: 2 }
|
||||
-#%a{ class: "delete-tag-rule icon-trash no-text", "ng-click": "deleteTagRule(tagGroup || defaultTagGroup, rule)" }
|
||||
-# TODO implement delete action in turbo
|
||||
%a{ class: "delete-tag-rule icon-trash no-text" }
|
||||
%td.actions{ rowspan: 2 , "data-controller": "delete-tag-rule", "data-delete-tag-rule-index-value": index }
|
||||
- if rule.new_record?
|
||||
= link_to("", "#", { "data-action": "click->delete-tag-rule#delete" ,class: "delete-tag-rule icon-trash no-text"})
|
||||
- else
|
||||
= link_to("", "#{admin_enterprise_tag_rule_url(rule.enterprise_id, rule.id)}?index=#{index}", { "data-turbo-method": "delete", "data-turbo-confirm": t("admin.tag_rules.confirm_delete"), class: "delete-tag-rule icon-trash no-text" })
|
||||
%tr
|
||||
%td
|
||||
%span.text-normal
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Admin
|
||||
class TagRulesController < Admin::ResourceController
|
||||
class TagRulesController < Spree::Admin::BaseController
|
||||
respond_to :json
|
||||
|
||||
respond_override destroy: { json: {
|
||||
success: lambda { head :no_content }
|
||||
} }
|
||||
|
||||
def new
|
||||
@index = params[:index]
|
||||
status = :ok
|
||||
@@ -23,6 +19,24 @@ module Admin
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@rule = TagRule.find(params[:id])
|
||||
@index = params[:index]
|
||||
authorize! :destroy, @rule
|
||||
|
||||
status = :ok
|
||||
if @rule.destroy
|
||||
flash[:success] = Spree.t(:successfully_removed, resource: "Tag Rule")
|
||||
else
|
||||
flash.now[:error] = t(".destroy_error")
|
||||
status = :internal_server_error
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
format.turbo_stream { render :destroy, status: }
|
||||
end
|
||||
end
|
||||
|
||||
def map_by_tag
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
@@ -55,6 +69,10 @@ module Admin
|
||||
end
|
||||
end
|
||||
|
||||
def model_class
|
||||
TagRule
|
||||
end
|
||||
|
||||
def permitted_tag_rule_type
|
||||
%w{FilterOrderCycles FilterPaymentMethods FilterProducts FilterShippingMethods}
|
||||
end
|
||||
|
||||
4
app/views/admin/tag_rules/destroy.turbo_stream.haml
Normal file
4
app/views/admin/tag_rules/destroy.turbo_stream.haml
Normal file
@@ -0,0 +1,4 @@
|
||||
- unless flash[:error]
|
||||
= turbo_stream.remove "tr_#{@index}"
|
||||
= turbo_stream.append "flashes" do
|
||||
= render(partial: 'admin/shared/flashes', locals: { flashes: flash })
|
||||
13
app/webpacker/controllers/delete_tag_rule_controller.js
Normal file
13
app/webpacker/controllers/delete_tag_rule_controller.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Controller } from "stimulus";
|
||||
|
||||
export default class extends Controller {
|
||||
static values = { index: Number };
|
||||
|
||||
delete(e) {
|
||||
// prevent default link action
|
||||
e.preventDefault();
|
||||
if (confirm(I18n.t("admin.tag_rules.confirm_delete")) == true) {
|
||||
document.getElementById(`tr_${this.indexValue}`).remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1582,6 +1582,8 @@ en:
|
||||
error: "Something went wrong"
|
||||
tag_rules:
|
||||
not_supported_type: Tag rule type not supported
|
||||
confirm_delete: Are you sure you want to delete this rule ?
|
||||
destroy_error: There was an issue when removing the Tag rule
|
||||
order_cycles:
|
||||
loading_flash:
|
||||
loading_order_cycles: LOADING ORDER CYCLES
|
||||
|
||||
@@ -3,34 +3,42 @@
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe Admin::TagRulesController do
|
||||
describe "destroy" do
|
||||
context "json" do
|
||||
let(:format) { :json }
|
||||
let(:format) { :turbo_stream }
|
||||
|
||||
let(:enterprise) { create(:distributor_enterprise) }
|
||||
let!(:tag_rule) { create(:filter_order_cycles_tag_rule, enterprise:) }
|
||||
let(:params) { { format:, id: tag_rule.id } }
|
||||
describe "#destroy" do
|
||||
let(:enterprise) { create(:distributor_enterprise) }
|
||||
let!(:tag_rule) { create(:filter_order_cycles_tag_rule, enterprise:) }
|
||||
let(:params) { { format:, id: tag_rule.id } }
|
||||
|
||||
context "where I don't manage the tag rule enterprise" do
|
||||
let(:user) { create(:user) }
|
||||
context "where I don't manage the tag rule enterprise" do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
before do
|
||||
user.owned_enterprises << create(:enterprise)
|
||||
allow(controller).to receive(:spree_current_user) { user }
|
||||
end
|
||||
|
||||
it "redirects to unauthorized" do
|
||||
spree_delete :destroy, params
|
||||
expect(response).to redirect_to unauthorized_path
|
||||
end
|
||||
before do
|
||||
user.owned_enterprises << create(:enterprise)
|
||||
allow(controller).to receive(:spree_current_user) { user }
|
||||
end
|
||||
|
||||
context "where I manage the tag rule enterprise" do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { enterprise.owner }
|
||||
end
|
||||
it "redirects to unauthorized" do
|
||||
spree_delete :destroy, params
|
||||
expect(response).to redirect_to unauthorized_path
|
||||
end
|
||||
end
|
||||
|
||||
it { expect{ spree_delete :destroy, params }.to change{ TagRule.count }.by(-1) }
|
||||
context "where I manage the tag rule enterprise" do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { enterprise.owner }
|
||||
end
|
||||
|
||||
it { expect{ spree_delete :destroy, params }.to change{ TagRule.count }.by(-1) }
|
||||
|
||||
context "when an error happens" do
|
||||
it "displays an error flash" do
|
||||
allow_any_instance_of(TagRule).to receive(:destroy).and_return(false)
|
||||
|
||||
spree_delete :destroy, params
|
||||
|
||||
expect(flash[:error]).to eq "There was an issue when removing the Tag rule"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -45,7 +53,7 @@ RSpec.describe Admin::TagRulesController do
|
||||
end
|
||||
|
||||
it "returns new tag rule form" do
|
||||
spree_get(:new, format: :turbo_stream, id: enterprise, params:)
|
||||
spree_get(:new, format:, id: enterprise, params:)
|
||||
|
||||
expect(response).to render_template :new
|
||||
end
|
||||
@@ -54,7 +62,7 @@ RSpec.describe Admin::TagRulesController do
|
||||
let(:rule_type) { "OtherType" }
|
||||
|
||||
it "returns an error" do
|
||||
spree_get(:new, format: :turbo_stream, id: enterprise, params:)
|
||||
spree_get(:new, format:, id: enterprise, params:)
|
||||
|
||||
expect(response).to render_template :new
|
||||
expect(flash[:error]).to eq "Tag rule type not supported"
|
||||
|
||||
Reference in New Issue
Block a user