FEATURE: new tags can be created from the "edit category" modal when defining the set of permitted tags
This commit is contained in:
parent
2c78bea5a0
commit
deb93044b4
|
@ -112,17 +112,25 @@ class TagsController < ::ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def search
|
def search
|
||||||
query = DiscourseTagging.filter_allowed_tags(
|
category = params[:categoryId] ? Category.find_by_id(params[:categoryId]) : nil
|
||||||
|
|
||||||
|
tags_with_counts = DiscourseTagging.filter_allowed_tags(
|
||||||
self.class.tags_by_count(guardian, params.slice(:limit)),
|
self.class.tags_by_count(guardian, params.slice(:limit)),
|
||||||
guardian,
|
guardian,
|
||||||
{
|
{ for_input: params[:filterForInput], term: params[:q], category: category }
|
||||||
for_input: params[:filterForInput],
|
|
||||||
term: params[:q],
|
|
||||||
category: params[:categoryId] ? Category.find_by_id(params[:categoryId]) : nil
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
tags = query.count.map {|t, c| { id: t, text: t, count: c } }
|
tags = tags_with_counts.count.map {|t, c| { id: t, text: t, count: c } }
|
||||||
|
|
||||||
|
unused_tags = DiscourseTagging.filter_allowed_tags(
|
||||||
|
Tag.where(topic_count: 0),
|
||||||
|
guardian,
|
||||||
|
{ for_input: params[:filterForInput], term: params[:q], category: category }
|
||||||
|
)
|
||||||
|
|
||||||
|
unused_tags.each do |t|
|
||||||
|
tags << { id: t.name, text: t.name, count: 0 }
|
||||||
|
end
|
||||||
|
|
||||||
render json: { results: tags }
|
render json: { results: tags }
|
||||||
end
|
end
|
||||||
|
|
|
@ -318,6 +318,12 @@ SQL
|
||||||
def allowed_tags=(tag_names)
|
def allowed_tags=(tag_names)
|
||||||
if self.tags.pluck(:name).sort != tag_names.sort
|
if self.tags.pluck(:name).sort != tag_names.sort
|
||||||
self.tags = Tag.where(name: tag_names).all
|
self.tags = Tag.where(name: tag_names).all
|
||||||
|
if self.tags.size < tag_names.size
|
||||||
|
new_tag_names = tag_names - self.tags.map(&:name)
|
||||||
|
new_tag_names.each do |name|
|
||||||
|
self.tags << Tag.create(name: name)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -51,5 +51,10 @@ describe "category tag restrictions" do
|
||||||
post = create_post(category: other_category, tags: [tag3.name, "newtag"])
|
post = create_post(category: other_category, tags: [tag3.name, "newtag"])
|
||||||
expect(post.topic.tags.map(&:name).sort).to eq([tag3.name, "newtag"].sort)
|
expect(post.topic.tags.map(&:name).sort).to eq([tag3.name, "newtag"].sort)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "can create tags when changing category settings" do
|
||||||
|
expect { other_category.update(allowed_tags: ['newtag']) }.to change { Tag.count }.by(1)
|
||||||
|
expect { other_category.update(allowed_tags: [tag1.name, 'tag-stuff', tag2.name, 'another-tag']) }.to change { Tag.count }.by(2)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue