FEATURE: new tags can be created from the "edit category" modal when defining the set of permitted tags

This commit is contained in:
Neil Lalonde 2016-05-31 17:26:00 -04:00
parent 2c78bea5a0
commit deb93044b4
3 changed files with 26 additions and 7 deletions

View File

@ -112,17 +112,25 @@ class TagsController < ::ApplicationController
end
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)),
guardian,
{
for_input: params[:filterForInput],
term: params[:q],
category: params[:categoryId] ? Category.find_by_id(params[:categoryId]) : nil
}
{ for_input: params[:filterForInput], term: params[:q], category: category }
)
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 }
end

View File

@ -318,6 +318,12 @@ SQL
def allowed_tags=(tag_names)
if self.tags.pluck(:name).sort != tag_names.sort
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

View File

@ -51,5 +51,10 @@ describe "category tag restrictions" do
post = create_post(category: other_category, tags: [tag3.name, "newtag"])
expect(post.topic.tags.map(&:name).sort).to eq([tag3.name, "newtag"].sort)
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