Skip to content

Commit f0580d2

Browse files
authored
FEATURE: allow tags from tag field to be confined to a tag group (#175)
* FEATURE: allow tag field to be confined to a tag group * fixed linting * bump minor version * moved monkeypatch to a separate module * use snake case for variable names * added specs
1 parent 676d538 commit f0580d2

File tree

12 files changed

+123
-4
lines changed

12 files changed

+123
-4
lines changed

assets/javascripts/discourse/templates/components/wizard-custom-field.hbs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@
208208
</div>
209209
{{/if}}
210210

211+
{{#if isTag}}
212+
<div class="setting full field-mapper-setting">
213+
<div class="setting-label">
214+
<label>{{i18n "admin.wizard.field.tag_groups"}}</label>
215+
</div>
216+
217+
<div class="setting-value">
218+
{{tag-group-chooser
219+
tagGroups=field.tag_groups
220+
}}
221+
</div>
222+
</div>
223+
{{/if}}
224+
211225
{{#if showAdvanced}}
212226
{{wizard-advanced-toggle showAdvanced=field.showAdvanced}}
213227

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import TagChooser from "select-kit/components/tag-chooser";
2+
3+
export default TagChooser.extend({
4+
searchTags(url, data, callback) {
5+
if (this.tagGroups) {
6+
let tagGroupsString = this.tagGroups.join(",");
7+
data.tag_groups = tagGroupsString;
8+
}
9+
10+
return this._super(url, data, callback);
11+
},
12+
});
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
{{tag-chooser tags=field.value maximum=field.limit tabindex=field.tabindex everyTag=true}}
1+
{{wizard-tag-chooser
2+
tags=field.value
3+
maximum=field.limit
4+
tabindex=field.tabindex
5+
tagGroups=field.tag_groups
6+
everyTag=true
7+
}}

config/locales/client.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ en:
188188
property: "Property"
189189
prefill: "Prefill"
190190
content: "Content"
191+
tag_groups: "Tag Groups"
191192
date_time_format:
192193
label: "Format"
193194
instructions: "<a href='https://momentjs.com/docs/#/displaying/format/' target='_blank'>Moment.js format</a>"

controllers/custom_wizard/admin/wizard.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def save_wizard_params
117117
condition: mapped_params,
118118
index: mapped_params,
119119
validations: {},
120+
tag_groups: [],
120121
]
121122
],
122123
actions: [

extensions/discourse_tagging.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module CustomWizardDiscourseTagging
4+
def filter_allowed_tags(guardian, opts = {})
5+
if tag_groups = RequestStore.store[:tag_groups]
6+
tag_group_array = tag_groups.split(",")
7+
filtered_tags = TagGroup.includes(:tags).where(name: tag_group_array).map do |tag_group|
8+
tag_group.tags.pluck(:name)
9+
end.flatten
10+
11+
opts[:only_tag_names] ||= []
12+
opts[:only_tag_names].push(*filtered_tags)
13+
end
14+
15+
super
16+
end
17+
end

extensions/tags_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
module CustomWizardTagsController
4+
def search
5+
RequestStore.store[:tag_groups] = params[:tag_groups] if params[:tag_groups].present?
6+
super
7+
end
8+
end

lib/custom_wizard/builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def append_field(step, step_template, field_template, build_opts)
8686
required: field_template['required']
8787
}
8888

89-
%w(label description image key validations min_length max_length char_counter).each do |key|
89+
%w(label description image key validations min_length max_length char_counter tag_groups).each do |key|
9090
params[key.to_sym] = field_template[key] if field_template[key]
9191
end
9292

lib/custom_wizard/field.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class CustomWizard::Field
2121
:limit,
2222
:property,
2323
:content,
24+
:tag_groups,
2425
:preview_template,
2526
:placeholder
2627

@@ -46,6 +47,7 @@ def initialize(attrs)
4647
@limit = attrs[:limit]
4748
@property = attrs[:property]
4849
@content = attrs[:content]
50+
@tag_groups = attrs[:tag_groups]
4951
@preview_template = attrs[:preview_template]
5052
@placeholder = attrs[:placeholder]
5153
end
@@ -111,7 +113,8 @@ def self.types
111113
tag: {
112114
limit: nil,
113115
prefill: nil,
114-
content: nil
116+
content: nil,
117+
tag_groups: nil
115118
},
116119
category: {
117120
limit: 1,

plugin.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# frozen_string_literal: true
22
# name: discourse-custom-wizard
33
# about: Create custom wizards
4-
# version: 1.17.3
4+
# version: 1.18.0
55
# authors: Angus McLeod
66
# url: https://github.com/paviliondev/discourse-custom-wizard
77
# contact emails: angus@thepavilion.io
88

99
gem 'liquid', '5.0.1', require: true
10+
## ensure compatibility with category lockdown plugin
11+
gem 'request_store', '1.5.0', require: true
1012
register_asset 'stylesheets/common/wizard-admin.scss'
1113
register_asset 'stylesheets/common/wizard-mapper.scss'
1214

@@ -110,9 +112,11 @@ def process_require_tree_discourse_directive(path = ".")
110112
../extensions/invites_controller.rb
111113
../extensions/guardian.rb
112114
../extensions/users_controller.rb
115+
../extensions/tags_controller.rb
113116
../extensions/custom_field/preloader.rb
114117
../extensions/custom_field/serializer.rb
115118
../extensions/custom_field/extension.rb
119+
../extensions/discourse_tagging.rb
116120
].each do |path|
117121
load File.expand_path(path, __FILE__)
118122
end
@@ -249,5 +253,10 @@ def process_require_tree_discourse_directive(path = ".")
249253
"#{serializer_klass}_serializer".classify.constantize.prepend CustomWizardCustomFieldSerializer
250254
end
251255

256+
reloadable_patch do |plugin|
257+
::TagsController.prepend CustomWizardTagsController
258+
::DiscourseTagging.singleton_class.prepend CustomWizardDiscourseTagging
259+
end
260+
252261
DiscourseEvent.trigger(:custom_wizard_ready)
253262
end

0 commit comments

Comments
 (0)