mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Adding tag rule logic for filtering products
This commit is contained in:
37
app/models/tag_rule/filter_products.rb
Normal file
37
app/models/tag_rule/filter_products.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
class TagRule::FilterProducts < TagRule
|
||||
preference :matched_variants_visibility, :string, default: "visible"
|
||||
preference :variant_tags, :string, default: ""
|
||||
|
||||
attr_accessible :preferred_matched_variants_visibility, :preferred_variant_tags
|
||||
|
||||
private
|
||||
|
||||
# Warning: this should only EVER be called via TagRule#apply
|
||||
def apply!
|
||||
unless preferred_matched_variants_visibility == "visible"
|
||||
subject.reject! do |product|
|
||||
product[:variants].reject!{ |v| tags_match?(v) }
|
||||
product[:variants].empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def apply_default!
|
||||
if preferred_matched_variants_visibility == "visible"
|
||||
subject.reject! do |product|
|
||||
product[:variants].reject!{ |v| tags_match?(v) }
|
||||
product[:variants].empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def tags_match?(variant)
|
||||
variant_tags = variant.andand[:tag_list] || []
|
||||
preferred_tags = preferred_variant_tags.split(",")
|
||||
( variant_tags & preferred_tags ).any?
|
||||
end
|
||||
|
||||
def subject_class_matches?
|
||||
subject.class == HashWithIndifferentAccess
|
||||
end
|
||||
end
|
||||
@@ -290,6 +290,10 @@ FactoryGirl.define do
|
||||
enterprise { FactoryGirl.create :distributor_enterprise }
|
||||
end
|
||||
|
||||
factory :filter_products_tag_rule, class: TagRule::FilterProducts do
|
||||
enterprise { FactoryGirl.create :distributor_enterprise }
|
||||
end
|
||||
|
||||
factory :tag_rule, class: TagRule::DiscountOrder do
|
||||
enterprise { FactoryGirl.create :distributor_enterprise }
|
||||
before(:create) do |tr|
|
||||
|
||||
83
spec/models/tag_rule/filter_products_spec.rb
Normal file
83
spec/models/tag_rule/filter_products_spec.rb
Normal file
@@ -0,0 +1,83 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe TagRule::FilterProducts, type: :model do
|
||||
let!(:tag_rule) { create(:filter_products_tag_rule) }
|
||||
|
||||
describe "determining whether tags match for a given variant" do
|
||||
context "when the variantm is nil" do
|
||||
|
||||
it "returns false" do
|
||||
expect(tag_rule.send(: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
|
||||
before { allow(tag_rule).to receive(:preferred_variant_tags) { "" } }
|
||||
it { expect(tag_rule.send(:tags_match?, variant_object)).to be false }
|
||||
end
|
||||
|
||||
context "when the rule has preferred variant tags specified that match ANY of the variant tags" do
|
||||
before { allow(tag_rule).to receive(:preferred_variant_tags) { "wholesale,some_tag,member" } }
|
||||
it { expect(tag_rule.send(:tags_match?, variant_object)).to be true }
|
||||
end
|
||||
|
||||
context "when the rule has preferred variant tags specified that match NONE of the variant tags" do
|
||||
before { allow(tag_rule).to receive(:preferred_variant_tags) { "wholesale,some_tag,some_other_tag" } }
|
||||
it { expect(tag_rule.send(:tags_match?, variant_object)).to be false }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "applying the rule" do
|
||||
# Assume that all validation is done by the TagRule base class
|
||||
|
||||
let(:product1) { { name: "product1", variants: [{ name: "v1", tag_list: ["tag1", "something", "somethingelse"]}] } }
|
||||
let(:product2) { { name: "product2", variants: [{ name: "v2", tag_list: ["tag2"]}] } }
|
||||
let(:product3) { { name: "product3", variants: [{ name: "v3", tag_list: ["tag3"]}] } }
|
||||
let!(:product_hash) { [product1, product2, product3] }
|
||||
|
||||
before do
|
||||
tag_rule.update_attribute(:preferred_variant_tags, "tag2")
|
||||
tag_rule.set_context(product_hash, nil)
|
||||
end
|
||||
|
||||
context "apply!" do
|
||||
context "when showing matching variants" do
|
||||
before { tag_rule.update_attribute(:preferred_matched_variants_visibility, "visible") }
|
||||
it "does nothing" do
|
||||
tag_rule.send(:apply!)
|
||||
expect(product_hash).to eq [product1, product2, product3]
|
||||
end
|
||||
end
|
||||
|
||||
context "when hiding matching variants" do
|
||||
before { tag_rule.update_attribute(:preferred_matched_variants_visibility, "hidden") }
|
||||
it "removes matching variants from the list" do
|
||||
tag_rule.send(:apply!)
|
||||
expect(product_hash).to eq [product1, product3]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "apply_default!" do
|
||||
context "when showing matching variants" do
|
||||
before { tag_rule.update_attribute(:preferred_matched_variants_visibility, "visible") }
|
||||
it "remove matching variants from the list" do
|
||||
tag_rule.send(:apply_default!)
|
||||
expect(product_hash).to eq [product1, product3]
|
||||
end
|
||||
end
|
||||
|
||||
context "when hiding matching variants" do
|
||||
before { tag_rule.update_attribute(:preferred_matched_variants_visibility, "hidden") }
|
||||
it "does nothing" do
|
||||
tag_rule.send(:apply_default!)
|
||||
expect(product_hash).to eq [product1, product2, product3]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,6 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe TagRule::DiscountOrder, type: :model do
|
||||
describe TagRule::FilterShippingMethods, type: :model do
|
||||
let!(:tag_rule) { create(:filter_shipping_methods_tag_rule) }
|
||||
|
||||
describe "determining whether tags match for a given shipping method" do
|
||||
|
||||
Reference in New Issue
Block a user