From a931caffc1d27d9b142b30c011d0b5ad6fc9cd0f Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 7 Feb 2023 14:43:39 +0100 Subject: [PATCH] Remove request_store and use for_input --- .../custom-wizard-tag-chooser.js.es6 | 5 +- .../extensions/discourse_tagging.rb | 5 +- .../extensions/tags_controller.rb | 9 --- plugin.rb | 2 - spec/extensions/discourse_tagging_spec.rb | 67 +++++++++++++++++++ spec/extensions/tags_controller_spec.rb | 59 ---------------- 6 files changed, 73 insertions(+), 74 deletions(-) delete mode 100644 lib/custom_wizard/extensions/tags_controller.rb create mode 100644 spec/extensions/discourse_tagging_spec.rb delete mode 100644 spec/extensions/tags_controller_spec.rb 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 3d84d670..629e94b7 100644 --- a/plugin.rb +++ b/plugin.rb @@ -88,7 +88,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 @@ -231,7 +230,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 ad50a613..00000000 --- a/spec/extensions/tags_controller_spec.rb +++ /dev/null @@ -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