Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/custom_wizard/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/custom_wizard/tag_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
95 changes: 71 additions & 24 deletions spec/components/custom_wizard/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions spec/components/custom_wizard/tag_search_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading