From 8b8da33ffcda77b5b00334b55787482c0861b9fe Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 21 Apr 2016 14:12:06 +1000 Subject: [PATCH] Further refinements to FilterProducts tag rule --- app/models/tag_rule/filter_products.rb | 12 ++++++------ spec/models/tag_rule/filter_products_spec.rb | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/models/tag_rule/filter_products.rb b/app/models/tag_rule/filter_products.rb index e087d8737a..f9cba66d62 100644 --- a/app/models/tag_rule/filter_products.rb +++ b/app/models/tag_rule/filter_products.rb @@ -10,8 +10,8 @@ class TagRule::FilterProducts < TagRule def apply! unless preferred_matched_variants_visibility == "visible" subject.reject! do |product| - product[:variants].reject!{ |v| tags_match?(v) } - product[:variants].empty? + product["variants"].reject!{ |v| tags_match?(v) } + product["variants"].empty? end end end @@ -19,19 +19,19 @@ class TagRule::FilterProducts < TagRule def apply_default! if preferred_matched_variants_visibility == "visible" subject.reject! do |product| - product[:variants].reject!{ |v| tags_match?(v) } - product[:variants].empty? + product["variants"].reject!{ |v| tags_match?(v) } + product["variants"].empty? end end end def tags_match?(variant) - variant_tags = variant.andand[:tag_list] || [] + 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 + subject.class == Array end end diff --git a/spec/models/tag_rule/filter_products_spec.rb b/spec/models/tag_rule/filter_products_spec.rb index 2f1d9ec495..3cc47ea127 100644 --- a/spec/models/tag_rule/filter_products_spec.rb +++ b/spec/models/tag_rule/filter_products_spec.rb @@ -4,7 +4,7 @@ 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 + context "when the variant is nil" do it "returns false" do expect(tag_rule.send(:tags_match?, nil)).to be false @@ -12,7 +12,7 @@ describe TagRule::FilterProducts, type: :model do end context "when the variant is not nil" do - let(:variant_object) { { tag_list: ["member","local","volunteer"] } } + 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) { "" } } @@ -34,14 +34,14 @@ describe TagRule::FilterProducts, type: :model do 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(: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) + tag_rule.set_context({subject: product_hash}) end context "apply!" do