Skip to content

A platform for community discussion. Free, open, simple.

UX: Order Form Template TagChooser choices alphabetically (#33109)

Juan David Martínez Cubillosee6b59344f40
diff --git a/app/controllers/form_templates_controller.rb b/app/controllers/form_templates_controller.rb
index fbe6e8538d5..48ec8729e68 100644
--- a/app/controllers/form_templates_controller.rb
+++ b/app/controllers/form_templates_controller.rb
@@ -44,9 +44,15 @@ class FormTemplatesController < ApplicationController
tag_group_name = form_field["tag_group"]
tags = tag_groups[tag_group_name].tags.reject { |tag| tag.target_tag_id.present? }
-
ordered_field = {}
+ tags =
+ tags.sort_by do |t|
+ # Transform the name to the displayed value before ordering
+ display = t.description.presence || t.name.tr("-", " ").upcase
+ display
+ end
+
form_field.each do |key, value|
ordered_field[key] = value
diff --git a/spec/requests/form_templates_controller_spec.rb b/spec/requests/form_templates_controller_spec.rb
index 87c433e81ea..3851d09d885 100644
--- a/spec/requests/form_templates_controller_spec.rb
+++ b/spec/requests/form_templates_controller_spec.rb
@@ -65,13 +65,13 @@ RSpec.describe FormTemplatesController do
context "when using tag groups in a form template" do
fab!(:admin)
- fab!(:tag1) { Fabricate(:tag, description: "Tag 1 custom Translation") }
- fab!(:tag2) { Fabricate(:tag, description: "Tag 2 custom Translation") }
+ fab!(:tag1) { Fabricate(:tag, description: "A Tag 1 custom Translation") }
+ fab!(:tag2) { Fabricate(:tag, description: "A Tag 2 custom Translation") }
fab!(:tag3) { Fabricate(:tag) }
fab!(:tag4) { Fabricate(:tag) }
fab!(:tag_synonym) { Fabricate(:tag, name: "discourse") }
- fab!(:tag_group1) { Fabricate(:tag_group, name: "tag_group1", tags: [tag1, tag3]) }
+ fab!(:tag_group1) { Fabricate(:tag_group, name: "tag_group1", tags: [tag3, tag1]) }
fab!(:tag_group2) { Fabricate(:tag_group, name: "tag_group2", tags: [tag2, tag4]) }
fab!(:tag_groups_form_template) do
@@ -137,6 +137,20 @@ RSpec.describe FormTemplatesController do
expect(parsed_template[0]["choices"]).to eq([tag1.name, tag3.name])
expect(parsed_template[1]["choices"]).to eq([tag2.name, tag4.name])
end
+
+ it "should return a single template with the correct data in order" do
+ new_tag = Fabricate(:tag, description: "Custom Tag")
+ tag_group1.tags = [tag3, tag1, new_tag]
+ tag_group1.save
+
+ get "/form-templates/#{tag_groups_form_template.id}.json"
+ expect(response.status).to eq(200)
+ json = response.parsed_body
+
+ current_template = json["form_template"]
+ parsed_template = YAML.safe_load(current_template["template"])
+ expect(parsed_template[0]["choices"]).to eq([tag1.name, new_tag.name, tag3.name])
+ end
end
end