Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-25 18:50:27 +01:00
Add dropdown tests and improvements
Dieser Commit ist enthalten in:
Ursprung
c182435805
Commit
6ad44d58ad
2 geänderte Dateien mit 65 neuen und 41 gelöschten Zeilen
|
@ -267,52 +267,58 @@ 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
|
||||||
|
|
||||||
|
def build_dropdown_custom(field, template)
|
||||||
|
template['choices'].each do |c|
|
||||||
|
field.add_choice(c['key'], label: c['value'])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_dropdown_translation(field, template)
|
||||||
|
choices = I18n.t(template['choices_key'])
|
||||||
|
|
||||||
if field_template['choices'] && field_template['choices'].length > 0
|
if choices.is_a?(Hash)
|
||||||
field_template['choices'].each do |c|
|
choices.each { |k, v| field.add_choice(k, label: v) }
|
||||||
field.add_choice(c['key'], label: c['value'])
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_dropdown_preset(field, template)
|
||||||
|
objects = []
|
||||||
|
guardian = Guardian.new(@wizard.user)
|
||||||
|
site = Site.new(guardian)
|
||||||
|
|
||||||
|
case template['choices_preset']
|
||||||
|
when 'categories'
|
||||||
|
objects = site.categories
|
||||||
|
when 'groups'
|
||||||
|
objects = site.groups
|
||||||
|
when 'tags'
|
||||||
|
objects = Tag.top_tags(guardian: guardian).map do |tag|
|
||||||
|
TagStruct.new(tag,tag)
|
||||||
end
|
end
|
||||||
elsif field_template['choices_key'] && field_template['choices_key'].length > 0
|
else
|
||||||
choices = I18n.t(field_template['choices_key'])
|
# do nothing
|
||||||
|
end
|
||||||
|
|
||||||
if choices.is_a?(Hash)
|
if template['choices_filters'] && template['choices_filters'].length > 0
|
||||||
choices.each { |k, v| field.add_choice(k, label: v) }
|
template['choices_filters'].each do |f|
|
||||||
end
|
objects.reject! do |o|
|
||||||
elsif field_template['choices_preset'] && field_template['choices_preset'].length > 0
|
if f['key'].include? 'custom_fields'
|
||||||
objects = []
|
o.custom_fields[f['key'].split('.')[1]].to_s != f['value'].to_s
|
||||||
guardian = Guardian.new(@wizard.user)
|
else
|
||||||
site = Site.new(guardian)
|
o[f['key']].to_s != f['value'].to_s
|
||||||
|
|
||||||
if field_template['choices_preset'] === 'categories'
|
|
||||||
objects = site.categories
|
|
||||||
end
|
|
||||||
|
|
||||||
if field_template['choices_preset'] === 'groups'
|
|
||||||
objects = site.groups
|
|
||||||
end
|
|
||||||
|
|
||||||
if field_template['choices_preset'] === 'tags'
|
|
||||||
objects = Tag.top_tags(guardian: guardian).map { |tag| TagStruct.new(tag,tag) }
|
|
||||||
end
|
|
||||||
|
|
||||||
if field_template['choices_filters'] && field_template['choices_filters'].length > 0
|
|
||||||
field_template['choices_filters'].each do |f|
|
|
||||||
objects.reject! do |o|
|
|
||||||
if f['key'].include? 'custom_fields'
|
|
||||||
o.custom_fields[f['key'].split('.')[1]].to_s != f['value'].to_s
|
|
||||||
else
|
|
||||||
o[prop].to_s != f['value'].to_s
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
if objects.length > 0
|
|
||||||
objects.each do |o|
|
if objects.length > 0
|
||||||
field.add_choice(o.id, label: o.name)
|
objects.each do |o|
|
||||||
end
|
field.add_choice(o.id, label: o.name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
||||||
|
|
Laden …
In neuem Issue referenzieren