mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-11 18:26:50 +00:00
Display existing grouped tag rules
This commit is contained in:
@@ -108,8 +108,30 @@ module Admin
|
||||
}
|
||||
end
|
||||
|
||||
# Group tag rules per rule.preferred_customer_tags
|
||||
def tag_groups(tag_rules)
|
||||
tag_rules.each_with_object([]) do |tag_rule, tag_groups|
|
||||
tag_group = find_match(tag_groups, tag_rule.preferred_customer_tags.split(","))
|
||||
|
||||
if tag_group[:rules].blank?
|
||||
tag_groups << tag_group
|
||||
tag_group[:position] = tag_groups.count
|
||||
end
|
||||
|
||||
tag_group[:rules] << tag_rule
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_match(tag_groups, tags)
|
||||
tag_groups.each do |tag_group|
|
||||
return tag_group if tag_group[:tags].length == tags.length &&
|
||||
(tag_group[:tags] & tags) == tag_group[:tags]
|
||||
end
|
||||
{ tags:, rules: [] }
|
||||
end
|
||||
|
||||
def build_enterprise_side_menu_items(is_shop:, show_options: ) # rubocop:disable Metrics/MethodLength
|
||||
[
|
||||
{ name: 'primary_details', icon_class: "icon-home", show: true, selected: 'selected' },
|
||||
|
||||
@@ -1,8 +1,45 @@
|
||||
.row
|
||||
.row
|
||||
.eleven.columns.alpha.omega
|
||||
%div
|
||||
%div{ "data-turbo": true }
|
||||
- rules = @enterprise.tag_rules.prioritised.reject(&:is_default)
|
||||
- if rules.empty?
|
||||
.no_tags
|
||||
= t('.no_tags_yet')
|
||||
= render 'admin/enterprises/form/tag_rules/default_rules', f: f
|
||||
|
||||
= render 'admin/enterprises/form/tag_rules/default_rules', f: f
|
||||
|
||||
#customer-tag-rule
|
||||
-# TODO both index can't overlap
|
||||
- customer_rule_index = 10
|
||||
- tag_groups(rules).each_with_index do |group, group_index|
|
||||
%div{ id: "tg_#{group_index}", "data-controller": "tag-rule-group"}
|
||||
.customer_tag
|
||||
.header
|
||||
%table
|
||||
%colgroup
|
||||
%col{width: '35%'}
|
||||
%col{width: '65%'}
|
||||
%tr
|
||||
%td
|
||||
%h5
|
||||
= t('.for_customers_tagged')
|
||||
%td
|
||||
= render(TagListInputComponent.new(name: "group[#{group_index}][preferred_customer_tags]",
|
||||
tags: group[:tags],
|
||||
hidden_field_data_options: { "data-action": "input->tag-rule-group#updatePreferredCustomerTag", "data-tag-rule-group-target": "customerTag" }))
|
||||
#new-customer-tag-rule
|
||||
- group[:rules].each_with_index do |rule, rule_index|
|
||||
- customer_rule_index = customer_rule_index + 1
|
||||
= render(TagRuleFormComponent.new(rule: rule,
|
||||
rule_data: rule_data(rule),
|
||||
index: customer_rule_index,
|
||||
customer_tags: group[:tags],
|
||||
hidden_field_customer_tag_options: { "data-tag-rule-group-target": "ruleCustomerTag" }))
|
||||
|
||||
%hr
|
||||
.add_rule.text-center
|
||||
%input.button{ type: 'button', value: t('.add_new_rule') }
|
||||
|
||||
.add_tag
|
||||
%input.button{ type: 'button', value: t('.add_new_tag') }
|
||||
|
||||
|
||||
11
app/webpacker/controllers/tag_rule_group_controller.js
Normal file
11
app/webpacker/controllers/tag_rule_group_controller.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Controller } from "stimulus";
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = ["customerTag", "ruleCustomerTag"];
|
||||
|
||||
updatePreferredCustomerTag() {
|
||||
const customerTag = this.customerTagTarget.value
|
||||
|
||||
this.ruleCustomerTagTargets.forEach((element) => element.value = customerTag)
|
||||
}
|
||||
}
|
||||
@@ -56,4 +56,81 @@ RSpec.describe Admin::EnterprisesHelper do
|
||||
expect(helper.enterprise_sells_options.map(&:first)).to eq %w[unspecified none own any]
|
||||
end
|
||||
end
|
||||
|
||||
describe "#tag_groups" do
|
||||
subject { helper.tag_groups(tag_rules) }
|
||||
|
||||
context "with one group" do
|
||||
let(:tag_rules) { [tag_rule] }
|
||||
let(:tag_rule) { create(:filter_products_tag_rule, preferred_customer_tags: "test") }
|
||||
|
||||
it "return an array of one tag group" do
|
||||
expected = [
|
||||
{
|
||||
position: 1,
|
||||
rules: [tag_rule],
|
||||
tags: ["test"]
|
||||
}
|
||||
]
|
||||
expect(subject.length).to eq(1)
|
||||
expect(subject).to eq(expected)
|
||||
end
|
||||
|
||||
context "wiht mutiple rules" do
|
||||
let(:enterprise) { create(:enterprise) }
|
||||
let(:tag_rules) {
|
||||
create_list(:filter_products_tag_rule, 3, enterprise:, preferred_customer_tags: "good")
|
||||
}
|
||||
|
||||
it "returns all the rules associated with the tag group" do
|
||||
expect(subject.length).to eq(1)
|
||||
|
||||
group = subject.first
|
||||
expect(group[:rules].length).to eq(3)
|
||||
expect(group[:tags]).to eq(["good"])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with multiple group" do
|
||||
let(:tag_rules) { [tag_rule_group1, tag_rule_group2, tag_rule_group3] }
|
||||
let(:tag_rule_group1) {
|
||||
create(:filter_products_tag_rule, enterprise:, preferred_customer_tags: "group_1")
|
||||
}
|
||||
let(:tag_rule_group2) {
|
||||
create(:filter_products_tag_rule, enterprise:, preferred_customer_tags: "group_2")
|
||||
}
|
||||
let(:tag_rule_group3) {
|
||||
create(:filter_products_tag_rule, enterprise:, preferred_customer_tags: "group_3")
|
||||
}
|
||||
let(:enterprise) { create(:enterprise) }
|
||||
|
||||
it "return an array of all the group" do
|
||||
expect(subject.length).to eq(3)
|
||||
expect(subject.first[:tags]).to eq(["group_1"])
|
||||
expect(subject.second[:tags]).to eq(["group_2"])
|
||||
expect(subject.third[:tags]).to eq(["group_3"])
|
||||
end
|
||||
|
||||
context "with multiple rules per group" do
|
||||
let(:tag_rules) { enterprise.tag_rules }
|
||||
let!(:tag_rules_group1) {
|
||||
create_list(:filter_products_tag_rule, 3, enterprise:, preferred_customer_tags: "group_1")
|
||||
}
|
||||
let!(:tag_rules_group2) {
|
||||
create(:filter_products_tag_rule, enterprise:, preferred_customer_tags: "group_2")
|
||||
}
|
||||
let!(:tag_rules_group3) {
|
||||
create_list(:filter_products_tag_rule, 2, enterprise:, preferred_customer_tags: "group_3")
|
||||
}
|
||||
|
||||
it "matches the rules with the correct group" do
|
||||
expect(subject.length).to eq(3)
|
||||
expect(subject.first[:rules].length).to eq(3)
|
||||
expect(subject.second[:rules].length).to eq(1)
|
||||
expect(subject.third[:rules].length).to eq(2)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user