mirror of
https://github.com/discourse/discourse.git
synced 2026-03-04 01:15:08 +08:00
Currently in several endpoints, we return an array of strings for tags.
Our goal with this PR is to expand array tag name strings to an array of
tag objects.
#### before: Tags were returned as string arrays
```
{ "tags": ["support", "bug-report"] }
```
#### after: Tags are returned as object arrays
```
{ "tags": [{"id": 12, "name": "support", "slug": "support"}, {"id": 13, "name": "bug-report", "slug": "bug-report"}] }
```
This allows us to start referencing tags by their ids, and return more
information for a tag for future features.
This commit involves updating several areas:
- topic lists (/latest.json, /top.json, /c/:category/:id.json, etc, for
`top_tags`)
- tag chooser components (`MiniTagChooser`, `TagDrop`, etc)
- topic view (/t/:id.json)
- tag groups (/tag_groups.json, tags, parent_tag)
- category settings
- staff action logs
- synonyms
- ...
APIs that reference tags based on their names will still be supported
with a deprecation warning. Moving on, we will reference them using
their tag ids.
142 lines
4.1 KiB
Ruby
142 lines
4.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe TopicList do
|
|
let!(:topic) do
|
|
t = Fabricate(:topic)
|
|
t.allowed_user_ids = [t.user.id]
|
|
t
|
|
end
|
|
|
|
let(:user) { topic.user }
|
|
let(:topic_list) { TopicList.new("liked", user, [topic]) }
|
|
|
|
before { TopicList.preloaded_custom_fields.clear }
|
|
|
|
after { TopicList.preloaded_custom_fields.clear }
|
|
|
|
describe ".preloaded_custom_fields" do
|
|
it "should return a unique set of values" do
|
|
TopicList.preloaded_custom_fields << "test"
|
|
TopicList.preloaded_custom_fields << "test"
|
|
TopicList.preloaded_custom_fields << "apple"
|
|
|
|
expect(TopicList.preloaded_custom_fields).to eq(Set.new(%w[test apple]))
|
|
end
|
|
end
|
|
|
|
describe "preload" do
|
|
it "allows preloading of data" do
|
|
preloaded_topic = false
|
|
preloader =
|
|
lambda do |topics, topic_list|
|
|
expect(TopicList === topic_list).to eq(true)
|
|
expect(topics.length).to eq(1)
|
|
preloaded_topic = true
|
|
end
|
|
|
|
TopicList.on_preload(&preloader)
|
|
|
|
topic_list.topics
|
|
expect(preloaded_topic).to eq(true)
|
|
|
|
TopicList.cancel_preload(&preloader)
|
|
end
|
|
end
|
|
|
|
describe "#load_topics" do
|
|
it "loads additional data for serialization" do
|
|
category_user =
|
|
CategoryUser.create!(
|
|
user: user,
|
|
category: topic.category,
|
|
notification_level: NotificationLevels.all[:regular],
|
|
)
|
|
|
|
topic = topic_list.load_topics.first
|
|
|
|
expect(topic.category_user_data).to eq(category_user)
|
|
end
|
|
|
|
it "preloads first_post association" do
|
|
first_post = Fabricate(:post, topic: topic, post_number: 1)
|
|
topic.update!(first_post: first_post)
|
|
|
|
loaded_topic = topic_list.load_topics.first
|
|
|
|
expect(loaded_topic.association(:first_post).loaded?).to eq(true)
|
|
expect(loaded_topic.first_post).to eq(first_post)
|
|
end
|
|
end
|
|
|
|
describe "#top_tags" do
|
|
it "should return the right tags" do
|
|
tag = Fabricate(:tag, topics: [topic])
|
|
other_tag = Fabricate(:tag, topics: [topic], name: "use-anywhere")
|
|
expect(topic_list.top_tags).to eq(
|
|
[
|
|
{ id: tag.id, name: tag.name, slug: tag.slug },
|
|
{ id: other_tag.id, name: other_tag.name, slug: other_tag.slug },
|
|
],
|
|
)
|
|
end
|
|
|
|
describe "when there are tags restricted to a category" do
|
|
fab!(:category)
|
|
fab!(:topic) { Fabricate(:topic, category: category) }
|
|
fab!(:other_topic, :topic) # uncategorized
|
|
fab!(:tag) { Fabricate(:tag, topics: [topic], categories: [category], name: "category-tag") }
|
|
fab!(:other_tag) { Fabricate(:tag, topics: [topic], name: "use-anywhere") }
|
|
let(:topic_list) do
|
|
TopicList.new(
|
|
"latest",
|
|
topic.user,
|
|
[topic],
|
|
category: category.id,
|
|
category_id: category.id,
|
|
)
|
|
end
|
|
|
|
it "should return tags used in the category" do
|
|
expect(topic_list.top_tags).to eq(
|
|
[
|
|
{ id: tag.id, name: tag.name, slug: tag.slug },
|
|
{ id: other_tag.id, name: other_tag.name, slug: other_tag.slug },
|
|
],
|
|
)
|
|
end
|
|
|
|
it "with no category, should return all tags" do
|
|
expect(TopicList.new("latest", other_topic.user, [other_topic]).top_tags).to eq(
|
|
[
|
|
{ id: tag.id, name: tag.name, slug: tag.slug },
|
|
{ id: other_tag.id, name: other_tag.name, slug: other_tag.slug },
|
|
],
|
|
)
|
|
end
|
|
|
|
it "with another category with no tags, should return no tags" do
|
|
other_category = Fabricate(:category)
|
|
topic3 = Fabricate(:topic, category: other_category)
|
|
list =
|
|
TopicList.new(
|
|
"latest",
|
|
topic3.user,
|
|
[topic3],
|
|
category: other_category.id,
|
|
category_id: other_category.id,
|
|
)
|
|
expect(list.top_tags).to be_empty
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#preload_key" do
|
|
let(:category) { Fabricate(:category) }
|
|
let(:tag) { Fabricate(:tag) }
|
|
|
|
it "returns topic_list" do
|
|
topic_list = TopicList.new("latest", nil, nil, category: category, category_id: category.id)
|
|
expect(topic_list.preload_key).to eq("topic_list")
|
|
end
|
|
end
|
|
end
|