diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 5b31d148e..944965399 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -416,9 +416,16 @@ def action_category end def action_tags - output = CustomWizard::Mapper.new(inputs: action["tags"], data: mapper_data, user: user).perform - - return false if output.blank? + inputs = action["tags"] + output = + CustomWizard::Mapper.new( + inputs: inputs, + data: mapper_data, + user: user, + opts: { + multiple: inputs&.all? { |input| input["type"] == "assignment" }, + }, + ).perform CustomWizard::TagSearch.tag_names(output).presence || false end diff --git a/lib/custom_wizard/tag_search.rb b/lib/custom_wizard/tag_search.rb index 6b7aa9003..ac1ffcab5 100644 --- a/lib/custom_wizard/tag_search.rb +++ b/lib/custom_wizard/tag_search.rb @@ -16,7 +16,7 @@ def self.tag_names(value) Array .wrap(value) .flatten - .map do |tag| + .filter_map do |tag| tag.is_a?(Hash) || tag.is_a?(ActionController::Parameters) ? tag[:name] || tag["name"] : tag end .flat_map { |tag| tag.to_s.split(",").map(&:strip) } diff --git a/plugin.rb b/plugin.rb index 820ab5163..32c504a9b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.18.8 +# version: 2.18.9 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index 2d69e29a9..a4b13ed46 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -109,40 +109,87 @@ def complete_wizard(body: "topic body", **last_step_fields) expect(topic.category_id).to eq(SiteSetting.uncategorized_category_id) end - it "adds tags mapped to tag objects" do - wizard_template[:actions][0]["tags"] = [ + context "with tags" do + fab!(:tag2) { Fabricate(:tag, name: "tag2") } + + let(:tag_field_input) do { type: "assignment", output_type: "wizard_field", output_connector: "set", output: "step_3_field_2", - }, - ] - wizard_template[:steps][2]["fields"] << { "id" => "step_3_field_2", "type" => "tag" } - update_template(wizard_template) + } + end - complete_wizard(step_3_field_2: [{ id: tag.id, name: tag.name }]) + def text_input(output) + { type: "assignment", output_type: "text", output_connector: "set", output: output } + end - topic = Topic.find_by(title: "Topic Title") - expect(topic.tags.pluck(:name)).to eq([tag.name]) - end + def set_tag_inputs(*inputs) + wizard_template[:actions][0]["tags"] = inputs + wizard_template[:steps][2]["fields"] << { "id" => "step_3_field_2", "type" => "tag" } + update_template(wizard_template) + end - it "adds comma separated tags from a text input" do - tag2 = Fabricate(:tag, name: "tag2") - wizard_template[:actions][0]["tags"] = [ - { - type: "assignment", - output_type: "text", - output_connector: "set", - output: "#{tag.name}, #{tag2.name},#{tag.name}", - }, - ] - update_template(wizard_template) + def topic_tags + Topic.find_by(title: "Topic Title").tags.pluck(:name) + end - complete_wizard + it "adds tags mapped to tag objects" do + set_tag_inputs(tag_field_input) - topic = Topic.find_by(title: "Topic Title") - expect(topic.tags.pluck(:name)).to contain_exactly(tag.name, tag2.name) + complete_wizard(step_3_field_2: [{ id: tag.id, name: tag.name }]) + + expect(topic_tags).to eq([tag.name]) + end + + it "adds comma separated tags from a text input" do + set_tag_inputs(text_input("#{tag.name}, #{tag2.name},#{tag.name}")) + + complete_wizard + + expect(topic_tags).to contain_exactly(tag.name, tag2.name) + end + + it "adds the tags of every input" do + set_tag_inputs(tag_field_input, text_input(tag2.name)) + + complete_wizard(step_3_field_2: [tag.name]) + + expect(topic_tags).to contain_exactly(tag.name, tag2.name) + end + + it "adds the fixed tag when the tag field is empty" do + set_tag_inputs(tag_field_input, text_input(tag2.name)) + + complete_wizard + + expect(topic_tags).to eq([tag2.name]) + end + + it "uses only the first matching input when an input is conditional" do + set_tag_inputs( + text_input(tag.name).merge( + type: "conditional", + output_connector: "then", + pairs: [ + { + index: 0, + key: "name", + key_type: "user_field", + value: "Angus", + value_type: "text", + connector: "equal", + }, + ], + ), + text_input(tag2.name), + ) + + complete_wizard + + expect(topic_tags).to eq([tag.name]) + end end it "queues the topic when it needs approval and keeps wizard data on approval" do diff --git a/spec/components/custom_wizard/tag_search_spec.rb b/spec/components/custom_wizard/tag_search_spec.rb new file mode 100644 index 000000000..20bd3a3f6 --- /dev/null +++ b/spec/components/custom_wizard/tag_search_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +describe CustomWizard::TagSearch do + describe ".tag_names" do + it "ignores unset values" do + expect(described_class.tag_names([false, nil, "", "tag1", { "name" => "tag2" }])).to eq( + %w[tag1 tag2], + ) + end + end +end