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

Add dropdown tests and improvements

Dieser Commit ist enthalten in:
Angus McLeod 2019-12-12 10:20:10 +11:00
Ursprung c182435805
Commit 6ad44d58ad
2 geänderte Dateien mit 65 neuen und 41 gelöschten Zeilen

Datei anzeigen

@ -267,43 +267,50 @@ class CustomWizard::Builder
end end
end end
def build_dropdown_list(field, field_template) def build_dropdown_list(field, template)
field.dropdown_none = field_template['dropdown_none'] if field_template['dropdown_none'] field.dropdown_none = template['dropdown_none'] if template['dropdown_none']
self.send("build_dropdown_#{template['choices_type']}", field, template)
end
if field_template['choices'] && field_template['choices'].length > 0 def build_dropdown_custom(field, template)
field_template['choices'].each do |c| template['choices'].each do |c|
field.add_choice(c['key'], label: c['value']) field.add_choice(c['key'], label: c['value'])
end end
elsif field_template['choices_key'] && field_template['choices_key'].length > 0 end
choices = I18n.t(field_template['choices_key'])
def build_dropdown_translation(field, template)
choices = I18n.t(template['choices_key'])
if choices.is_a?(Hash) if choices.is_a?(Hash)
choices.each { |k, v| field.add_choice(k, label: v) } choices.each { |k, v| field.add_choice(k, label: v) }
end end
elsif field_template['choices_preset'] && field_template['choices_preset'].length > 0 end
def build_dropdown_preset(field, template)
objects = [] objects = []
guardian = Guardian.new(@wizard.user) guardian = Guardian.new(@wizard.user)
site = Site.new(guardian) site = Site.new(guardian)
if field_template['choices_preset'] === 'categories' case template['choices_preset']
when 'categories'
objects = site.categories objects = site.categories
end when 'groups'
if field_template['choices_preset'] === 'groups'
objects = site.groups objects = site.groups
when 'tags'
objects = Tag.top_tags(guardian: guardian).map do |tag|
TagStruct.new(tag,tag)
end
else
# do nothing
end end
if field_template['choices_preset'] === 'tags' if template['choices_filters'] && template['choices_filters'].length > 0
objects = Tag.top_tags(guardian: guardian).map { |tag| TagStruct.new(tag,tag) } template['choices_filters'].each do |f|
end
if field_template['choices_filters'] && field_template['choices_filters'].length > 0
field_template['choices_filters'].each do |f|
objects.reject! do |o| objects.reject! do |o|
if f['key'].include? 'custom_fields' if f['key'].include? 'custom_fields'
o.custom_fields[f['key'].split('.')[1]].to_s != f['value'].to_s o.custom_fields[f['key'].split('.')[1]].to_s != f['value'].to_s
else else
o[prop].to_s != f['value'].to_s o[f['key']].to_s != f['value'].to_s
end end
end end
end end
@ -315,7 +322,6 @@ class CustomWizard::Builder
end end
end end
end end
end
def validate_field(field, updater, step_template) def validate_field(field, updater, step_template)
value = updater.fields[field['id']] value = updater.fields[field['id']]

Datei anzeigen

@ -5,6 +5,8 @@ require 'rails_helper'
describe CustomWizard::Builder do describe CustomWizard::Builder do
fab!(:user) { Fabricate(:user, username: 'angus') } fab!(:user) { Fabricate(:user, username: 'angus') }
fab!(:trusted_user) { Fabricate(:user, trust_level: 3) } fab!(:trusted_user) { Fabricate(:user, trust_level: 3) }
fab!(:category1) { Fabricate(:category, name: 'cat1') }
fab!(:category2) { Fabricate(:category, name: 'cat2') }
fab!(:group) { Fabricate(:group) } fab!(:group) { Fabricate(:group) }
let!(:template) do let!(:template) do
@ -31,7 +33,7 @@ describe CustomWizard::Builder do
let(:dropdown_tags_field) {{"id": "dropdown_tags","type": "dropdown","choices_type": "preset","choices_preset": "tags","label": "Dropdown Tags"}} let(:dropdown_tags_field) {{"id": "dropdown_tags","type": "dropdown","choices_type": "preset","choices_preset": "tags","label": "Dropdown Tags"}}
let(:dropdown_custom_field) {{"id": "dropdown_custom","type": "dropdown","choices_type": "custom","choices": [{"key": "option_1","value": "Option 1"},{"key": "option_2","value": "Option 2"}]}} let(:dropdown_custom_field) {{"id": "dropdown_custom","type": "dropdown","choices_type": "custom","choices": [{"key": "option_1","value": "Option 1"},{"key": "option_2","value": "Option 2"}]}}
let(:dropdown_translation_field) {{"id": "dropdown_translation","type": "dropdown","choices_type": "translation","choices_key": "key1.key2"}} let(:dropdown_translation_field) {{"id": "dropdown_translation","type": "dropdown","choices_type": "translation","choices_key": "key1.key2"}}
let(:dropdown_categories_filtered_field) {{"id": "dropdown_categories_filtered_field","type": "dropdown","choices_type": "preset","choices_preset": "categories","choices_filters": [{"key": "slug","value": "staff"}]}} let(:dropdown_categories_filtered_field) {{"id": "dropdown_categories_filtered_field","type": "dropdown","choices_type": "preset","choices_preset": "categories","choices_filters": [{"key": "slug","value": "cat2"}]}}
let(:create_topic_action) {{"id":"create_topic","type":"create_topic","title":"text","post":"textarea"}} let(:create_topic_action) {{"id":"create_topic","type":"create_topic","title":"text","post":"textarea"}}
let(:send_message_action) {{"id":"send_message","type":"send_message","title":"text","post":"textarea","username":"angus"}} let(:send_message_action) {{"id":"send_message","type":"send_message","title":"text","post":"textarea","username":"angus"}}
let(:route_to_action) {{"id":"route_to","type":"route_to","url":"https://google.com"}} let(:route_to_action) {{"id":"route_to","type":"route_to","url":"https://google.com"}}
@ -188,6 +190,22 @@ describe CustomWizard::Builder do
template['steps'][0]['fields'][1] = checkbox_field template['steps'][0]['fields'][1] = checkbox_field
expect(build_wizard(template, user).steps[0].fields.length).to eq(2) expect(build_wizard(template, user).steps[0].fields.length).to eq(2)
end end
it 'returns a preset dropdown' do
template['steps'][0]['fields'][0] = dropdown_categories_field
choices = build_wizard(template, user).steps[0].fields[0].choices
expect(choices.present?).to eq(true)
expect(choices.length).to eq(2)
expect(choices.first.label).to eq("<p>cat1</p>")
end
it 'returns a filtered preset dropdown' do
template['steps'][0]['fields'][0] = dropdown_categories_filtered_field
choices = build_wizard(template, user).steps[0].fields[0].choices
expect(choices.present?).to eq(true)
expect(choices.length).to eq(1)
expect(choices.first.label).to eq("<p>cat2</p>")
end
end end
context 'on update' do context 'on update' do