mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-11 18:26:50 +00:00
Add tag rule to filter by variant
This commit is contained in:
22
app/models/tag_rule/filter_variants.rb
Normal file
22
app/models/tag_rule/filter_variants.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class TagRule
|
||||
class FilterVariants < TagRule
|
||||
preference :matched_variants_visibility, :string, default: "visible"
|
||||
preference :variant_tags, :string, default: ""
|
||||
|
||||
def tags_match?(variant)
|
||||
variant_tags = variant&.[]("tag_list") || []
|
||||
preferred_tags = preferred_variant_tags.split(",")
|
||||
variant_tags.intersect?(preferred_tags)
|
||||
end
|
||||
|
||||
def reject_matched?
|
||||
preferred_matched_variants_visibility != "visible"
|
||||
end
|
||||
|
||||
def tags
|
||||
preferred_variant_tags
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -13,6 +13,10 @@ FactoryBot.define do
|
||||
enterprise factory: :distributor_enterprise
|
||||
end
|
||||
|
||||
factory :filter_variants_tag_rule, class: TagRule::FilterVariants do
|
||||
enterprise factory: :distributor_enterprise
|
||||
end
|
||||
|
||||
factory :filter_payment_methods_tag_rule, class: TagRule::FilterPaymentMethods do
|
||||
enterprise factory: :distributor_enterprise
|
||||
end
|
||||
|
||||
60
spec/models/tag_rule/filter_variants_spec.rb
Normal file
60
spec/models/tag_rule/filter_variants_spec.rb
Normal file
@@ -0,0 +1,60 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe TagRule::FilterVariants do
|
||||
let(:tag_rule) { build(:filter_variants_tag_rule, preferred_variant_tags: variant_tags) }
|
||||
let(:variant_tags) { "" }
|
||||
|
||||
describe "#tags" do
|
||||
let(:variant_tags) { "my_tag" }
|
||||
|
||||
it "return the variants tags" do
|
||||
expect(tag_rule.tags).to eq("my_tag")
|
||||
end
|
||||
end
|
||||
|
||||
describe "#tags_match?" do
|
||||
let(:variant_tags) { "my_tag" }
|
||||
|
||||
context "when the variant is nil" do
|
||||
it "returns false" do
|
||||
expect(tag_rule.tags_match?(nil)).to be false
|
||||
end
|
||||
end
|
||||
|
||||
context "when the variant is not nil" do
|
||||
let(:variant_object) { { "tag_list" => ["member", "local", "volunteer"] } }
|
||||
|
||||
context "when the rule has no preferred variant tags specified" do
|
||||
it { expect(tag_rule.tags_match?(variant_object)).to be false }
|
||||
end
|
||||
|
||||
context "when the rule has preferred variant tags specified that match ANY variant tags" do
|
||||
let(:variant_tags) { "wholesale,some_tag,member" }
|
||||
|
||||
it { expect(tag_rule.tags_match?(variant_object)).to be true }
|
||||
end
|
||||
|
||||
context "when the rule has preferred variant tags specified that match NO variant tags" do
|
||||
let(:variant_tags) { "wholesale,some_tag,some_other_tag" }
|
||||
|
||||
it { expect(tag_rule.tags_match?(variant_object)).to be false }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#reject_matched?" do
|
||||
it "return false with default visibility (visible)" do
|
||||
expect(tag_rule.reject_matched?).to be false
|
||||
end
|
||||
|
||||
context "when visiblity is set to hidden" do
|
||||
let(:tag_rule) {
|
||||
build(:filter_variants_tag_rule, preferred_matched_variants_visibility: "hidden")
|
||||
}
|
||||
|
||||
it { expect(tag_rule.reject_matched?).to be true }
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user