Skip to content

Commit

Permalink
respect tags_sort_alphabetically setting (#19)
Browse files Browse the repository at this point in the history
* respect tags_sort_alphabetically setting

* Fixed linting error

Offenses:

app/controllers/discourse_preset_topic_composer/preset_tag_groups_controller.rb:17:71: W: [Correctable] Lint/SafeNavigationChain: Do not chain ordinary method call after safe navigation operator.
      tags = TagGroup.visible(guardian).find_by(name: tag_group)&.tags.order(tag_order) || []
                                                                      ^^^^^^^^^^^^^^^^^

* DEV: Add tests to ensure that the tags are sorted alphabetically if settings enabled

* DEV: lint preset_topic_composer_spec.rb

* DEV: change to use ArraySerializer in controller

* Update app/controllers/discourse_preset_topic_composer/preset_tag_groups_controller.rb

Co-authored-by: Gabriel Grubba <[email protected]>

---------

Co-authored-by: Gabriel Grubba <[email protected]>
Co-authored-by: Gabriel Grubba <[email protected]>
  • Loading branch information
3 people committed Jul 2, 2024
1 parent 44db5f8 commit 4c49bcd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ class PresetTagGroupsController < ::ApplicationController
def search_tags_by_tag_group
tag_group = params[:tag_group]
tag_group = CGI.unescape(tag_group)
tags = TagGroup.visible(guardian).find_by(name: tag_group)&.tags || []
render json: { tags: tags }

tag_order =
if SiteSetting.tags_sort_alphabetically
"name ASC"
else
"public_topic_count DESC"
end

tags = TagGroup.visible(guardian).find_by(name: tag_group)&.tags&.order(tag_order) || []
render json: {
tags: ActiveModel::ArraySerializer.new(tags, each_serializer: TagSerializer).as_json,
}
end
end
end
5 changes: 5 additions & 0 deletions spec/system/page_objects/components/preset_composer_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def get_last_label
input.last.find(".name").text
end

def get_first_list_options
input.first.click
input.first.find(".select-kit-collection").find_all("li")
end

def input
find(".tag-group_wrapper").find_all(".select-kit.combobox.combo-box")
end
Expand Down
19 changes: 19 additions & 0 deletions spec/system/preset_topic_composer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,24 @@ def self.add_new_json(json)
button = find("li[title='New Question3']")
expect(button[:class]).to include("is-highlighted")
end

it "should sort alphabetically if SiteSetting is enabled" do
SiteSetting.tags_sort_alphabetically = true
Fabricate(:topic, tags: [tag_synonym_for_tag1])
visit "/"

preset_dropdown = PageObjects::Components::PresetTopicDropdown.new
preset_dropdown.select("New Question2")
preset_input = PageObjects::Components::PresetComposerInput.new
expect(preset_input.get_first_list_options.first.text).to eq(tag1.name)

SiteSetting.tags_sort_alphabetically = false
visit "/"

preset_dropdown = PageObjects::Components::PresetTopicDropdown.new
preset_dropdown.select("New Question2")
preset_input = PageObjects::Components::PresetComposerInput.new
expect(preset_input.get_first_list_options.first.text).to eq(tag_synonym_for_tag1.name)
end
end
end

0 comments on commit 4c49bcd

Please sign in to comment.