0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 17:30:29 +01:00

Merge pull request #219 from paviliondev/tag_group_restriction_fix

Tag group restriction fix
Dieser Commit ist enthalten in:
Richard Odekerken 2023-02-10 18:13:22 +01:00 committet von GitHub
Commit 643c5ecff0
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
6 geänderte Dateien mit 74 neuen und 62 gelöschten Zeilen

Datei anzeigen

@ -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);

Datei anzeigen

@ -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

Datei anzeigen

@ -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

Datei anzeigen

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
# name: discourse-custom-wizard # name: discourse-custom-wizard
# about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more.
# version: 2.2.0 # version: 2.2.1
# authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever
# url: https://github.com/paviliondev/discourse-custom-wizard # url: https://github.com/paviliondev/discourse-custom-wizard
# contact_emails: development@pavilion.tech # contact_emails: development@pavilion.tech
@ -90,7 +90,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
@ -233,7 +232,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

Datei anzeigen

@ -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

Datei anzeigen

@ -1,46 +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
end
end