diff --git a/app/models/prompt.rb b/app/models/prompt.rb index cdae4bb6a4..9a2ed13cd4 100755 --- a/app/models/prompt.rb +++ b/app/models/prompt.rb @@ -175,6 +175,13 @@ def restricted_tags # tag_type is one of a set set so we know it is safe for constantize allowed_tags = tag_type.classify.constantize.with_parents(tag_set.fandom_taglist).canonical + + if restriction.send("#{tag_type}_restrict_to_tag_set") + allowed_tags = allowed_tags.where( + id: tag_set_associations.where(parent_tag_id: tag_set.fandom_taglist).select(:tag_id) + ) + end + disallowed_taglist = tag_set ? tag_set.send("#{tag_type}_taglist") - allowed_tags : [] # check for tag set associations diff --git a/spec/models/prompt_spec.rb b/spec/models/prompt_spec.rb index ab5254441b..a61ee88863 100644 --- a/spec/models/prompt_spec.rb +++ b/spec/models/prompt_spec.rb @@ -43,5 +43,68 @@ expect(prompt.errors[:base][0]).to include("not in the selected fandom") end end + + context "when restrict_to_fandom and restrict_to_tag_set are both enabled" do + let(:fandom_character) { create(:character, canonical: true) } + + before do + create(:common_tagging, filterable: fandom, common_tag: fandom_character) + create(:common_tagging, filterable: fandom, common_tag: non_fandom_character) + create(:tag_set_association, tag: fandom_character, parent_tag: fandom, owned_tag_set: owned_tag_set) + end + + let!(:challenge) do + create(:gift_exchange, + offer_restriction: create(:prompt_restriction, + character_restrict_to_fandom: true, + character_restrict_to_tag_set: true, + owned_tag_sets: [owned_tag_set])) + end + + it "marks the prompt as valid when the character is in the tag set" do + signup = build(:challenge_signup, collection: collection) + prompt = build(:offer, + tag_set: create(:tag_set, tags: [fandom, fandom_character]), + collection_id: collection.id, + challenge_signup: signup) + expect(prompt).to be_valid + end + + it "marks the prompt as invalid when the character is canonical in the fandom but not in the tag set" do + other_character = create(:character, canonical: true) + create(:common_tagging, filterable: fandom, common_tag: other_character) + signup = build(:challenge_signup, collection: collection) + prompt = build(:offer, + tag_set: create(:tag_set, tags: [fandom, other_character]), + collection_id: collection.id, + challenge_signup: signup) + expect(prompt).not_to be_valid + end + end + + context "when only restrict_to_fandom is enabled (not restrict_to_tag_set)" do + let(:fandom_character) { create(:character, canonical: true) } + + before do + create(:common_tagging, filterable: fandom, common_tag: fandom_character) + end + + let!(:challenge) do + create(:gift_exchange, + offer_restriction: create(:prompt_restriction, + character_restrict_to_fandom: true, + character_restrict_to_tag_set: false, + owned_tag_sets: [owned_tag_set])) + end + + it "marks the prompt as valid when the character is canonical in the fandom even if not in the tag set" do + signup = build(:challenge_signup, collection: collection) + prompt = build(:offer, + tag_set: create(:tag_set, tags: [fandom, fandom_character]), + collection_id: collection.id, + challenge_signup: signup) + expect(prompt).to be_valid + end + end end end