Fix variant tag rules endpoint

It now returns tag rules filtered on the preferred variant tags and not
the prefered customer tags
This commit is contained in:
Gaetan Craig-Riou
2025-11-03 15:32:41 +11:00
parent ffd5817749
commit bb8ecccc31
4 changed files with 23 additions and 14 deletions

View File

@@ -57,7 +57,7 @@ module Admin
TagRule.matching_variant_tag_rules_by_enterprises(params[:enterprise_id], params[:q])
@formatted_tag_rules = tag_rules.each_with_object({}) do |rule, mapping|
rule.preferred_customer_tags.split(",").each do |tag|
rule.preferred_variant_tags.split(",").each do |tag|
if mapping[tag]
mapping[tag][:rules] += 1
else

View File

@@ -27,7 +27,7 @@ class TagRule < ApplicationRecord
return [] if rules.empty?
rules.select { |r| r.preferred_customer_tags =~ /#{tag}/ }
rules.select { |r| r.preferred_variant_tags =~ /#{tag}/ }
end
# The following method must be overriden in a concrete tagRule

View File

@@ -76,13 +76,20 @@ RSpec.describe Admin::TagRulesController do
let(:enterprise) { create(:distributor_enterprise) }
let(:q) { "" }
let!(:rule1) {
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "Tag-1" )
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "Tag-1",
preferred_variant_tags: "variant-tag-1" )
}
let!(:rule2) {
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "Tag-1" )
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "Tag-1",
preferred_variant_tags: "variant2-tag-1" )
}
let!(:rule3) {
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "organic" )
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "organic",
preferred_variant_tags: "variant-organic" )
}
let!(:rule4) {
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "organic",
preferred_variant_tags: "variant-tag-1" )
}
before do
@@ -93,8 +100,9 @@ RSpec.describe Admin::TagRulesController do
spree_get(:variant_tag_rules, format: :html, enterprise_id: enterprise.id, q:)
expect(response).to render_template :variant_tag_rules
expect(response.body).to include "Tag-1 has 2 rules"
expect(response.body).to include "organic has 1 rule"
expect(response.body).to include "variant-tag-1 has 2 rules"
expect(response.body).to include "variant2-tag-1 has 1 rule"
expect(response.body).to include "variant-organic has 1 rule"
end
context "with search string" do
@@ -104,8 +112,9 @@ RSpec.describe Admin::TagRulesController do
spree_get(:variant_tag_rules, format: :html, enterprise_id: enterprise.id, q:)
expect(response).to render_template :variant_tag_rules
expect(response.body).not_to include "Tag-1 has 2 rules"
expect(response.body).to include "organic has 1 rule"
expect(response.body).not_to include "variant-tag-1 has 2 rules"
expect(response.body).not_to include "variant2-tag-1 has 1 rule"
expect(response.body).to include "variant-organic has 1 rule"
end
end
end

View File

@@ -12,20 +12,20 @@ RSpec.describe TagRule do
describe ".matching_variant_tag_rules_by_enterprises" do
let(:enterprise) { create(:enterprise) }
let!(:rule1) {
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "filtered" )
create(:filter_variants_tag_rule, enterprise:, preferred_variant_tags: "filtered" )
}
let!(:rule2) {
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "filtered" )
create(:filter_variants_tag_rule, enterprise:, preferred_variant_tags: "filtered" )
}
let!(:rule3) {
create(:filter_variants_tag_rule, enterprise: create(:enterprise),
preferred_customer_tags: "filtered" )
preferred_variant_tags: "filtered" )
}
let!(:rule4) {
create(:filter_variants_tag_rule, enterprise:, preferred_customer_tags: "other-tag" )
create(:filter_variants_tag_rule, enterprise:, preferred_variant_tags: "other-tag" )
}
let!(:rule5) {
create(:filter_order_cycles_tag_rule, enterprise:, preferred_customer_tags: "filtered" )
create(:filter_order_cycles_tag_rule, enterprise:, preferred_exchange_tags: "filtered" )
}
it "returns a list of rule partially matching the tag" do