Add default tag rule

I tried to leverage turbo as much as possible
This commit is contained in:
Gaetan Craig-Riou
2025-07-23 15:18:29 +10:00
parent 8f0cdf8722
commit 7087d1b290
10 changed files with 104 additions and 15 deletions

View File

@@ -6,7 +6,7 @@
%col.actions{ width: "10%" }
%tr
%td
= hidden_field_tag element_name("id")
= hidden_field_tag element_name("id"), rule.id
= hidden_field_tag element_name("type"), rule.type
= hidden_field_tag element_name("priority"), index
= hidden_field_tag element_name("is_default"), rule.is_default

View File

@@ -379,16 +379,15 @@ module Admin
end
def load_tag_rule_types
# Load rule types
@tag_rule_types = [
{ id: "FilterShippingMethods", name: t('js.tag_rules.show_hide_shipping') },
{ id: "FilterPaymentMethods", name: t('js.tag_rules.show_hide_payment') },
{ id: "FilterOrderCycles", name: t('js.tag_rules.show_hide_order_cycles') }
[t(".form.tag_rules.show_hide_shipping"), "FilterShippingMethods"],
[t(".form.tag_rules.show_hide_payment"), "FilterPaymentMethods"],
[t(".form.tag_rules.show_hide_order_cycles"), "FilterOrderCycles"]
]
return unless helpers.feature?(:inventory, @object)
@tag_rule_types.prepend({ id: "FilterProducts", name: t('js.tag_rules.show_hide_variants') })
@tag_rule_types.prepend([t(".form.tag_rules.show_hide_variants"), "FilterProducts"])
end
def setup_property

View File

@@ -8,6 +8,21 @@ module Admin
success: lambda { head :no_content }
} }
def new
@index = params[:index]
status = :ok
if permitted_tag_rule_type.include?(params[:rule_type])
@default_rule = "TagRule::#{params[:rule_type]}".constantize.new(is_default: true)
else
flash.now[:error] = t(".not_supported_type")
status = :internal_server_error
end
respond_with do |format|
return format.turbo_stream { render :new, status: }
end
end
def map_by_tag
respond_to do |format|
format.json do
@@ -39,5 +54,9 @@ module Admin
Enterprise.managed_by(spree_current_user)
end
end
def permitted_tag_rule_type
%w{FilterOrderCycles FilterPaymentMethods FilterProducts FilterShippingMethods}
end
end
end

View File

@@ -142,6 +142,7 @@ module Spree
can [:admin, :index, :read, :create, :edit, :update_positions, :destroy], ProducerProperty
can :new, TagRule
can [:admin, :map_by_tag, :destroy], TagRule do |tag_rule|
user.enterprises.include? tag_rule.enterprise
end

View File

@@ -15,6 +15,7 @@
.no_rules
= t('.no_rules_yet')
.add_rule.text-center
-# TODO add pop up
%input.button.icon-plus{ type: 'button', value: t('.add_new_button') }
- else
- default_rules.each_with_index do |default_rule, index|
@@ -23,8 +24,18 @@
%hr
.add_rule.text-center
%input.button.icon-plus{ type: 'button', value: t('.add_new_button') }
%input.button.icon-plus{ type: 'button', value: t('.add_new_button'), "data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "tag_rule_add_new_default_rule" }
= render ModalComponent.new(id: "tag_rule_add_new_default_rule", close_button: false, modal_class: "tiny" ) do
#new-tag-rule-dialog{ "data-controller": "default-tag-rule" }
=hidden_field_tag "current_index", current_index, { "data-default-tag-rule-target": "index" }
.text-normal.margin-bottom-30.text-center
= t('.new_tag_rule_dialog.select_rule_type')
.text-center.margin-bottom-30
= select_tag :rule_type_selector, options_for_select(@tag_rule_types), { "data-controller": "tom-select", "data-default-tag-rule-target": "rule", class: "primary no-search" }
.text-center
%input.button.red.icon-plus{ type: 'button', value: "#{t('.new_tag_rule_dialog.add_rule')}", "data-action": "click->default-tag-rule#add click->modal#close", }
= render HelpModalComponent.new(id: "tag_rule_help_modal") do
#tag-rule-help
.margin-bottom-30.text-center

View File

@@ -0,0 +1,6 @@
- if flash[:error]
= turbo_stream.append "flashes" do
= render(partial: 'admin/shared/flashes', locals: { flashes: flash })
- else
= turbo_stream.append "default-tag-rule" do
= render TagRuleFormComponent.new(rule: @default_rule, rule_data: rule_data(@default_rule), index: @index)

View File

@@ -0,0 +1,24 @@
import { Controller } from "stimulus";
export default class extends Controller {
static targets = ["rule", "index"];
add() {
const rule_type = this.ruleTarget.value;
const index = this.indexTarget.value;
// fetch from backend
fetch(`tag_rules/new?rule_type=${rule_type}&index=${index}`, {
method: "GET",
headers: {
Accept: "text/vnd.turbo-stream.html",
},
})
.then((r) => r.text())
.then((html) => {
Turbo.renderStreamMessage(html);
this.indexTarget.value = parseInt(index) + 1;
})
.catch((error) => console.warn(error));
}
}

View File

@@ -1416,11 +1416,18 @@ en:
by_default: By Default
no_rules_yet: No default rules apply yet
add_new_button: '+ Add A New Default Rule'
new_tag_rule_dialog:
select_rule_type: "Select a rule type:"
add_rule: "Add Rule"
no_tags_yet: No tags apply to this enterprise yet
no_rules_yet: No rules apply to this tag yet
for_customers_tagged: 'For customers tagged:'
add_new_rule: '+ Add A New Rule'
add_new_tag: '+ Add A New Tag'
show_hide_variants: 'Show or Hide variants in my shopfront'
show_hide_shipping: 'Show or Hide shipping methods at checkout'
show_hide_payment: 'Show or Hide payment methods at checkout'
show_hide_order_cycles: 'Show or Hide order cycles in my shopfront'
users:
legend: "Users"
email_confirmation_notice_html: "Email confirmation is pending. We've sent a confirmation email to %{email}."
@@ -1573,6 +1580,8 @@ en:
invite_manager:
user_already_exists: "User already exists"
error: "Something went wrong"
tag_rules:
not_supported_type: Tag rule type not supported
order_cycles:
loading_flash:
loading_order_cycles: LOADING ORDER CYCLES
@@ -3672,9 +3681,6 @@ See the %{link} to find out more about %{sitename}'s features and to start using
order_cycle_tagged_bottom: "are:"
inventory_tagged_top: "Inventory variants tagged"
inventory_tagged_bottom: "are:"
new_tag_rule_dialog:
select_rule_type: "Select a rule type:"
add_rule: "Add Rule"
enterprise_fees:
inherit_from_product: "Inherit From Product"
orders:
@@ -3804,10 +3810,6 @@ See the %{link} to find out more about %{sitename}'s features and to start using
changing_on_hand_stock: Changing on hand stock levels...
stock_reset: Stocks reset to defaults.
tag_rules:
show_hide_variants: 'Show or Hide variants in my shopfront'
show_hide_shipping: 'Show or Hide shipping methods at checkout'
show_hide_payment: 'Show or Hide payment methods at checkout'
show_hide_order_cycles: 'Show or Hide order cycles in my shopfront'
visible: VISIBLE
not_visible: NOT VISIBLE
services:

View File

@@ -41,7 +41,7 @@ Openfoodnetwork::Application.routes.draw do
post :update_positions, on: :collection
end
resources :tag_rules, only: [:destroy]
resources :tag_rules, only: [:destroy, :new]
resources :vouchers, only: [:new, :create]
end

View File

@@ -34,4 +34,31 @@ RSpec.describe Admin::TagRulesController do
end
end
end
describe "#edit" do
let(:enterprise) { create(:distributor_enterprise) }
let(:params) { { rule_type:, index: 1 } }
let(:rule_type) { "FilterProducts" }
before do
controller_login_as_enterprise_user [enterprise]
end
it "returns new tag rule form" do
spree_get(:new, format: :turbo_stream, id: enterprise, params:)
expect(response).to render_template :new
end
context "wiht a wrong tag rule type" do
let(:rule_type) { "OtherType" }
it "returns an error" do
spree_get(:new, format: :turbo_stream, id: enterprise, params:)
expect(response).to render_template :new
expect(flash[:error]).to eq "Tag rule type not supported"
end
end
end
end