diff --git a/assets/javascripts/discourse/components/custom-wizard-tag-chooser.js.es6 b/assets/javascripts/discourse/components/custom-wizard-tag-chooser.js.es6 index 32a1fd6a..8d439aa4 100644 --- a/assets/javascripts/discourse/components/custom-wizard-tag-chooser.js.es6 +++ b/assets/javascripts/discourse/components/custom-wizard-tag-chooser.js.es6 @@ -4,7 +4,10 @@ export default TagChooser.extend({ searchTags(url, data, callback) { if (this.tagGroups) { let tagGroupsString = this.tagGroups.join(","); - data.tag_groups = tagGroupsString; + data.filterForInput = { + name: "custom-wizard-tag-chooser", + groups: tagGroupsString, + }; } return this._super(url, data, callback); diff --git a/lib/custom_wizard/extensions/discourse_tagging.rb b/lib/custom_wizard/extensions/discourse_tagging.rb index 3c81bbb3..701158bf 100644 --- a/lib/custom_wizard/extensions/discourse_tagging.rb +++ b/lib/custom_wizard/extensions/discourse_tagging.rb @@ -1,10 +1,9 @@ # frozen_string_literal: true -require 'request_store' module CustomWizardDiscourseTagging def filter_allowed_tags(guardian, opts = {}) - if tag_groups = ::RequestStore.store[:tag_groups] - tag_group_array = tag_groups.split(",") + if opts[:for_input].respond_to?(:dig) && (groups = opts.dig(:for_input, :groups)).present? + tag_group_array = groups.split(",") filtered_tags = TagGroup.includes(:tags).where(name: tag_group_array).map do |tag_group| tag_group.tags.pluck(:name) end.flatten diff --git a/lib/custom_wizard/extensions/tags_controller.rb b/lib/custom_wizard/extensions/tags_controller.rb deleted file mode 100644 index 0ddacb5f..00000000 --- a/lib/custom_wizard/extensions/tags_controller.rb +++ /dev/null @@ -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 diff --git a/plugin.rb b/plugin.rb index ccb08d6f..1bbd8f8e 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # 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 # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech @@ -90,7 +90,6 @@ after_initialize do ../lib/custom_wizard/extensions/extra_locales_controller.rb ../lib/custom_wizard/extensions/invites_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/custom_field/preloader.rb ../lib/custom_wizard/extensions/custom_field/serializer.rb @@ -233,7 +232,6 @@ after_initialize do end reloadable_patch do |plugin| - ::TagsController.prepend CustomWizardTagsController ::DiscourseTagging.singleton_class.prepend CustomWizardDiscourseTagging end diff --git a/spec/extensions/discourse_tagging_spec.rb b/spec/extensions/discourse_tagging_spec.rb new file mode 100644 index 00000000..14adaf5b --- /dev/null +++ b/spec/extensions/discourse_tagging_spec.rb @@ -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 diff --git a/spec/extensions/tags_controller_spec.rb b/spec/extensions/tags_controller_spec.rb deleted file mode 100644 index b3c1ccc8..00000000 --- a/spec/extensions/tags_controller_spec.rb +++ /dev/null @@ -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