Remove request_store and use for_input
Dieser Commit ist enthalten in:
Ursprung
735d43e1ee
Commit
a931caffc1
6 geänderte Dateien mit 73 neuen und 74 gelöschten Zeilen
|
@ -4,7 +4,10 @@ export default TagChooser.extend({
|
||||||
searchTags(url, data, callback) {
|
searchTags(url, data, callback) {
|
||||||
if (this.tagGroups) {
|
if (this.tagGroups) {
|
||||||
let tagGroupsString = this.tagGroups.join(",");
|
let tagGroupsString = this.tagGroups.join(",");
|
||||||
data.tag_groups = tagGroupsString;
|
data.filterForInput = {
|
||||||
|
name: "custom-wizard-tag-chooser",
|
||||||
|
groups: tagGroupsString,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._super(url, data, callback);
|
return this._super(url, data, callback);
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
require 'request_store'
|
|
||||||
|
|
||||||
module CustomWizardDiscourseTagging
|
module CustomWizardDiscourseTagging
|
||||||
def filter_allowed_tags(guardian, opts = {})
|
def filter_allowed_tags(guardian, opts = {})
|
||||||
if tag_groups = ::RequestStore.store[:tag_groups]
|
if opts[:for_input].respond_to?(:dig) && (groups = opts.dig(:for_input, :groups)).present?
|
||||||
tag_group_array = tag_groups.split(",")
|
tag_group_array = groups.split(",")
|
||||||
filtered_tags = TagGroup.includes(:tags).where(name: tag_group_array).map do |tag_group|
|
filtered_tags = TagGroup.includes(:tags).where(name: tag_group_array).map do |tag_group|
|
||||||
tag_group.tags.pluck(:name)
|
tag_group.tags.pluck(:name)
|
||||||
end.flatten
|
end.flatten
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
require 'request_store'
|
|
||||||
|
|
||||||
module CustomWizardTagsController
|
|
||||||
def search
|
|
||||||
::RequestStore.store[:tag_groups] = params[:tag_groups] if params[:tag_groups].present?
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -88,7 +88,6 @@ after_initialize do
|
||||||
../lib/custom_wizard/extensions/extra_locales_controller.rb
|
../lib/custom_wizard/extensions/extra_locales_controller.rb
|
||||||
../lib/custom_wizard/extensions/invites_controller.rb
|
../lib/custom_wizard/extensions/invites_controller.rb
|
||||||
../lib/custom_wizard/extensions/users_controller.rb
|
../lib/custom_wizard/extensions/users_controller.rb
|
||||||
../lib/custom_wizard/extensions/tags_controller.rb
|
|
||||||
../lib/custom_wizard/extensions/guardian.rb
|
../lib/custom_wizard/extensions/guardian.rb
|
||||||
../lib/custom_wizard/extensions/custom_field/preloader.rb
|
../lib/custom_wizard/extensions/custom_field/preloader.rb
|
||||||
../lib/custom_wizard/extensions/custom_field/serializer.rb
|
../lib/custom_wizard/extensions/custom_field/serializer.rb
|
||||||
|
@ -231,7 +230,6 @@ after_initialize do
|
||||||
end
|
end
|
||||||
|
|
||||||
reloadable_patch do |plugin|
|
reloadable_patch do |plugin|
|
||||||
::TagsController.prepend CustomWizardTagsController
|
|
||||||
::DiscourseTagging.singleton_class.prepend CustomWizardDiscourseTagging
|
::DiscourseTagging.singleton_class.prepend CustomWizardDiscourseTagging
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
67
spec/extensions/discourse_tagging_spec.rb
Normale Datei
67
spec/extensions/discourse_tagging_spec.rb
Normale Datei
|
@ -0,0 +1,67 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
describe ::DiscourseTagging, type: :request do
|
||||||
|
fab!(:user) { Fabricate(:user) }
|
||||||
|
fab!(:tag_1) { Fabricate(:tag, name: "Angus") }
|
||||||
|
fab!(:tag_2) { Fabricate(:tag, name: "Faizaan") }
|
||||||
|
fab!(:tag_3) { Fabricate(:tag, name: "Robert") }
|
||||||
|
fab!(:tag_4) { Fabricate(:tag, name: "Eli") }
|
||||||
|
fab!(:tag_5) { Fabricate(:tag, name: "Jeff") }
|
||||||
|
|
||||||
|
fab!(:tag_group_1) { Fabricate(:tag_group, tags: [tag_1, tag_2]) }
|
||||||
|
fab!(:tag_group_2) { Fabricate(:tag_group, tags: [tag_3, tag_4]) }
|
||||||
|
|
||||||
|
describe "#filter_allowed_tags" do
|
||||||
|
let(:guardian) { Guardian.new(user) }
|
||||||
|
|
||||||
|
context "for_input is a boolean" do
|
||||||
|
it "works normally" do
|
||||||
|
filter_params = {
|
||||||
|
q: '',
|
||||||
|
for_input: true
|
||||||
|
}
|
||||||
|
tags = DiscourseTagging.filter_allowed_tags(guardian, filter_params)
|
||||||
|
names = tags.map(&:name)
|
||||||
|
all_tag_names = Tag.all.pluck(:name)
|
||||||
|
expect(names).to contain_exactly(*all_tag_names)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "for_input is an object including a tag group" do
|
||||||
|
it "returns tags only in the tag group" do
|
||||||
|
filter_params = {
|
||||||
|
q: "",
|
||||||
|
for_input: {
|
||||||
|
name: "custom-wizard-tag-chooser",
|
||||||
|
groups: tag_group_1.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tags = DiscourseTagging.filter_allowed_tags(guardian, filter_params)
|
||||||
|
names = tags.map(&:name)
|
||||||
|
expected_tag_names = TagGroup
|
||||||
|
.includes(:tags)
|
||||||
|
.where(id: tag_group_1.id)
|
||||||
|
.map { |tag_group| tag_group.tags.pluck(:name) }.flatten
|
||||||
|
|
||||||
|
expect(names).to contain_exactly(*expected_tag_names)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "for_input is an object including an empty tag group string" do
|
||||||
|
it "returns all tags" do
|
||||||
|
filter_params = {
|
||||||
|
q: "",
|
||||||
|
for_input: {
|
||||||
|
name: "custom-wizard-tag-chooser",
|
||||||
|
groups: ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tags = DiscourseTagging.filter_allowed_tags(guardian, filter_params)
|
||||||
|
names = tags.map(&:name)
|
||||||
|
|
||||||
|
all_tag_names = Tag.all.pluck(:name)
|
||||||
|
expect(names).to contain_exactly(*all_tag_names)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,59 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
describe ::TagsController, type: :request do
|
|
||||||
fab!(:tag_1) { Fabricate(:tag, name: "Angus") }
|
|
||||||
fab!(:tag_2) { Fabricate(:tag, name: "Faizaan") }
|
|
||||||
fab!(:tag_3) { Fabricate(:tag, name: "Robert") }
|
|
||||||
fab!(:tag_4) { Fabricate(:tag, name: "Eli") }
|
|
||||||
fab!(:tag_5) { Fabricate(:tag, name: "Jeff") }
|
|
||||||
|
|
||||||
fab!(:tag_group_1) { Fabricate(:tag_group, tags: [tag_1, tag_2]) }
|
|
||||||
fab!(:tag_group_2) { Fabricate(:tag_group, tags: [tag_3, tag_4]) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
::RequestStore.store[:tag_groups] = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#search" do
|
|
||||||
context "tag group param present" do
|
|
||||||
it "returns tags only in the tag group" do
|
|
||||||
get "/tags/filter/search.json", params: { q: '', tag_groups: [tag_group_1.name, tag_group_2.name] }
|
|
||||||
expect(response.status).to eq(200)
|
|
||||||
results = response.parsed_body['results']
|
|
||||||
names = results.map { |result| result['name'] }
|
|
||||||
|
|
||||||
expected_tag_names = TagGroup
|
|
||||||
.includes(:tags)
|
|
||||||
.where(id: [tag_group_1.id, tag_group_2.id])
|
|
||||||
.map { |tag_group| tag_group.tags.pluck(:name) }.flatten
|
|
||||||
|
|
||||||
expect(names).to contain_exactly(*expected_tag_names)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "tag group param not present" do
|
|
||||||
it "returns all tags" do
|
|
||||||
get "/tags/filter/search.json", params: { q: '' }
|
|
||||||
expect(response.status).to eq(200)
|
|
||||||
results = response.parsed_body['results']
|
|
||||||
names = results.map { |result| result['name'] }
|
|
||||||
|
|
||||||
all_tag_names = Tag.all.pluck(:name)
|
|
||||||
expect(names).to contain_exactly(*all_tag_names)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "tag group param is stored as an empty string" do
|
|
||||||
it "returns all tags" do
|
|
||||||
::RequestStore.store[:tag_groups] = ""
|
|
||||||
get "/tags/filter/search.json", params: { q: '' }
|
|
||||||
expect(response.status).to eq(200)
|
|
||||||
results = response.parsed_body['results']
|
|
||||||
names = results.map { |result| result['name'] }
|
|
||||||
|
|
||||||
all_tag_names = Tag.all.pluck(:name)
|
|
||||||
expect(names).to contain_exactly(*all_tag_names)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
Laden …
In neuem Issue referenzieren